W3cubDocs

/CSS

aspect-ratio

The aspect-ratio CSS property sets a preferred aspect ratio for the box, which will be used in the calculation of auto sizes and some other layout functions.

aspect-ratio: 1 / 1;

/* Global values */
aspect-ratio: inherit;
aspect-ratio: initial;
aspect-ratio: unset;

Values

<auto>
Replaced elements with an intrinsic aspect ratio use that aspect ratio, otherwise the box has no preferred aspect ratio. Size calculations involving intrinsic aspect ratio always work with the content box dimensions.
<ratio>
The box’s preferred aspect ratio is the specified ratio of width / height. Size calculations involving preferred aspect ratio work with the dimensions of the box specified by box-sizing.

Formal definition

Initial value auto
Applies to all elements except inline boxes and internal ruby or table boxes
Inherited no
Computed value as specified
Animation type discrete

Formal syntax

auto | <ratio>

Examples

Examples of values for aspect-ratio

aspect-ratio: 1 / 1; 
aspect-ratio: 16 / 9;

Mapping width and height to aspect-ratio

Browsers have added an internal aspect-ratio property that applies to replaced elements and other related elements that accept width and height attributes. This appears in the browser's internal UA stylesheet.

In Firefox, the internal stylesheet rule looks like this:

img, input[type="image"], video, embed, iframe, marquee, object, table {
  aspect-ratio: attr(width) / attr(height);
}

You can read more about this feature in Setting Height And Width On Images Is Important Again.

Specifications

Specification Status Comment
CSS Box Sizing Module Level 4
The definition of 'aspect-ratio' in that specification.
Editor's Draft Initial definition

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
aspect-ratio 79
79
Chrome 79 adds internal support only for mapped values
79
79
Edge 79 adds internal support only for mapped values
71
71
Firefox 71 adds internal support only for mapped values
69 — 71
Disabled
Firefox 69 adds internal support only for mapped values
Disabled From version 69 until version 71 (exclusive): this feature is behind the layout.css.width-and-height-map-to-aspect-ratio.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No No 14
14
Safari 14 adds internal support only for mapped values
Internal mapping of width and height 79 79 71
71
69 — 71
Disabled
Disabled From version 69 until version 71 (exclusive): this feature is behind the layout.css.width-and-height-map-to-aspect-ratio.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No No 14
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
aspect-ratio 79
79
Webview 79 adds internal support only for mapped values
79
79
Chrome 79 adds internal support only for mapped values
No No 14
14
Safari 14 adds internal support only for mapped values
12.0
12.0
Samsung Internet 12.0 adds internal support only for mapped values
Internal mapping of width and height 79 79 No No 14 12.0

See also

© 2005–2020 Mozilla and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio