Home CSS Media Queries aspect-ratio Definition
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-ratiodevice-aspect-ratio is deprecated in Media Queries Level 4. Query the viewport with aspect-ratio instead.
Syntax
CSS Syntax @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.
Values
Feature Values Form Description Try it min-aspect-ratioViewport is this wide or wider (e.g. 16/9) Try itmax-aspect-ratioViewport is this tall/narrow or more (e.g. 1/1) Try itaspect-ratioExact viewport ratio (e.g. 1/1) Try itRange pair Combine min and max for a band of ratios Try it
Approach
How to Use aspect-ratio Ship a solid default layout, then branch for wide or tall viewports with min/max queries.
Design the default for the most common viewport shape you support. Use min-aspect-ratio for widescreen tweaks (side-by-side columns, cinematic heroes). Use max-aspect-ratio for tall/narrow tweaks (stacked cards, larger type). Prefer ranges over exact aspect-ratio — exact matches are rare while resizing. Piece Required? Role @mediaYes Wraps the media feature <ratio>Yes Width/height such as 16/9 Default layout Yes Must work when no ratio query matches min / max variants Optional Range checks (most useful in practice)
Try it
Live Preview This viewport’s ratio via matchMedia. Resize the window to see the chips and sample box update.
Code
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.
1. Widescreen 2. Tall / square 3. Exact square Example 1 — Widescreen with min-aspect-ratio Change a panel color when the viewport is at least 16:9 wide.
<style>
.panel {
padding: 16px;
background: #64748b;
color: #fff;
}
@media (min-aspect-ratio: 16/9) {
.panel {
background: #2563eb;
}
}
</style>
<div class="panel">Hero panel</div> Blue when this viewport is at least 16/9; slate otherwise.
How It Works Set a default. Slate panel works on every viewport.Query a minimum ratio. min-aspect-ratio: 16/9 means width/height ≥ 16/9.Enhance for wide screens. Blue background signals the widescreen branch.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.
<style>
.panel {
padding: 16px;
background: #64748b;
color: #fff;
}
@media (max-aspect-ratio: 1/1) {
.panel {
background: #16a34a;
}
}
</style>
<div class="panel">Phone layout</div> Green when this viewport is 1/1 or taller; slate otherwise.
How It Works Keep a default. Slate covers wide desktops.Cap the ratio. max-aspect-ratio: 1/1 matches square and portrait.Switch the accent. Green marks the tall branch.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.
<style>
.panel {
padding: 16px;
background: #64748b;
color: #fff;
}
@media (aspect-ratio: 1/1) {
.panel {
background: #dc2626;
}
}
</style>
<div class="panel">Square viewport</div> Red only on an exact 1/1 viewport. Prefer min/max for most layouts.
How It Works Default first. Slate is the everyday look.Require equality. aspect-ratio: 1/1 matches only when width equals height.Expect rare hits. Freeform window resizing rarely lands on exact 1/1.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.
Compatibility
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+.
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.
Remember
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 What does the CSS aspect-ratio media feature do? 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.
What is the difference between aspect-ratio, min-aspect-ratio, and max-aspect-ratio? aspect-ratio matches an exact ratio. min-aspect-ratio matches that ratio or wider. max-aspect-ratio matches that ratio or taller/narrower.
How is the media feature different from the aspect-ratio property? 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).
Is orientation the same as aspect-ratio? 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.
Is device-aspect-ratio still recommended? No. device-aspect-ratio is deprecated in Media Queries Level 4. Prefer the viewport aspect-ratio feature instead.
Is aspect-ratio a runtime API? 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 → About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion