Performance: Aspect ratio
Aspect ratio affects layout stability and rendering speed by controlling element dimensions before content loads.
Jump into concepts and practice - no test required
img { width: 100%; aspect-ratio: 16 / 9; }img { width: 100%; height: auto; }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No aspect-ratio on images | Multiple reflows as images load | Multiple reflows | Higher paint cost due to shifting | [X] Bad |
| Using aspect-ratio property | Single layout calculation | Single reflow | Lower paint cost, stable layout | [OK] Good |
aspect-ratio do for an element?aspect-ratioaspect-ratio property uses a ratio with a slash between numbers, no quotes or commas.16 / 9 which is the correct way to write the ratio.div {
width: 320px;
aspect-ratio: 4 / 3;
background-color: lightblue;
}div when rendered in the browser?.box {
width: 200px;
aspect-ratio: 1 / 1;
height: 150px;
background: coral;
}width and height explicitly, which conflicts with aspect-ratio.div that always stays square regardless of screen size. Which CSS setup achieves this best?width: 50vw; sets width to half the viewport width, and aspect-ratio: 1 / 1; keeps height equal to width, making a square.height: auto;, which depends on content. width: 50vw; height: 50vh; uses height: 50vh;, half viewport height, so not square on widescreen monitors. width: 50vw; max-height: 50vw; uses max-height: 50vw;, which caps but doesn't enforce square.