Aspect Ratio — CSS

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…

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;
}

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

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?

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.

What you will learn