03. Viewport & Dynamic Units (vw, vh, dvh, svh, cqw)
🟢 Beginner
1. Definition
Viewport Units scale relative to screen window dimensions: 1vw = 1% of viewport width, 1vh = 1% of viewport height. Dynamic mobile viewport units (dvh, svh, lvh) adapt to collapsible browser address bars. Container query units (cqw) scale relative to parent container width.
2. Why It Is Used
Viewport units allow building full-screen hero sections (min-height: 100dvh) and fluid typography without mobile browser URL bar scroll jumps.
3. How It Works
100vh includes the area covered by mobile browser address bars. 100dvh (Dynamic Viewport Height) updates dynamically as mobile address bars retract during scrolling. 100svh represents the smallest viewport height when bars are expanded.
4. Syntax
.full-screen-hero {
min-height: 100dvh;
}
5. Viewport Units Reference Table
| Unit | Definition | Mobile Address Bar Behavior | Best Practice Usage |
|---|---|---|---|
vw / vh | 1% Viewport Width / Height | Fixed static snapshot size | Desktop hero sections |
dvh | Dynamic Viewport Height | Resizes dynamically on scroll | Mobile Fullscreen Hero Sections |
svh | Smallest Viewport Height | Fixed minimum area when address bar visible | Mobile modals & fixed drawers |
cqw | 1% Container Query Width | Relative to parent container width | Component-driven responsive design |
6. Beginner Example
.full-width-banner {
width: 100vw;
}
7. Intermediate Example
.hero-section {
min-height: 100dvh; /* Prevents mobile scroll overflow glitches */
display: flex;
align-items: center;
justify-content: center;
}
8. Advanced Container Query Unit Example
.card-wrapper {
container-type: inline-size;
}
.card-title {
font-size: clamp(1rem, 5cqw, 2.5rem); /* Scales with container width! */
}
9. Line-by-Line Code Explanation
min-height: 100dvh;: Ensures full-screen height on mobile devices regardless of whether browser navigation bars are visible or hidden.font-size: clamp(1rem, 5cqw, 2.5rem);: Scales title font dynamically based on container query width (cqw).
10. Expected Terminal / Browser Output
A full-viewport hero container that fits mobile device screens perfectly without vertical scrollbars or layout shifts.
11. Common Mistakes & Pitfalls
- Using classic
100vhon iOS Safari: Classic100vhextends behind the mobile bottom bar, causing unexpected page scrolling. Use100dvh.
12. Important Rules & Limitations
cqwunits require declaringcontainer-type: inline-sizeon an ancestor element.
13. Multilingual Explanation: English
Viewport units scale relative to screen window dimensions. Use 100dvh for mobile full-screen layouts to handle address bar retraction properly.
14. Multilingual Explanation: Hindi (Roman Script)
Viewport units screen size par depend karte hain. Mobile par full screen hero section ke liye 100dvh ka use karein.
15. Multilingual Explanation: Marathi (Roman Script)
Viewport units screen chya aakaravar aadharit aastat. Mobile fullscreen hero layout sathi 100dvh vapara.
16. Practice Questions
- Why is
100dvhsuperior to100vhon mobile devices? - What is the difference between
vwandcqw?
17. Practical Coding Challenge
Build a full-screen hero section using min-height: 100dvh, display: flex, and centered content.