css · level 3 · unit 4 · lesson 1
- meet it
- guess first
- your turn
- fix it
- 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.
guess first
300px wide with a ratio of 3 / 2. How tall is it?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<div class="photo">Photo slot</div> .photo {
width: 300px;
aspect-ratio: 3 / 2;
background-color: #e5e7eb;
}
your turn
Your turn. Make square avatars.
Fill in the ratio so the avatar is a perfect square, whatever width it is given.
the page you’re styling
<div class="avatar">AB</div>your css
Press show my page to see it here.
- the avatar is a perfect squarenot checked yet
- the avatar stays 96px widenot checked yet
fix it
The video player ignores its ratio. Can you fix it?
the page you’re styling
<div class="player">Loading video</div>your css
Press show my page to see it here.
- the player is 225px tallnot checked yet
- the player keeps its 16 / 9 rationot checked yet
- The box has a ratio, but something else is setting its height.
- When a box has a height and a ratio, the height wins.
- Delete the
heightline, then press run.
nailed it
you kept every box in shape. :D
aspect-ratiois width to height, like16 / 9.- The browser works out the height from the width.
- Set the width or the height, never both.
new words in your collection
- aspect ratio
This lesson isn’t saved. A free account remembers every lesson you finish.