CSS aspect-ratio Media Feature

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The aspect-ratio media feature tests the viewport’s width-to-height ratio.

Use @media (min-aspect-ratio: 16/9) for wide viewports, @media (max-aspect-ratio: 1/1) for square-or-taller ones, and @media (aspect-ratio: 1/1) for an exact match. It is a range feature — not the same as the aspect-ratio property, which sizes a single element.

Baseline · Widely available.

Not deprecated. Standard-track media feature (Media Queries Level 4). Widely available since July 2015. Prefer viewport aspect-ratio; avoid the older device-aspect-ratio feature.

Deprecated device-aspect-ratio

device-aspect-ratio is deprecated in Media Queries Level 4. Query the viewport with aspect-ratio instead.

CSS Syntax

syntax.css
@media ( aspect-ratio | min-aspect-ratio | max-aspect-ratio: <ratio>) { … }

Write ratios as width/height (for example 16/9 or 1/1). Always wrap the feature in parentheses.

Feature Values

FormDescriptionTry it
min-aspect-ratioViewport is this wide or wider (e.g. 16/9)Try it
max-aspect-ratioViewport is this tall/narrow or more (e.g. 1/1)Try it
aspect-ratioExact viewport ratio (e.g. 1/1)Try it
Range pairCombine min and max for a band of ratiosTry it

How to Use aspect-ratio

Ship a solid default layout, then branch for wide or tall viewports with min/max queries.

  1. Design the default for the most common viewport shape you support.
  2. Use min-aspect-ratio for widescreen tweaks (side-by-side columns, cinematic heroes).
  3. Use max-aspect-ratio for tall/narrow tweaks (stacked cards, larger type).
  4. Prefer ranges over exact aspect-ratio — exact matches are rare while resizing.
PieceRequired?Role
@mediaYesWraps the media feature
<ratio>YesWidth/height such as 16/9
Default layoutYesMust work when no ratio query matches
min / max variantsOptionalRange checks (most useful in practice)

Live Preview

This viewport’s ratio via matchMedia. Resize the window to see the chips and sample box update.

Detected aspect-ratio queries

Blue sample = min 16/9. Green sample = max 1/1. Exact 1/1 is uncommon while dragging a window.

aspect-ratio detection
Sample
(min-aspect-ratio: 16/9) (max-aspect-ratio: 1/1) (aspect-ratio: 1/1)

Checking…

CSS Examples

Each snippet focuses on the viewport aspect-ratio media feature. Resize the browser (or open Try It Yourself) to see the match change.

Example 1 — Widescreen with min-aspect-ratio

Change a panel color when the viewport is at least 16:9 wide.

CSS
<style>
  .panel {
    padding: 16px;
    background: #64748b;
    color: #fff;
  }

  @media (min-aspect-ratio: 16/9) {
    .panel {
      background: #2563eb;
    }
  }
</style>

<div class="panel">Hero panel</div>
Try It Yourself

How It Works

  1. Set a default. Slate panel works on every viewport.
  2. Query a minimum ratio. min-aspect-ratio: 16/9 means width/height ≥ 16/9.
  3. Enhance for wide screens. Blue background signals the widescreen branch.
  4. Resize to verify. Widen the window until the query matches.

Example 2 — Tall Viewports with max-aspect-ratio

Style a panel when the viewport is square or taller.

CSS
<style>
  .panel {
    padding: 16px;
    background: #64748b;
    color: #fff;
  }

  @media (max-aspect-ratio: 1/1) {
    .panel {
      background: #16a34a;
    }
  }
</style>

<div class="panel">Phone layout</div>
Try It Yourself

How It Works

  1. Keep a default. Slate covers wide desktops.
  2. Cap the ratio. max-aspect-ratio: 1/1 matches square and portrait.
  3. Switch the accent. Green marks the tall branch.
  4. Pair with orientation if needed. Use orientation when you only care about portrait vs landscape.

Example 3 — Exact Square Viewport

Match only when width equals height.

CSS
<style>
  .panel {
    padding: 16px;
    background: #64748b;
    color: #fff;
  }

  @media (aspect-ratio: 1/1) {
    .panel {
      background: #dc2626;
    }
  }
</style>

<div class="panel">Square viewport</div>
Try It Yourself

How It Works

  1. Default first. Slate is the everyday look.
  2. Require equality. aspect-ratio: 1/1 matches only when width equals height.
  3. Expect rare hits. Freeform window resizing rarely lands on exact 1/1.
  4. Prefer ranges. Use min/max for production layouts; keep exact matches for demos or special cases.

Edge Cases & Pitfalls

Viewport ratio is not the same as an element’s box ratio or device screen ratio.

vs property

Media feature ≠ property

@media (aspect-ratio) tests the viewport. The aspect-ratio property sets one element’s preferred ratio.

exact

Exact ratios are brittle

Browser chrome, scrollbars, and free resizing make exact aspect-ratio: 16/9 rare. Prefer min- / max- ranges.

vs orientation

Not a portrait/landscape alias

orientation is binary. aspect-ratio is numeric. A landscape phone and a 32:9 ultrawide are both landscape but different ratios.

deprecated

Skip device-aspect-ratio

device-aspect-ratio is deprecated. Stick to viewport aspect-ratio for modern CSS.

Browser Support

aspect-ratio is a browser media feature. Node.js, Deno, and Bun do not evaluate it.

✓ Baseline · Widely available since Jul 2015

aspect-ratio

Chrome 4+, Edge 12+, Firefox 3.5+, Safari 5+, Opera 10+, Samsung Internet 4+, Internet Explorer 9+.

97% Global reach
Google Chrome 4+
Full support
Mozilla Firefox 3.5+
Full support
Apple Safari 5+
Full support
Microsoft Edge 12+
Full support
Opera 10+
Full support
Samsung Internet 4+
Full support
Internet Explorer 9+
Full support
@media (aspect-ratio) Widely supported

Bottom line: Safe for production responsive design. It is not a server runtime API. Do not confuse it with the newer aspect-ratio property.

Key Takeaways

  • Role: test the viewport’s width-to-height ratio.
  • Forms: aspect-ratio, min-aspect-ratio, max-aspect-ratio.
  • vs property: media feature = viewport; property = one element.
  • Practice: prefer min/max ranges; avoid brittle exact matches.

One line: ask how wide vs tall the viewport is, then branch the layout with a ratio range.

Frequently Asked Questions

It tests the viewport’s width-to-height ratio. Use @media (aspect-ratio: 16/9), (min-aspect-ratio: …), or (max-aspect-ratio: …) to adapt layouts to wide, square, or tall viewports.
aspect-ratio matches an exact ratio. min-aspect-ratio matches that ratio or wider. max-aspect-ratio matches that ratio or taller/narrower.
The media feature checks the viewport for conditional CSS. The aspect-ratio property sets the preferred width-to-height ratio of one element (for example a video box).
No. orientation is portrait or landscape. aspect-ratio is a numeric width/height comparison such as 16/9 or 1/1. They often overlap but answer different questions.
No. device-aspect-ratio is deprecated in Media Queries Level 4. Prefer the viewport aspect-ratio feature instead.
No. It is a CSS media feature evaluated by the browser layout engine, not a Node.js or server runtime feature.

Next: height

Learn the height media feature for viewport height queries.

height tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful