---
title: "How do I make an element keep a fixed aspect ratio?"
description: "The aspect-ratio property keeps a box at a set shape, such as 16 / 9, as its width changes. Learn how it works with images, videos and cards, and why it sometimes breaks."
url: https://www.danny.engineering/answer/css/how-do-i-make-an-element-keep-a-fixed-aspect-ratio
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["CSS"]
---

# How do I make an element keep a fixed aspect ratio?

## The short answer


Use the `aspect-ratio` property. Give the element a width (or let it fill its container) and the browser works out the height from the ratio. It has been supported in all major browsers since 2021, so the old padding-top hack is no longer needed.


```css
.video {
  width: 100%;
  aspect-ratio: 16 / 9;
}
```


## Why it happens


By default, a block element's height comes from its content. A `div` meant to hold a video, a map or a placeholder image has no content when the page first renders, so it collapses to zero height, then jumps when the content arrives. Even with content, there was no direct way to say "keep this box twice as wide as it is tall".


The old workaround used the fact that percentage padding is calculated from the width: `padding-top: 56.25%` gave a 16 by 9 box. It worked, but it needed an extra wrapper and absolutely positioned children.


`aspect-ratio` sets a preferred ratio directly. When one dimension is known and the other is `auto`, the browser calculates the missing one from the ratio.


## How to fix it


### Set a ratio on the box


The value is width divided by height, written with a slash. A single number also works, so `aspect-ratio: 1` gives a square.


```css
.avatar {
  width: 4rem;
  aspect-ratio: 1;
  border-radius: 50%;
}

.card-media {
  aspect-ratio: 4 / 3;
}
```


### Crop images and videos to fit


When media does not match the box's ratio, it stretches. Add `object-fit` to crop it instead, and `object-position` to choose which part stays visible.


```css
.thumbnail {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  object-position: center;
}
```


### Size embeds such as iframes


Video embeds often arrive with fixed `width` and `height` attributes. Override the height with `auto` so the ratio can take over.


```css
iframe {
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
}
```


### Stop content from stretching the box


`aspect-ratio` is a preferred size, not a hard rule. Because a block's `min-height` defaults to `auto`, content taller than the ratio allows will make the box grow. If you would rather clip or scroll the content, add an overflow value, which lets the ratio win.


```css
.tile {
  aspect-ratio: 1;
  overflow: auto;
}
```


## Common pitfalls

- Setting both `width` and `height`. When both are fixed, there is nothing left to calculate and `aspect-ratio` is ignored.
- Wondering why text makes the box taller. That is the automatic minimum height at work; add `overflow` or `min-height: 0` if the ratio must hold.
- Forgetting `width` and `height` attributes on `img` tags. Browsers use them to reserve the right ratio before the image loads, which prevents layout shift, even when CSS sets `height: auto`.
- Stretching media without `object-fit`. The box keeps its ratio, but the picture inside gets squashed.

## Related reading

- [MDN: aspect-ratio](https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio)
- [MDN: object-fit](https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit)
