The aspect-ratio media feature matches the viewport’s width-to-height proportion. Adapt layouts for widescreen desktops, square tablets, and tall phone screens without guessing from pixel width alone.
01
@media
Media query.
02
16/9
Widescreen.
03
min-
At least.
04
max-
At most.
05
Portrait
Tall screens.
06
vs property
Viewport vs element.
Fundamentals
Introduction
The @media rule applies CSS when certain device conditions are met. The aspect-ratio media feature checks the viewport’s width-to-height ratio — for example, whether the browser window is wider than it is tall (landscape) or taller than it is wide (portrait).
This is especially useful for responsive design. Two devices may share the same pixel width but have very different shapes. A phone in landscape and a narrow desktop window can both be wide, but their aspect ratios tell you how much vertical space is available for content.
Definition and Usage
Write ratios as width/height inside parentheses: @media (aspect-ratio: 16/9), @media (min-aspect-ratio: 4/3), or @media (max-aspect-ratio: 1/1). When the viewport matches, the nested CSS rules apply.
💡
Beginner Tip
Do not confuse this with the aspect-ratio CSS property, which sets an element’s shape. The media feature reacts to the browser window proportions for layout decisions.
Foundation
📝 Syntax
Use aspect-ratio features inside an @media block. Ratios are written as two integers separated by a slash:
syntax.css
@media(aspect-ratio:16/9){/* Exact 16:9 viewport */}@media(min-aspect-ratio:4/3){/* Viewport is 4:3 or wider */}@media(max-aspect-ratio:1/1){/* Viewport is square or taller (portrait) */}
Feature Variants
aspect-ratio — Matches an exact viewport ratio such as 16/9 or 4/3.
min-aspect-ratio — Matches when the viewport ratio is equal to or wider than the value (more horizontal).
max-aspect-ratio — Matches when the viewport ratio is equal to or narrower/taller than the value (more vertical).
Combining min-width and min-aspect-ratio avoids three columns on narrow-but-wide windows or tall tablets.
Tips
💬 Usage Tips
Prefer ranges — Use min-aspect-ratio and max-aspect-ratio instead of exact ratios for reliable matches.
Portrait phones — max-aspect-ratio: 1/1 is a simple portrait detector.
Combine with width — Pair aspect-ratio with min-width to avoid layout jumps on odd window sizes.
Test by resizing — Drag the browser corner to see when queries fire; use DevTools responsive mode.
Element vs viewport — Use the aspect-ratio property for boxes; use the media feature for page layout.
Watch Out
⚠️ Common Pitfalls
Exact ratio fragility — aspect-ratio: 16/9 matches only precisely; one pixel off and styles won’t apply.
Confusing with orientation — orientation: portrait checks rotation; aspect-ratio checks numeric proportion (they often overlap but differ).
Replacing width queries — Aspect-ratio complements width breakpoints; it rarely replaces them entirely.
Colon vs slash — Media queries use 16/9; the CSS property uses 16 / 9 with spaces.
Ultrawide monitors — Very wide ratios may need upper bounds with max-aspect-ratio to cap layout width.
A11y
♿ Accessibility
Content reflow — Ensure stacked portrait layouts preserve reading order and logical focus sequence.
Zoom and resize — Users may resize windows; avoid hiding essential content at specific ratios only.
Orientation changes — Test layout shifts when users rotate mobile devices.
Reduced motion — Pair layout changes with prefers-reduced-motion if animations accompany ratio switches.
Keyboard access — Layout changes must not trap focus or remove reachable controls.
🧠 How aspect-ratio Works
1
Browser measures viewport
The user agent reads the current window width and height in CSS pixels.
Measure
2
Ratio is calculated
Width ÷ height is compared against your aspect-ratio, min-, or max- values.
Compare
3
@media rules apply
Matching media query blocks update layout, typography, and backgrounds.
Apply
=
📱
Shape-aware layout
Design adapts to how wide or tall the viewport actually is.
Compatibility
🖥 Browser Compatibility
The aspect-ratio media feature and its min- / max- variants are supported in all modern browsers.
✓ Baseline · Modern browsers
Viewport ratio queries in production
Works in Chrome, Firefox, Safari, Edge, and Opera.
97%Global support
aspect-ratio media feature97% supported
Bottom line:aspect-ratio media queries are safe for responsive layout patterns. Test by resizing the browser on real devices.
Wrap Up
🎉 Conclusion
The aspect-ratio media feature gives you fine-grained control over responsive layouts based on viewport shape. Whether you are targeting widescreen heroes, portrait stacking, or tablet-friendly grids, ratio-based queries complement traditional width breakpoints.
Remember the distinction: the media feature responds to the window; the aspect-ratio property shapes individual elements. Use both together for layouts that look great on every screen proportion.
Use these points when adapting layouts to viewport shape.
5
Core concepts
@01
Viewport
Window shape.
Scope
16:902
16/9
Widescreen.
Ratio
min03
min-/max-
Ranges.
Syntax
1:104
Portrait
max 1/1.
Pattern
🌐05
97% support
All browsers.
Compat
❓ Frequently Asked Questions
The aspect-ratio media feature matches when the viewport width-to-height ratio equals a specified value, such as 16/9 or 4/3. Use it inside @media rules to adapt layouts for widescreen, square, or tall portrait viewports.
aspect-ratio matches an exact ratio. min-aspect-ratio matches when the viewport is equal to or wider than the given ratio. max-aspect-ratio matches when the viewport is equal to or taller/narrower than the given ratio.
The media feature checks the viewport shape for conditional layout rules. The aspect-ratio property sets the width-to-height ratio of an individual element, such as a video or image container.
Popular ratios include 16/9 (widescreen), 4/3 (classic tablets), 3/2 (photos), and 1/1 (square). Use max-aspect-ratio: 1/1 for portrait-oriented layouts and min-aspect-ratio: 16/9 for cinematic widescreen sections.
Yes. aspect-ratio, min-aspect-ratio, and max-aspect-ratio are supported in all modern browsers including Chrome, Firefox, Safari, Edge, and Opera.