0
0
CSSmarkup~10 mins

Aspect ratio in CSS - Browser Rendering Trace

Choose your learning style9 modes available
Render Flow - Aspect ratio
[Parse CSS] -> [Match selector] -> [Apply aspect-ratio property] -> [Calculate width and height] -> [Adjust box size] -> [Paint box with fixed ratio]
The browser reads the CSS, finds elements with aspect-ratio, calculates height or width to keep the ratio, then sizes and paints the box accordingly.
Render Steps - 4 Steps
Code Added:width: 10rem;
Before
[ ] (empty space, no box visible)
→
After
[__________] (a horizontal rectangle 10rem wide, height default auto, so very short)
Setting width to 10rem creates a box 10rem wide but height is still automatic, so box is very short vertically.
šŸ”§ Browser Action:Calculates box width, triggers layout and paint for width
Code Sample
A light blue box with navy border sized 10rem wide and height automatically set to keep 16:9 ratio, with centered text inside.
CSS
<div class="box">Content</div>
CSS
.box {
  width: 10rem;
  aspect-ratio: 16 / 9;
  background-color: lightblue;
  border: 0.2rem solid navy;
  padding: 1rem;
  font-size: 1.2rem;
  color: navy;
  display: flex;
  align-items: center;
  justify-content: center;
}
Render Quiz - 3 Questions
Test your understanding
After applying step 2, what is the height of the box if width is 10rem and aspect-ratio is 16/9?
A5.625rem
B9rem
C10rem
D1rem
Common Confusions - 3 Topics
Why doesn't the height change when I set only width and aspect-ratio?
The aspect-ratio property tells the browser to calculate height automatically based on width to keep the ratio. So height changes behind the scenes even if you don't set it explicitly (see step 2).
šŸ’” If width is fixed and aspect-ratio set, height adjusts automatically to keep ratio.
What happens if I set both height and aspect-ratio?
If both height and width are set, aspect-ratio is ignored because the box size is fixed. Aspect-ratio only works when one dimension is automatic.
šŸ’” Aspect-ratio controls the missing dimension, not both.
Why does the box sometimes overflow or look stretched?
If the container or parent limits size, the fixed aspect ratio box might overflow or cause scroll. Also, if width or height is set in conflicting ways, the ratio can't be kept.
šŸ’” Use aspect-ratio with flexible containers or control max sizes.
Property Reference
PropertyValue AppliedAxis/DirectionVisual EffectCommon Use
aspect-ratio16 / 9Width to HeightFixes height based on width to keep ratioResponsive media boxes, videos
aspect-ratio1 / 1Width to HeightCreates square boxesIcons, avatars
aspect-ratioautoNoneNo fixed ratio, height and width independentDefault behavior
width10remHorizontalSets box width explicitlyControl box size
heightautoVerticalHeight adjusts automatically, can be fixed by aspect-ratioDefault height behavior
Concept Snapshot
aspect-ratio fixes the ratio between width and height. Set width or height, the other adjusts automatically. Common ratios: 16/9 for videos, 1/1 for squares. Works best with flexible layouts. Ignored if both width and height are fixed.