The video box on your page looks right on a laptop. On a phone, the box gets narrower but its height stays the same. So the video sits in a box that is too tall, with black bars above and below it. What you need is a box whose height follows its width.
The relation between width and height is called the aspect ratio, or just the ratio. For example, 16:9 for a YouTube video, or 1:1 for a square. The aspect-ratio property locks that ratio: you only give the width, and the browser calculates the height. When the width changes, the height changes with it, so the shape of the box stays the same.
Locking the ratio with aspect-ratio
This rule sets the size of a video box:
.video-wrapper {
width: 100%;
aspect-ratio: 16 / 9;
}
width: 100%makes the box as wide as its parent.aspect-ratio: 16 / 9means: for every 16 parts of width, there are 9 parts of height. The left number is for the width, the right number is for the height, and a slash/separates them.heightis left out on purpose, because the browser calculates it.
The result: if the box is 400px wide, its height is 400 × 9 ÷ 16 = 225px. If the screen narrows and the box becomes 320px wide, its height changes to 180px. The shape is always a wide rectangle, like a video screen.
The formula: height = width × right number ÷ left number.
Common ratios
16 / 9: widescreen video, like YouTube.4 / 3: classic photos.1 / 1: a square, for avatars (profile photos) or thumbnails.21 / 9: ultrawide (very wide) screens.
Spaces around the / are optional: 16/9 is the same as 16 / 9.
A round avatar is made from a square with rounded corners:
.avatar {
width: 100px;
aspect-ratio: 1 / 1;
border-radius: 50%;
}
aspect-ratio: 1 / 1 makes the height equal to the width, so the result is a 100px × 100px square. border-radius: 50% rounds the corners of that square until it is a circle. If .avatar is an empty <div>, then without aspect-ratio its height is 0, so you cannot see it at all.
When height is written too
aspect-ratio only calculates a size that has not been set yet. If width and height are both written, there is no size left to calculate. So height is used, and the ratio is ignored. Write only one of them, usually width.
⚠️ Ratio not quite right?
- Using a colon.
aspect-ratio: 16:9is not valid. The separator is a slash:16 / 9. - Content that does not fit. If the text in the box is taller than the height the ratio gives, the box grows taller so the content fits. Then the ratio is no longer exact.
- An image forced into another ratio. If an
<img>gets a ratio different from the ratio of its photo, the photo is squashed. Addobject-fit: coverso the photo is cropped, not squashed.
TL;DR: aspect-ratio: 16 / 9 locks the relation between a box's width and height: for every 16 parts of width, there are 9 parts of height. Give a width, and the browser calculates the height: 400px wide gives 225px tall. 1 / 1 makes a square, and with border-radius: 50% that square becomes a circle. If width and height are both written, height is used and the ratio is ignored.
⚡ Lock the ratio
Write:
.video-wrapper {
width: 100%;
aspect-ratio: 16 / 9;
}
Meaning: aspect-ratio locks the relation between a box's width and height. 16 / 9 means: for every 16 parts of width, there are 9 parts of height. You only give the width, and the browser calculates the height. For example, a box 400px wide is 225px tall.
⚡ A slash, not a colon
Guess first: does aspect-ratio: 16:9, with a colon, work?
No. The two numbers must be separated by a slash: 16 / 9.
Meaning: A slash / separates the two numbers. Spaces around it are optional. 1 / 1 makes a square, and with border-radius: 50% that square becomes a circle.
⚡ Don't write the height too
Guess first: width, height and aspect-ratio are all written in one rule. Which one sets the height of the box: height or aspect-ratio?
height. The ratio is ignored, because there is no size left to calculate.
Meaning: aspect-ratio only calculates a size that has not been set. Write only one of width and height, usually width.