Viewport Units: vw, vh, svh, dvh
- CSS Basics
- Selectors & Specificity
- Box Model & Units
- Typography & Text Styling
- Colors & Color Spaces
- Positioning & Stacking Context
- Flexbox
- Grid
- Grid Advanced
- Responsive Design
- Container Queries
- Pseudo-classes & Pseudo-elements
- Modern Selectors
- CSS Custom Properties
- CSS Functions
- Transitions & Animations
- Transforms
- Forms & Input Styling
- CSS Architecture & Methodologies
- Accessibility in CSS
- Performance & Optimization
- View Transitions & Modern APIs
- Cascade Layers & Scope
What Viewport Units Are
Viewport units size an element relative to the browser's visible window rather than to a parent element. 1vw equals 1% of the viewport's width, and 1vh equals 1% of the viewport's height.
.hero {
height: 100vh; /* fills the full visible height */
font-size: 5vw; /* text scales with window width */
}The Problem with vh on Mobile
On mobile browsers, the address bar can appear and disappear as a user scrolls, which changes how much space is actually visible. Traditional vh is calculated against the largest possible viewport, so height: 100vh can end up taller than what is actually visible, hiding content behind the browser's UI.
svh and dvh: The Modern Fix
| Unit | Meaning |
|---|---|
svh | Small viewport height - assumes browser UI (address bar, etc.) is fully expanded |
lvh | Large viewport height - assumes browser UI is fully collapsed |
dvh | Dynamic viewport height - updates live as the browser UI shows or hides |
.full-screen-section {
height: 100dvh; /* adjusts as the address bar appears/disappears */
}Practical Guidance
For a full-height hero section or modal on mobile, dvh usually gives the most accurate result. It is still sensible to provide a vh fallback for older browsers that do not yet support the newer units.