In the ever-evolving landscape of web development, creating truly responsive designs that work flawlessly across all devices remains a significant challenge. The introduction of new viewport units—specifically svh
, lvh
, and dvh
—represents a major breakthrough for front-end developers seeking more reliable ways to control layout based on viewport dimensions.
This comprehensive guide will explore these modern viewport height units, explain how they differ from traditional units like vh
, and demonstrate practical implementation strategies through real-world code examples.
Table of Contents
- Understanding the Viewport Challenge
- Traditional Viewport Units: Limitations of VH
- Introducing the New Viewport Units
- SVH: Small Viewport Height
- LVH: Large Viewport Height
- DVH: Dynamic Viewport Height
- Browser Support and Compatibility
- Practical Implementation Examples
- Best Practices and Performance Considerations
- Conclusion
- FAQ
Understanding the Viewport Challenge
The mobile web introduced a fundamental challenge for developers: how to handle dynamic viewport sizes that change when browser UI elements appear or disappear. On mobile devices, the viewport height can vary significantly as users scroll and browser controls (like address bars) either collapse or expand.
<div class="hero-section">
<h1>Welcome to my website</h1>
<p>This content needs to fit perfectly in the viewport</p>
</div>
When using traditional viewport units like vh
, the above hero section might look perfect initially but could overflow or create unwanted scrollbars when browser UI elements appear.
Traditional Viewport Units: Limitations of VH
The viewport height unit (vh
) was introduced to make it easier to size elements relative to the viewport height, with 1vh
representing 1% of the viewport height. However, mobile browsers implemented vh
in inconsistent ways:
- Some browsers calculated
vh
based on the maximum viewport size (when browser UI is hidden) - Others used the minimum viewport size (when browser UI is visible)
- This inconsistency led to layout issues, especially for full-viewport designs
/* Traditional approach with vh units */
.hero-section {
height: 100vh; /* Can cause problems on mobile */
padding: 2rem;
display: flex;
flex-direction: column;
justify-content: center;
}
The above code might result in content being cut off or scrollbars appearing unexpectedly as users scroll and the browser UI changes.
Introducing the New Viewport Units
To address these inconsistencies, the CSS Working Group introduced new viewport units that provide developers with more precise control over how elements respond to viewport changes:
-
svh
: Small Viewport Height -
lvh
: Large Viewport Height -
dvh
: Dynamic Viewport Height
These units are part of the CSS Values and Units Module Level 4 specification and represent a significant improvement for responsive design.
SVH: Small Viewport Height
The svh
unit represents 1% of the viewport height in its smallest state—typically when mobile browser UI elements like address bars and toolbars are fully visible.
.header {
height: 100svh; /* Will fit even when browser UI is visible */
padding: 1rem;
}
When to use svh
:
- When you want to ensure content fits without scrolling even when browser UI is visible
- For critical content that must remain fully visible at all times
- For forms or interactive elements that shouldn't be hidden behind browser UI
LVH: Large Viewport Height
The lvh
unit represents 1% of the viewport height in its largest state—typically when mobile browser UI elements are hidden or collapsed.
.full-screen-image {
height: 100lvh; /* Will use the maximum available viewport height */
object-fit: cover;
}
When to use lvh
:
- For immersive experiences that should utilize maximum screen space
- For background elements that should fill the entire viewport
- When you want elements to expand to fill space when browser UI is hidden
DVH: Dynamic Viewport Height
Perhaps the most useful of the new units, dvh
(Dynamic Viewport Height) automatically adjusts between the small and large viewport states as the user scrolls and browser UI elements appear or disappear.
.hero-section {
min-height: 100dvh; /* Dynamically adjusts as viewport changes */
display: flex;
align-items: center;
justify-content: center;
}
When to use dvh
:
- For elements that should respond smoothly to viewport changes
- To avoid layout shifts when browser UI appears/disappears
- For full-height sections that should work reliably across all devices
Browser Support and Compatibility
As of 2023, these new viewport units have gained significant browser support:
- Chrome: Full support since version 108
- Firefox: Full support since version 101
- Safari: Full support since version 15.4
- Edge: Full support since version 108
For browsers that don't support these units, you can implement fallbacks:
.container {
/* Fallback for browsers that don't support dvh */
height: 100vh;
/* Modern browsers will use this instead */
height: 100dvh;
}
Practical Implementation Examples
Example 1: Full-Height Hero Section
<header class="hero">
<div class="hero-content">
<h1>Welcome to My Website</h1>
<p>Using modern viewport units for reliable layouts</p>
<button class="cta">Learn More</button>
</div>
</header>
.hero {
/* Fallback */
height: 100vh;
/* Modern approach */
height: 100dvh;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
text-align: center;
}
.hero-content {
padding: 2rem;
max-width: 800px;
}
Example 2: Fixed-Position Bottom Navigation
<nav class="bottom-nav">
<a href="#home">Home</a>
<a href="#search">Search</a>
<a href="#profile">Profile</a>
<a href="#settings">Settings</a>
</nav>
.bottom-nav {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 60px;
background: white;
display: flex;
justify-content: space-around;
align-items: center;
box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
/* Ensures the nav stays at the bottom even as viewport changes */
bottom: 0;
bottom: calc(100% - 100svh);
}
Example 3: Modal Dialog
<div class="modal-overlay">
<div class="modal">
<h2>Important Information</h2>
<p>This modal will be properly centered regardless of browser UI.</p>
<button class="close-button">Close</button>
</div>
</div>
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
/* Use dvh for dynamic adjustment */
height: 100dvh;
background: rgba(0, 0, 0, 0.7);
display: flex;
align-items: center;
justify-content: center;
}
.modal {
background: white;
padding: 2rem;
border-radius: 8px;
max-width: 500px;
width: 90%;
/* Ensure modal doesn't exceed viewport */
max-height: 90dvh;
overflow-y: auto;
}
Best Practices and Performance Considerations
When implementing these new viewport units, consider the following best practices:
- Always provide fallbacks for browsers that don't support the new units
- Test on real devices to ensure your implementation works as expected
- Combine with media queries for more sophisticated responsive designs
- Consider performance implications of dynamic units that may cause frequent layout recalculations
-
Use appropriate units for specific scenarios:
-
dvh
for elements that should adjust dynamically -
svh
for elements that should fit in the smallest viewport state -
lvh
for elements that should utilize maximum space
-
/* Progressive enhancement example */
.container {
/* Base styles with traditional units */
height: 100vh;
/* Enhanced experience with modern units */
@supports (height: 100dvh) {
height: 100dvh;
}
}
Conclusion
The introduction of svh
, lvh
, and dvh
viewport units represents a significant advancement in CSS, offering web developers more precise control over how elements respond to viewport changes. By understanding and implementing these units appropriately, you can create more reliable, responsive layouts that provide better user experiences across all devices.
As browser support continues to improve, these units will become essential tools in every front-end developer's toolkit. Start incorporating them into your projects today to future-proof your designs and solve long-standing viewport challenges.
FAQ
Q: Can I use these units for width as well as height?
A: Yes, there are corresponding width units: svw
, lvw
, and dvw
that work the same way for horizontal dimensions.
Q: What's the difference between using dvh
and just using JavaScript to detect viewport changes?
A: Using dvh
is more performant and reliable as it's handled natively by the browser without requiring JavaScript execution or event listeners.
Q: Do these units affect performance?
A: While dvh
may cause more frequent layout calculations as the viewport changes, modern browsers are optimized to handle this efficiently. For most use cases, the performance impact is negligible.
Q: How do I handle older browsers that don't support these units?
A: Always provide fallbacks using traditional units like vh
first, then override with the new units. You can also use feature detection with @supports
.
Q: Can I use these units with CSS animations and transitions?
A: Yes, these units work well with animations and transitions, allowing for smooth responsive animations that adapt to viewport changes.
This article was last updated on 19th June, 2025, reflecting the latest -browser support information and best practices for implementing modern viewport units.
Top comments (0)