π What Are Viewport Units?
Viewport units allow elements to size themselves relative to the browser window. They make layouts fluid, responsive, and adaptable to any screen size β from mobile to desktop.
Note
β No need for media queries for many responsive behaviors
π¦ The 4 Main Viewport Units
| Unit | Meaning | Equivalent |
|---|---|---|
| vw | 1% of viewport width | 1vw = 1% browser width |
| vh | 1% of viewport height | 1vh = 1% browser height |
| vmin | 1% of smaller dimension | min(vw, vh) |
| vmax | 1% of larger dimension | max(vw, vh) |
π 1. vw (Viewport Width)
vw Example
.full-width {
width: 100vw; /* Full browser width */
}50vw = half of the screen width
π 2. vh (Viewport Height)
vh Example
.full-screen {
height: 100vh; /* Full screen height */
}Useful for hero sections, splash screens, or modals.
Note
π’ 3. vmin (Viewport Minimum)
vmin is based on the smaller of the viewport width or height.
vmin Example
.circle {
width: 50vmin;
height: 50vmin;
border-radius: 50%;
}Great for ensuring shapes scale proportionally to the smallest side of the screen.
πΊ 4. vmax (Viewport Maximum)
vmax is based on the larger of the viewport dimensions.
vmax Example
.banner-text {
font-size: 5vmax; /* Scales dramatically on large screens */
}π§ͺ Practical Examples
π± Responsive Typography with vw
Fluid Text Example
h1 {
font-size: 5vw; /* Scales with width */
}Note
font-size: calc(1rem + 2vw);
πΌοΈ Full-Screen Hero Section
Hero Section
.hero {
width: 100vw;
height: 100vh;
background: url('hero.jpg') center/cover;
}π Perfectly Scaled Circle Using vmin
Circle Example
.avatar {
width: 20vmin;
height: 20vmin;
border-radius: 50%;
}Adjusts shape proportionally on all screens.
ποΈ vmax for Large Responsive Headlines
Headline
.headline {
font-size: 8vmax;
}𧬠Behavior Differences
| Unit | Best For |
|---|---|
| vw | Widths, fluid text |
| vh | Full-height sections |
| vmin | Responsive shapes, logos |
| vmax | Massive displays, hero text |
β οΈ Common Pitfalls
- β vh issues on mobile due to dynamic browser bars
- β vw-based text can become too small on mobile
- β Using vmax can create excessively large elements
- β Forgetting accessibility β text should not depend only on vw
Note
π₯ Summary
Viewport units (vw, vh, vmin, vmax) give you powerful tools for building responsive, fluid layouts without media queries. Use them with care, especially on mobile, and combine with relative units for best results.
Learn more βMDN Docs π