Reading history

CSS aspect-ratio

5 min read 0 views

Learn the CSS aspect-ratio property for responsive video frames, image cards and avatars, and how it works with object-fit: cover.

Videos are usually 16:9, profile pictures are square, and product photos often 4:3. Keeping those shapes in a responsive layout used to need a strange trick with percentage padding. Now one property does it: aspect-ratio. Give an element a width, and the browser works out the height. This lesson shows how to use it for video frames, image cards and avatars, and how it works together with object-fit.

In this lesson you will learn

  • How aspect-ratio calculates a height from a width
  • Common ratios and when to use them
  • Responsive video and image frames
  • Combining aspect-ratio with object-fit: cover
  • What happens when content does not fit

You should know the box model and object-fit.

Width In, Height Out

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>aspect-ratio</title>
  <style>
    body { font-family: sans-serif; margin: 12px; font-size: 13px; }
    .row { display: flex; gap: 12px; align-items: flex-start; }
    .box { background: #7e57c2; color: white; display: grid; place-items: center; border-radius: 6px; }
    .wide   { width: 160px; aspect-ratio: 16 / 9; }
    .square { width: 90px;  aspect-ratio: 1; }
    .tall   { width: 70px;  aspect-ratio: 3 / 4; }
    .video  { width: 100%; max-width: 320px; aspect-ratio: 16 / 9; background: #263238; margin-top: 12px; }
  </style>
</head>
<body>
  <div class="row">
    <div class="box wide">16 / 9</div>
    <div class="box square">1 / 1</div>
    <div class="box tall">3 / 4</div>
  </div>
  <div class="box video">video placeholder: always 16:9</div>
</body>
</html>

Result

  • aspect-ratio: 16 / 9 means “for every 16 units of width, use 9 of height”. The 160px-wide box becomes 90px tall.
  • aspect-ratio: 1 is short for 1 / 1: a square.
  • Set only one dimension, usually the width. If you set both width and height, the ratio is ignored.
  • The video placeholder has width: 100% with a maximum of 320px. On a phone it gets narrower, and its height shrinks with it, so it stays 16:9.
RatioShapeCommon use
16 / 9WideVideos, YouTube embeds, hero banners
4 / 3Slightly wideProduct and phone photos
1SquareAvatars, Instagram-style posts, icons
3 / 4 or 2 / 3TallBook covers, posters, portraits

Image Cards and Avatars

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>aspect-ratio cards</title>
  <style>
    body { font-family: sans-serif; margin: 12px; font-size: 13px; }
    .grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
    .card { border: 1px solid #ddd; border-radius: 8px; overflow: hidden; }
    /* The photo area keeps a 4:3 shape whatever the column width.
       object-fit: cover crops the image to fill it without stretching. */
    .card img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; display: block; background: #b2dfdb; }
    .card p { margin: 6px 8px; }
    .avatar { width: 56px; aspect-ratio: 1; border-radius: 50%; background: #ffb74d;
              display: grid; place-items: center; font-weight: bold; margin-top: 10px; }
  </style>
</head>
<body>
  <div class="grid">
    <div class="card"><img alt="Sundarbans" src="hills.jpg"><p>Sundarbans</p></div>
    <div class="card"><img alt="Cox's Bazar" src="hills.jpg"><p>Cox's Bazar</p></div>
    <div class="card"><img alt="Sylhet tea garden" src="hills.jpg"><p>Sylhet</p></div>
  </div>
  <div class="avatar">NR</div>
</body>
</html>

Result

  • The three source images have very different shapes: wide, tall and square. Every card still shows a neat 4:3 photo area, so the row lines up.
  • object-fit: cover fills that area and crops whatever does not fit, instead of stretching the picture.
  • Because the grid columns change width on smaller screens, the photo heights change too, always keeping 4:3.
  • width: 56px; aspect-ratio: 1; border-radius: 50% is a quick round avatar.
  • Giving images a ratio also helps performance: the browser reserves the right amount of space before the image loads, so the page does not jump.

width and height attributes on images

If you write <img width="800" height="600"> in HTML, modern browsers use those numbers as a default aspect ratio, even when CSS makes the image responsive. It is a good habit for every image, as covered in responsive images.

When the Content Is Too Big

aspect-ratio sets a preferred height. If the text inside needs more room, the box grows taller to fit it, rather than letting the text spill out. To keep the exact shape anyway, add overflow: auto (scroll) or overflow: hidden (cut off), or set min-height: 0.

Common Mistakes

  1. Setting both width and height, which makes the ratio do nothing.
  2. Forgetting object-fit on images, so photos are squashed into the new shape.
  3. Writing aspect-ratio: 16:9 with a colon. The correct form uses a slash: 16 / 9.
  4. Expecting the ratio to win over content. Long text makes the box taller unless overflow is controlled.

Practice Exercises

Exercise 1. A box is 300px wide with aspect-ratio: 3 / 2. How tall is it?

Show answer

300 × 2 ÷ 3 = 200px.

Exercise 2. Write CSS for an embedded YouTube iframe that fills its column and always stays 16:9.

Show answer
iframe.video {
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
  border: 0;
}

height: auto overrides the iframe’s default height so the ratio can take effect.

Frequently Asked Questions

What was the old way of keeping a ratio?

Developers put an empty wrapper with padding-top: 56.25% (9 divided by 16) and positioned the content absolutely inside it, because percentage padding is based on the width. aspect-ratio replaces that trick with one clear line.

Can I use decimal ratios?

Yes. aspect-ratio: 1.5 is the same as 3 / 2. The slash form is usually easier to read, because it matches how ratios are normally written.

Summary

  • aspect-ratio: width / height works out one dimension from the other.
  • Set only the width (or only the height) so the ratio can apply.
  • Pair it with object-fit: cover for neat, evenly sized image cards.
  • Content that needs more room makes the box grow unless overflow is controlled.

Next: CSS clip-path Shapes.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *