×

css · level 3 · unit 4 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

aspect-ratio keeps a box's shape at any width.

A box's is its shape, written as width to height. 16 / 9 is a wide video, 1 / 1 a square, 4 / 3 an old television. Set aspect-ratio: 16 / 9 and the browser works out the height from the width.

That fixes a classic problem: video frames and photo slots that jump about while the page loads. Give the box a width, or let it stretch, and it always keeps its shape.

Set a height as well and the height wins, so the ratio is ignored. Set one size, not both.

<div class="video">Trailer</div>
.video {
  width: 320px;
  aspect-ratio: 16 / 9;
  color: white;
  background-color: #264653;
}

Press show my page to see it here.