The aspect-ratio CSS media feature can be used to test the aspect ratio of the viewport.

Syntax

The aspect-ratio feature is specified as a <ratio> value representing the width-to-height aspect ratio of the viewport. It is a range feature, meaning you can also use the prefixed min-aspect-ratio and max-aspect-ratio variants to query minimum and maximum values, respectively.

Example

The example below is contained in an <iframe>, which creates its own viewport. Resize the <iframe> to see aspect-ratio in action.

HTML

<div>Watch this element as you resize your viewport's width and height.</div>

CSS

/* Exact aspect ratio */
@media (aspect-ratio: 1/1) {
  div {
    color: red;
  }
}

/* Minimum aspect ratio */
@media (min-aspect-ratio: 8/5) {
  div {
    background: yellow;
  }
}

/* Maximum aspect ratio */
@media (max-aspect-ratio: 2/1) {
  div {
    border: 2px solid blue;
  }
}

Result

Specifications

Specification Status Comment
Media Queries Level 4
The definition of 'aspect-ratio' in that specification.
Candidate Recommendation No change.
Media Queries
The definition of 'aspect-ratio' in that specification.
Recommendation Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 4Edge ? Firefox Full support 3.5IE Full support 9Opera Full support 53Safari Full support YesWebView Android Full support YesChrome Android ? Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown

Document Tags and Contributors

Last updated by: mfluehr,