0
0
CSSmarkup~10 mins

Viewport units in CSS - Browser Rendering Trace

Choose your learning style9 modes available
Render Flow - Viewport units
Parse CSS
Identify viewport units (vw, vh, vmin, vmax)
Compute element sizes based on viewport units
Apply sizes to elements
Layout
The browser reads CSS, detects viewport units, calculates the viewport size, then computes element sizes relative to that viewport before laying out and painting the page.
Render Steps - 6 Steps
Code Added:<div class="box">Viewport unit box</div>
Before





After
[box]
|Viewport unit box|
[     empty     ]
Adding the div element with text creates a visible box with default size and inline layout.
🔧 Browser Action:Creates DOM node and renders default inline box
Code Sample
A blue box sized to half the viewport width and 30% of the viewport height, centered text inside.
CSS
<div class="box">Viewport unit box</div>
CSS
.box {
  width: 50vw;
  height: 30vh;
  background-color: #4a90e2;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  border-radius: 0.5rem;
}
Render Quiz - 3 Questions
Test your understanding
After applying step 3 (height: 30vh), what happens to the box's height?
AIt stays the default height
BIt becomes 30% of the viewport height
CIt becomes 30% of the viewport width
DIt becomes 30 pixels tall
Common Confusions - 3 Topics
Why does the box size change when I resize the browser window?
Viewport units (vw, vh) depend on the size of the browser window. When you resize, the viewport changes, so the box resizes accordingly (see render_steps 2 and 3).
💡 Viewport units always scale with the visible browser area.
Why doesn't 100vw always equal the full width of the screen?
100vw is the width of the viewport, which excludes browser UI like scrollbars. So if a vertical scrollbar appears, 100vw might be slightly wider than the visible content area.
💡 Viewport units measure the visible browser area, not the entire screen.
Why does text inside the box stay centered vertically and horizontally?
Because display:flex with align-items:center and justify-content:center centers content inside the box (see render_step 5).
💡 Flexbox is a simple way to center content inside containers.
Property Reference
PropertyValue AppliedDescriptionVisual EffectCommon Use
width50vwWidth is 50% of viewport widthBox stretches horizontally to half screen widthResponsive width sizing
height30vhHeight is 30% of viewport heightBox height is 30% of screen heightResponsive height sizing
font-size2remFont size relative to root font sizeText appears largerReadable text sizing
border-radius0.5remRounded corners with 0.5 rem radiusBox corners are roundedVisual softness
background-color#4a90e2Blue background colorBox background changes colorVisual emphasis
colorwhiteText color whiteText is visible on dark backgroundContrast for readability
Concept Snapshot
Viewport units (vw, vh) size elements relative to the browser window. 1vw = 1% of viewport width, 1vh = 1% of viewport height. They help create responsive layouts that adapt to screen size. Commonly used for widths, heights, font sizes, and spacing. Viewport units change dynamically when the window resizes. Combine with flexbox for centered, responsive content.