CSS aspect-ratio Property

The aspect-ratio property sets a preferred ratio between an element's width and height.

It helps elements maintain consistent proportions as their dimensions change. The property is useful for responsive images, videos, cards, embedded content, and layout containers, especially when one dimension is determined automatically.

aspect-ratio Syntax

selector {
  aspect-ratio: 16 / 9;
}

The property accepts the keyword auto, a positive ratio, or a combination of both.

.default-ratio {
  aspect-ratio: auto;
}
.square {
  aspect-ratio: 1 / 1;
}
.widescreen {
  aspect-ratio: 16 / 9;
}
.portrait {
  aspect-ratio: 3 / 4;
}
.decimal-ratio {
  aspect-ratio: 1.5;
}
.image-ratio {
  aspect-ratio: auto 4 / 3;
}

The slash separates the width component from the height component. A single number is equivalent to that number divided by 1.

The property also accepts CSS-wide values such as inherit, initial, revert, revert-layer, and unset.

Property Values

Value Description
auto Uses the element's natural aspect ratio when one is available. Otherwise, it does not introduce a preferred aspect ratio. This is the initial value.
1 / 1 Specifies equal width and height, producing square proportions.
16 / 9 Specifies widescreen proportions commonly used for video and media containers.
4 / 3 Specifies a traditional landscape proportion.
3 / 4 Specifies a portrait proportion, with height greater than width.
1.5 Specifies a width-to-height ratio of 1.5 to 1, equivalent to 3 / 2.
auto 16 / 9 Uses the specified ratio unless the element is a replaced element with a natural aspect ratio, in which case the natural ratio takes precedence.

The property applies to most elements that accept width and height sizing, but not inline boxes or internal ruby and table boxes. It is not inherited, its initial value is auto, and it is not animatable.

The computed value is the specified keyword, ratio, or combination of both.

Ratios must be positive and finite. A value such as 16 / 9 describes proportions, not a fixed width or height.

How aspect-ratio Works

The aspect-ratio property establishes a preferred relationship between an element's width and height.

For example, a box with a width of 320 pixels and an aspect ratio of 16 / 9 has a calculated height of 180 pixels when its height is automatic and no other sizing constraints interfere.

.video-box {
  width: 320px;
  height: auto;
  aspect-ratio: 16 / 9;
}

The relationship can be expressed as:

Height = Width / Aspect Ratio

For a 16:9 ratio, the calculation is 320 / (16 / 9) = 180.

The relationship also works in the opposite direction when height is specified and width is automatic.

.portrait-box {
  width: auto;
  height: 300px;
  aspect-ratio: 3 / 4;
}

In a sizing context where the preferred ratio determines the automatic width, this produces a width of 225 pixels.

The property provides a preferred ratio rather than an absolute constraint. Content, minimum sizes, maximum sizes, and layout rules can affect the final dimensions.

Common Aspect Ratios

Different aspect ratios are useful for different types of content.

Ratio Proportions Common Uses
1 / 1 Square Icons, profile images, thumbnails, and square cards.
4 / 3 Landscape Photographs, illustrations, and content previews.
3 / 2 Landscape Photography and image galleries.
16 / 9 Widescreen Videos, embedded media, and presentation previews.
21 / 9 Extra wide Wide banners and panoramic media.
3 / 4 Portrait Portrait images, posters, and vertical cards.
9 / 16 Tall portrait Vertical videos and mobile-oriented media.

The first number represents relative width, and the second represents relative height.

A ratio greater than 1 is wider than it is tall. A ratio less than 1 is taller than it is wide.

Responsive Images

The aspect-ratio property helps maintain consistent image dimensions in responsive layouts.

<img class="responsive-image" src="photo.jpg" alt="Mountain landscape">
.responsive-image {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
  object-fit: cover;
}

When the image width changes with its container, the preferred ratio provides a proportional height. The object-fit: cover declaration allows the image to fill its box, cropping portions when necessary.

For image collections, a consistent ratio helps align thumbnails even when the original files have different proportions.

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

The object-position property can control which portion of an image remains visible when cropping occurs.

.gallery img {
  object-position: center top;
}

For ordinary content images that should retain their natural proportions, width: 100% and height: auto may be sufficient without an explicit aspect ratio.

Including accurate width and height HTML attributes also helps the browser reserve space before an image loads, reducing unexpected layout shifts.

Videos and Embedded Content

The aspect-ratio property is particularly useful for responsive video players and embedded content.

Responsive Video

<video class="responsive-video" controls>
  <source src="sample-video.mp4" type="video/mp4">
  Your browser does not support HTML video.
</video>
.responsive-video {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
  object-fit: contain;
}

The video element maintains a widescreen box while adjusting to the available width. The object-fit: contain declaration keeps the complete video image visible within that box.

Responsive Iframes

<iframe class="responsive-embed" src="video-player.html" title="Video player" allowfullscreen></iframe>
.responsive-embed {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
  border: 0;
}

The iframe's height adjusts proportionally when its width changes.

This approach avoids the older technique of using percentage padding to create a responsive media container.

Use a maximum width when an embedded player should not become excessively large on wide screens.

.responsive-embed {
  width: 100%;
  max-width: 800px;
  aspect-ratio: 16 / 9;
}

Layout Containers

The aspect-ratio property also works with ordinary layout containers, including <div> elements.

Square Cards

.square-card {
  width: 100%;
  aspect-ratio: 1 / 1;
  background-color: #e8f2fb;
  border: 1px solid #1769aa;
}

The card maintains square proportions as its available width changes, provided its content and sizing constraints allow it.

CSS Grid Layout

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 15px;
}
.card-grid .card {
  aspect-ratio: 4 / 3;
  background-color: #e8f2fb;
  border: 1px solid #1769aa;
}

Each grid item uses the same preferred ratio, producing a consistent arrangement of cards as the number of columns changes.

Flexbox Layout

.flex-cards {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
}
.flex-cards .card {
  flex: 1 1 180px;
  min-width: 0;
  aspect-ratio: 3 / 2;
}

The cards can grow and shrink with the flex layout while maintaining their preferred proportions when the cross-axis sizing rules permit.

Remember that an aspect ratio does not guarantee identical dimensions if the content inside the elements requires additional space.

Replaced Elements and auto

Replaced elements, such as images and videos, can have natural dimensions and an intrinsic aspect ratio supplied by their content.

The auto keyword allows these elements to use their natural aspect ratio when one is available.

img {
  aspect-ratio: auto;
}

For an image with natural dimensions of 1200 by 800 pixels, its natural ratio is 3 / 2.

Combining auto with a Ratio

img {
  aspect-ratio: auto 3 / 2;
}

When both auto and a ratio are specified, the ratio is used as a preferred ratio until the replaced element has a natural aspect ratio available. The natural ratio then takes precedence.

This is useful for reserving approximately the correct amount of space before an image finishes loading.

For non-replaced elements, the specified ratio is used because they do not have an intrinsic aspect ratio supplied by replaced content.

Overriding a Natural Ratio

img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
}

Without auto in the aspect-ratio value, the specified square ratio is preferred even when the image has different natural proportions.

The object-fit property determines how the image content fits inside the resulting box.

Sizing Behavior and Constraints

The aspect-ratio property participates in automatic sizing. It does not always override other width, height, or content requirements.

One Automatic Dimension

.box {
  width: 300px;
  height: auto;
  aspect-ratio: 3 / 2;
}

For an empty box without conflicting constraints, the preferred ratio produces a height of 200 pixels.

Both Dimensions Explicitly Set

.box {
  width: 300px;
  height: 100px;
  aspect-ratio: 3 / 2;
}

Because both dimensions are explicitly specified, the box remains 300 by 100 pixels. The preferred aspect ratio does not override those definite sizes.

Minimum and Maximum Sizes

.box {
  width: 100%;
  max-width: 400px;
  min-height: 180px;
  aspect-ratio: 16 / 9;
}

The preferred ratio is considered during sizing, but the minimum height can prevent the final box from maintaining the exact ratio when its width becomes too small.

Content Overflow

Content can also affect an element's final size. A container with substantial text may need more height than its preferred ratio would otherwise produce.

.content-card {
  width: 250px;
  aspect-ratio: 1 / 1;
  padding: 20px;
  box-sizing: border-box;
}

The card prefers a square shape, but its automatic minimum size and content requirements can cause it to become taller.

When strict dimensions are required, consider explicit sizing, overflow behavior, and the impact on content accessibility.

Box Sizing

The preferred ratio applies to the box dimensions specified by box-sizing.

.border-box-example {
  width: 200px;
  aspect-ratio: 1 / 1;
  padding: 15px;
  border: 2px solid #1769aa;
  box-sizing: border-box;
}

With box-sizing: border-box, the preferred ratio applies to the border box, including padding and borders. With content-box, it applies to the content box instead.

aspect-ratio Example

The following example displays three boxes with different aspect ratios. Their widths adjust to the available space while their heights follow their preferred proportions.

<div class="ratio-demo">
  <div class="ratio-item">
    <strong>1 / 1</strong>
    <div class="ratio-box square">Square</div>
  </div>
  <div class="ratio-item">
    <strong>4 / 3</strong>
    <div class="ratio-box landscape">Landscape</div>
  </div>
  <div class="ratio-item">
    <strong>16 / 9</strong>
    <div class="ratio-box widescreen">Widescreen</div>
  </div>
</div>
.ratio-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 15px;
}
.ratio-item strong {
  display: block;
  margin-bottom: 8px;
  text-align: center;
}
.ratio-box {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
  padding: 10px;
  border: 2px solid #1769aa;
  background-color: #e8f2fb;
  color: #174c78;
  font-weight: bold;
}
.ratio-box.square {
  aspect-ratio: 1 / 1;
}
.ratio-box.landscape {
  aspect-ratio: 4 / 3;
}
.ratio-box.widescreen {
  aspect-ratio: 16 / 9;
}

Each box has an automatically calculated height based on its width and specified aspect ratio.

Browser Result
1 / 1
Square
4 / 3
Landscape
16 / 9
Widescreen

Resize the browser window to see how the boxes adjust. The square remains equal in width and height, while the landscape and widescreen boxes maintain their respective proportions.

At narrower widths, the grid automatically changes its column arrangement while preserving the preferred ratio of each box.

Important Considerations

Keep the following points in mind when using aspect-ratio:

  • Default value: The initial value is auto, allowing replaced elements to use their natural proportions.
  • Preferred ratio: The property establishes a preferred relationship between width and height rather than an unconditional size restriction.
  • Automatic sizing: At least one dimension generally needs to be automatic for the ratio to influence the resulting size.
  • Explicit dimensions: When both width and height are definite, the ratio does not override those sizes.
  • Content constraints: Content, minimum sizes, and maximum sizes can prevent the preferred ratio from being maintained exactly.
  • Responsive layouts: The property is useful with percentage widths, CSS Grid, and Flexbox.
  • Replaced elements: Images and videos may have natural aspect ratios that affect their sizing.
  • Image cropping: Use object-fit and object-position to control how image content fits within a specified ratio.
  • Box sizing: The box-sizing property determines whether the preferred ratio applies to the content box or border box.
  • Layout stability: Appropriate aspect ratios can reserve space for media before it loads, reducing layout shifts.

Use aspect-ratio when elements should scale proportionally without manually calculating both dimensions at every screen size.

Browser Support

The aspect-ratio property has Baseline Widely available status and is supported in modern versions of Chrome, Edge, Firefox, Safari, and other major browsers.

Older browsers may not support the property. When necessary, provide explicit dimensions or a compatible fallback layout.

.media-box {
  width: 100%;
  height: 200px;
}
@supports (aspect-ratio: 16 / 9) {
  .media-box {
    height: auto;
    aspect-ratio: 16 / 9;
  }
}

Browsers that support aspect-ratio calculate the height from the preferred ratio. Older browsers retain the fixed-height fallback.

Visit Can I Use? to check current browser compatibility.

Specifications

The aspect-ratio property is defined in the CSS Box Sizing Module Level 4 specification.

CSS Box Sizing Module Level 4