CSS: aspect-ratio

The aspect-ratio property defines a preferred width-to-height ratio for an element.

What you will learn

Basic example

.video-frame {
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #eee;
}

When the width changes, the preferred height follows the ratio. This helps prevent layout jumps while a video or image is loading.

Card image example

.card img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
}

aspect-ratio controls the box shape. object-fit controls how replaced content such as an image fits inside that box.

Common mistakes