Dynamic viewport units provide more control over sizing elements relative to the viewport, even when browser UI elements like address bars are present. This ensures a consistent layout across different devices and browser states.
Example 1: Using svw
for Small Viewport
.hero-section {
width: 100svw; /* 100% of the small viewport width */
height: 70svh; /* 70% of the small viewport height */
background-color: lightblue;
display: flex;
justify-content: center;
align-items: center;
font-size: 3svh; /* Font size scales with small viewport height */
}
/* Consider how svw handles situations where browser UI is visible */
/* Useful for content that should always fit within the 'safest' visible area */
Explanation svw
and svh
represent the small viewport width and height, respectively. This means the size is calculated excluding any dynamic browser UI elements (like the address bar) that might be present, providing a "safer" or "smaller" visible area.
Example 2: Using lvw
for Large Viewport
.full-page-overlay {
width: 100lvw; /* 100% of the large viewport width */
height: 100lvh; /* 100% of the large viewport height */
background-color: rgba(0, 0, 0, 0.5);
position: fixed;
top: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-size: 5lvw; /* Font size scales with large viewport width */
}
/* lvw is ideal for elements that should occupy the maximum possible space */
/* Useful for overlays or backgrounds that extend under browser UI */
Explanation lvw
and lvh
represent the large viewport width and height. These units calculate the size including any dynamic browser UI elements, providing the largest possible viewport dimension.
Example 3: Using dvw
for Dynamic Viewport
.responsive-card {
width: 90dvw; /* 90% of the dynamic viewport width */
max-width: 500px;
margin: 2dvh auto; /* Margin scales with dynamic viewport height */
padding: 20px;
border: 1px solid #ccc;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
font-size: 2.5dvh; /* Font size scales with dynamic viewport height */
}
/* dvw adjusts dynamically based on the presence or absence of browser UI */
/* Provides a balanced approach for responsive designs */
Explanation dvw
and dvh
represent the dynamic viewport width and height. These units automatically adjust based on whether browser UI elements are present or not, providing a fluid and adaptive sizing solution.
Addressing Browser UI Elements in Viewport Calculations
Traditional viewport units (vw
, vh
) don't account for dynamic browser UI, leading to potential layout shifts. Dynamic viewport units (svw
, lvw
, dvw
) solve this by providing different viewport definitions based on browser UI presence, ensuring more stable layouts.
Example 4: vw
vs dvw
in a Sticky Header
.header-old {
position: sticky;
top: 0;
width: 100vw; /* May cause layout shift when address bar appears/disappears */
background-color: #f8f8f8;
padding: 15px;
text-align: center;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.header-new {
position: sticky;
top: 0;
width: 100dvw; /* Adapts dynamically to browser UI changes */
background-color: #e6e6e6;
padding: 15px;
text-align: center;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
/* Observe the difference in how these headers behave on mobile browsers */
/* The 'new' header provides a more consistent experience */
Explanation This example highlights how 100vw
can lead to layout shifts when the browser's dynamic UI (like the address bar on mobile) appears or disappears. Conversely, 100dvw
adapts fluidly, maintaining a more stable layout.
Example 5: Impact on Full-Screen Modals
.modal-legacy {
position: fixed;
top: 0;
left: 0;
width: 100vw; /* Might be obscured by browser UI or cause scroll issues */
height: 100vh; /* Can result in content being pushed off-screen */
background-color: rgba(0, 0, 0, 0.7);
display: flex;
justify-content: center;
align-items: center;
color: white;
}
.modal-modern {
position: fixed;
top: 0;
left: 0;
width: 100dvw; /* Ensures modal always fits the current visible viewport */
height: 100dvh; /* Prevents unwanted scrolling due to UI elements */
background-color: rgba(0, 0, 0, 0.7);
display: flex;
justify-content: center;
align-items: center;
color: white;
}
/* Test these modals on a mobile device to see the difference */
/* The 'modern' modal provides a true full-screen experience */
Explanation Using 100vw
and 100vh
for full-screen modals can lead to issues where the modal is partially hidden by browser UI or causes unwanted scrolling. 100dvw
and 100dvh
guarantee that the modal always fits within the dynamically adjusted visible viewport, providing a seamless user experience.