The aspect-ratio property sets the preferred width-to-height ratio of a box. Write 16 / 9 and, if the width is known and the height is auto, the browser calculates the height. The ratio is width divided by height.
The initial value is auto. It is not inherited. One side must stay automatic, or the ratio does not change the used size. It applies to elements except inline boxes and internal table or ruby boxes. A ratio can be animated when one side stays automatic.
Standard-track.
Not deprecated and not experimental. It has been widely available since September 2021: Chrome 88, Firefox 89, and Safari 15. Internet Explorer does not support it.
Syntax
CSS Syntax
A ratio, the keyword auto, or both. The first number is the width. The second is the height.
unset equals initial here, so it is auto. revert is the user-agent value, also auto. A ratio with a 0, such as 1 / 0, is degenerate and behaves as auto. If both width and height are lengths, the ratio does not change the used size.
Approach
How to Use aspect-ratio
Give the box one size. Let the ratio calculate the other.
Set width, or let the box stretch across its container. Leave height at auto.
Set aspect-ratio to width / height, such as 16 / 9 or 1 / 1.
If you also set height to a length, the ratio no longer changes the used size. Remove one of the two lengths.
On an image, auto 16 / 9 holds that frame until the file's own ratio is known.
Piece
Required?
Role
One automatic side
Yes
The ratio fills in the missing side
16 / 9
The ratio
Width divided by height
Both sides as lengths
Blocks the ratio
The used size stays at those lengths
auto plus a ratio
Images
Placeholder ratio, then the file's own ratio
Try it
Live Preview
The box width stays 10rem. Only the ratio changes, so the height follows. auto lets the text set the height.
Width is fixed. Height is auto, so the ratio can calculate it.
Preview
1 / 1
aspect-ratio: 1 / 1
Code
CSS Examples
Each box has a width and an automatic height. The line to study is aspect-ratio. Use Try It Yourself for a live editor.
In a row flex container, align-items: stretch can set the height. Both sides are then definite, so the ratio does not change the box. align-self: flex-start leaves the height automatic.
auto
auto with a ratio follows the image
aspect-ratio: auto 3 / 2 holds 3 / 2 only until a replaced element has a natural ratio. A loaded image then uses its own ratio. A div has none, so it stays 3 / 2. object-fit sizes the picture inside the box. It is a different property.
box
The ratio follows box-sizing
With content-box, the ratio is the content box. Padding and border sit outside it. With border-box, padding and border are inside the ratio.
inline
Inline boxes ignore it
An inline span does not take a ratio. Set display to block or inline-block. Internal table and ruby boxes are excluded too.
0
Zero and negative numbers
A negative number is invalid, so the declaration is dropped. A 0 on either side is a degenerate ratio and behaves as auto.
Compatibility
Browser Support
aspect-ratio is a browser sizing property. Node.js, Deno, and Bun do not apply it.
✓ Baseline · Since Sep 2021
aspect-ratio
Chrome 88+, Edge 88+, Firefox 89+, Safari 15+, Opera 74+, and Samsung Internet 15+. No support in Internet Explorer.
96%Modern browser support
Google Chrome88+
Full support
Mozilla Firefox89+
Full support
Apple Safari15+
Full support
Microsoft Edge88+ · Chromium
Full support
Opera74+
Full support
Samsung Internet15+
Full support
Internet ExplorerNot supported
Not supported
aspect-ratioWidely supported
Bottom line: Safe in current Chrome, Edge, Firefox, Safari, Opera, and Samsung Internet. It has been baseline since September 2021, when Safari 15 shipped. Internet Explorer ignores it, so a fixed width and height is the fallback there.
Remember
Key Takeaways
Role: preferred width / height. One automatic side is what the ratio fills in.
Default:auto. A div has no preferred ratio. An image uses its natural one.
Numbers:16 / 9 is width / height. 1 means 1 / 1. 0.5 is a tall box.
Blocked: two lengths, min and max sizes, and inline boxes. The property is not inherited.
One line:aspect-ratio keeps width / height, as long as one side is left automatic.
Frequently Asked Questions
It sets a preferred width-to-height ratio. If one side is automatic, the browser calculates the other side so the box keeps that ratio.
The initial value is auto. A normal box then has no preferred ratio. A replaced element, such as an image, uses its own natural ratio. The property is not inherited.
Both width and height are already lengths. The ratio only fills in an automatic side. Set one side, such as width: 10rem, and leave the other at auto.
16 / 9 is about 1.777. A single number is width divided by height, and the missing height is 1. So 1 is a square, and 0.5 is twice as tall as it is wide.
The 16 / 9 ratio is used until the image has a natural ratio. After the file loads, auto switches to that natural ratio. A plain div has no natural ratio, so it keeps 16 / 9.
Chrome 88+, Edge 88+, Firefox 89+, Safari 15+, Opera 74+, and Samsung Internet 15+. It has been a baseline feature since September 2021. Internet Explorer does not support it. Node.js, Deno, and Bun do not apply it.