Skip to content

Latest commit

 

History

History
145 lines (103 loc) · 4.63 KB

index.md

File metadata and controls

145 lines (103 loc) · 4.63 KB
title slug page-type browser-compat
aspect-ratio
Web/CSS/aspect-ratio
css-property
css.properties.aspect-ratio

{{CSSRef}}

The aspect-ratio CSS property allows you to define the desired width-to-height ratio of an element's box. This means that even if the parent container or viewport size changes, the browser will adjust the element's dimensions to maintain the specified width-to-height ratio. The specified {{glossary("aspect ratio")}} is used in the calculation of auto sizes and some other layout functions.

At least one of the box's sizes needs to be automatic in order for aspect-ratio to have any effect. If neither the width nor height is an automatic size, then the provided aspect ratio has no effect on the box's preferred sizes.

{{EmbedInteractiveExample("pages/css/aspect-ratio.html")}}

Syntax

aspect-ratio: 1 / 1;
aspect-ratio: 1;

/* fallback to 'auto' for replaced elements */
aspect-ratio: auto 3/4;
aspect-ratio: 9/6 auto;

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

This property is specified as one or both of the keyword auto or a <ratio>. If both are given, and the element is a replaced element, such as <img>, then the given ratio is used until the content is loaded. After the content is loaded, the auto value is applied, so the intrinsic aspect ratio of the loaded content is used.

If the element is not a replaced element, then the given ratio is used.

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.
  • {{cssxref("<ratio>")}}

    • : The box's preferred aspect ratio is the specified ratio of width / height. If height and the preceding slash character are omitted, height defaults to 1. Size calculations involving preferred aspect ratio work with the dimensions of the box specified by box-sizing.
  • auto && <ratio>

    • : When both auto and a <ratio> are specified together, auto is used if the element is a replaced element with a natural aspect ratio, like an <img> element. Otherwise, the specified ratio of width / height is used as the preferred aspect ratio.

Formal definition

{{cssinfo}}

Formal syntax

{{csssyntax}}

Examples

Exploring aspect-ratio effects with fixed width

In this example, the width of the <div> elements has been set to 100px and height to auto. Since the width value is fixed here, the aspect-ratio property affects only the height of the <div> elements to maintain the specified width-to-height ratio.

<div>1/1</div>
<div>0.5</div>
<div>1</div>
<div>1/0.5</div>
<div>16/9</div>
div {
  display: inline-flex;
  background-color: lime;
  justify-content: center;
}
div {
  width: 100px;
  height: auto;
}
div:nth-child(1) {
  aspect-ratio: 1/1;
}
div:nth-child(2) {
  aspect-ratio: 0.5;
}
div:nth-child(3) {
  aspect-ratio: 1;
}
div:nth-child(4) {
  aspect-ratio: 1/0.5;
}
div:nth-child(5) {
  aspect-ratio: 16/9;
}

{{EmbedLiveSample('Exploring aspect-ratio effects with fixed width', '100%', '300px')}}

Fallback to natural aspect ratio

In this example we are using two <img> elements. The first element does not have its src attribute set to an image file.

<img src="" /> <img src="plumeria.jpg" />

The following code sets 3/2 as the preferred aspect ratio and auto as a fallback.

img {
  display: inline;
  width: 200px;
  border: 2px dashed red;
  background-color: lime;
  vertical-align: top;

  aspect-ratio: 3/2 auto;
}

Note how the first image without replaced content keeps the 3/2 aspect ratio, while the second image after the content is loaded uses the image's natural aspect ratio.

{{EmbedLiveSample('Fallback to natural aspect ratio', '100%', '300px')}}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also