Viewport Units for Scaling
You'll learn how to use viewport-based units (vw, vh, vmin, vmax) to create font sizes that automatically scale with the browser window size.
What Are Viewport Units for Scaling?
Viewport units let you size text (and other elements) as a percentage of the browser window's dimensions. Instead of fixed pixel sizes or relative em/rem units, viewport units make elements grow and shrink dynamically as users resize their browser or switch devices.
Think of it like a billboard: a sign painter doesn't use the same letter size for a roadside billboard and a highway billboard—the letters scale to the viewing distance. Viewport units work similarly, adjusting content size based on available screen space.
The Four Viewport Units
vw(viewport width): 1vw = 1% of the viewport's widthvh(viewport height): 1vh = 1% of the viewport's heightvmin: Uses whichever is smaller, vw or vhvmax: Uses whichever is larger, vw or vh
Scaling Font Sizes
You can apply these units to font-size to create text that responds to viewport changes:
h1 {
font-size: 5vw; /* Heading grows/shrinks with viewport width */
}
.hero-text {
font-size: 3vmin; /* Uses the smaller dimension for consistency */
}
On a 1000px-wide screen, 5vw equals 50px. On a 500px-wide mobile screen, it automatically becomes 25px—no media queries needed!
When to Use Each Unit
Use vw for horizontal scaling, vh for full-height designs, vmin for proportional scaling on any orientation, and vmax for maintaining large text on any screen.
Key Takeaway: Viewport units (vw, vh, vmin, vmax) create fluid font sizes that scale automatically with the browser window, giving you responsive typography without breakpoints.