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

UnitDefinitionMobile Address Bar BehaviorBest Practice Usage
vw / vh1% Viewport Width / HeightFixed static snapshot sizeDesktop hero sections
dvhDynamic Viewport HeightResizes dynamically on scrollMobile Fullscreen Hero Sections
svhSmallest Viewport HeightFixed minimum area when address bar visibleMobile modals & fixed drawers
cqw1% Container Query WidthRelative to parent container widthComponent-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 100vh on iOS Safari: Classic 100vh extends behind the mobile bottom bar, causing unexpected page scrolling. Use 100dvh.

12. Important Rules & Limitations

  • cqw units require declaring container-type: inline-size on 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

  1. Why is 100dvh superior to 100vh on mobile devices?
  2. What is the difference between vw and cqw?

17. Practical Coding Challenge

Build a full-screen hero section using min-height: 100dvh, display: flex, and centered content.

18. Related Topics