CSS viewport units are essential for creating responsive layouts that adapt to different screen sizes. These units measure dimensions relative to the viewport's height and width, allowing developers to design flexible and adaptable web interfaces. This guide will cover the traditional vh and vw units and introduce newer units like dvh, lvh, and svh, explaining how they enhance responsiveness and handle viewport changes more effectively.
Viewport units are relative units that adjust dynamically to the size of the browser's viewport. The most commonly used are:
These units make it easy to design elements that scale with the size of the browser window. For instance:
div { width: 50vw; /* 50% of the viewport width */ height: 100vh; /* 100% of the viewport height */ background-color: lightblue; }
In this example, the div spans half the width and the full height of the viewport.
While vh and vw work well in most scenarios, they don’t account for certain dynamic changes in the viewport, such as:
These limitations can cause designs to appear inconsistent or result in unintended layout issues, especially on mobile.
To address these issues, CSS introduced three new units: dvh (Dynamic Viewport Height), lvh (Large Viewport Height), and svh (Small Viewport Height).
dvh adjusts dynamically to changes in the viewport, such as the appearance or disappearance of browser UI elements. It represents 1% of the current viewport height, ensuring your layout adapts in real-time.
Example:
div { height: 100dvh; /* Adjusts dynamically to visible viewport height */ background-color: lightgreen; }
This ensures the div always fits the visible area, even when browser UI changes.
lvh represents 1% of the largest possible viewport height, ignoring dynamic UI changes (like mobile address bars).
Example:
div { height: 100lvh; /* Fixed to the maximum viewport height */ background-color: lightcoral; }
This is useful for layouts that need to maintain consistency, regardless of browser UI changes.
svh represents 1% of the smallest possible viewport height, accommodating scenarios where browser UI occupies a significant portion of the screen.
Example:
div { width: 50vw; /* 50% of the viewport width */ height: 100vh; /* 100% of the viewport height */ background-color: lightblue; }
This unit is particularly helpful when dealing with devices where UI elements like keyboard pop-ups can shrink the visible area.
vw measures 1% of the viewport width. It remains consistent and isn’t affected by dynamic changes like scrolling or UI appearance.
Example:
div { height: 100dvh; /* Adjusts dynamically to visible viewport height */ background-color: lightgreen; }
This is ideal for horizontal layouts or full-width designs.
Here’s how these units can be applied in real-world scenarios:
div { height: 100lvh; /* Fixed to the maximum viewport height */ background-color: lightcoral; }
This ensures the hero section always fits the screen, even when mobile address bars hide or appear.
div { height: 100svh; /* Adjusts to the smallest viewport height */ background-color: lightgoldenrodyellow; }
Using svh ensures the modal remains usable even when the on-screen keyboard reduces the viewport height.
div { width: 100vw; /* Full viewport width */ background-color: lightpink; }
A sticky footer that maintains its size across various devices.
You can mix and match these units for more adaptive designs. For example:
.hero { height: 100dvh; /* Ensures the hero fits the visible viewport */ width: 100vw; background: url('hero.jpg') no-repeat center center/cover; }
This approach ensures that the design adapts to all possible viewport scenarios.
While vh and vw are widely supported across browsers, dvh, lvh, and svh are newer additions. Ensure you check browser compatibility and provide fallbacks for older browsers.
Fallback Example:
.modal { height: 100svh; /* Accounts for the smallest viewport height */ width: 100vw; overflow-y: auto; /* Allows scrolling if needed */ background-color: white; }
CSS viewport units like vh, vw, dvh, lvh, and svh are powerful tools for creating responsive and adaptable web designs. While vh and vw handle most cases, the newer dvh, lvh, and svh units address limitations, especially on mobile devices. By understanding and leveraging these units, mobile app developers can craft seamless, user-friendly designs that work across all devices and scenarios.
The above is the detailed content of CSS Viewport Units: CSS *vh (dvh, lvh, svh) and *vw units. For more information, please follow other related articles on the PHP Chinese website!