CSS aspect-ratio Media Feature

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 4 Examples
Media Queries

What You’ll Learn

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.

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.

📝 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).

Basic Example

aspect-ratio-basic.css
body {
  background-color: #dbeafe;
}

@media (aspect-ratio: 16/9) {
  body {
    background-color: #bbf7d0;
  }
}

Media Feature vs CSS Property

@media (aspect-ratio: 16/9) Checks the viewport shape. Use for page-level layout changes when the window is widescreen or portrait.
aspect-ratio: 16 / 9; Sets an element’s box ratio. Use on videos, images, and cards to maintain proportions regardless of viewport.

Default Value

There is no default value. The browser evaluates the current viewport dimensions at runtime.

Syntax Rules

  • Write ratios as two integers with a slash: 16/9, not 16:9 in the media query.
  • Spaces around the slash are optional but the slash is required.
  • Combine with width queries: @media (min-width: 48rem) and (min-aspect-ratio: 16/9).
  • Exact aspect-ratio matches are rare; prefer min- and max- ranges.
  • Resize the browser window to test — DevTools device mode helps simulate ratios.

Related Topics

@media (aspect-ratio: 16/9) @media (min-aspect-ratio: 4/3) @media (max-aspect-ratio: 1/1)

⚡ Quick Reference

QuestionAnswer
Feature nameaspect-ratio, min-aspect-ratio, max-aspect-ratio
Ratio formatwidth/height (e.g. 16/9)
What it detectsViewport width-to-height proportion
Portrait layouts@media (max-aspect-ratio: 1/1)
Widescreen layouts@media (min-aspect-ratio: 16/9)
Browser supportAll modern browsers

When to Use aspect-ratio

Reach for aspect-ratio media queries when viewport shape matters:

  • Portrait vs landscape — Stack content vertically on tall phone screens; use side-by-side columns on widescreen windows.
  • Hero sections — Show cinematic full-width heroes only when min-aspect-ratio: 16/9 matches.
  • Video players — Adjust padding and controls when the viewport is wider or taller than 16:9.
  • Dashboard layouts — Reveal extra panels on ultrawide monitors with high aspect ratios.
  • Combined with width — Fine-tune tablet layouts where width alone is ambiguous.

👀 Live Preview

Resize your browser window to see this box react to viewport shape — blue on widescreen (16:9+), amber on portrait (taller than wide):

Viewport Shape Demo Detected by min-aspect-ratio / max-aspect-ratio Exact 4:3 match!

Examples Gallery

Practice aspect-ratio with backgrounds, portrait layouts, hero sections, and combined media queries.

📜 Core Patterns

Apply styles based on viewport width-to-height ratio.

Example 1 — Background at exact 16:9

Change the page background when the viewport matches a 16:9 widescreen ratio:

aspect-ratio-16-9.css
body {
  background-color: #dbeafe;
  font-family: system-ui, sans-serif;
  padding: 2rem;
}

@media (aspect-ratio: 16/9) {
  body {
    background-color: #bbf7d0;
  }
}
Try It Yourself

How It Works

Exact matches are precise — resize the window until width ÷ height equals 16 ÷ 9 to trigger the rule.

Example 2 — Portrait layout with max-aspect-ratio

Stack a two-column grid vertically when the viewport is square or taller than wide:

aspect-ratio-portrait.css
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

@media (max-aspect-ratio: 1/1) {
  .grid {
    grid-template-columns: 1fr;
  }
}
Try It Yourself

How It Works

max-aspect-ratio: 1/1 matches when height is greater than or equal to width — typical phone portrait orientation.

Example 3 — Cinematic hero on widescreen

Show a tall hero banner only when the viewport is at least 16:9 wide:

aspect-ratio-hero.css
.hero {
  padding: 2rem 1.5rem;
  background: #334155;
  color: #fff;
  text-align: center;
}

@media (min-aspect-ratio: 16/9) {
  .hero {
    padding: 4rem 2rem;
    background: linear-gradient(135deg, #1e3a8a, #2563eb);
  }
  .hero h1 {
    font-size: 2.5rem;
  }
}
Try It Yourself

How It Works

min-aspect-ratio: 16/9 adds cinematic padding and typography when the window is widescreen or wider.

Example 4 — Combine aspect-ratio with min-width

Apply a three-column layout only on large, widescreen viewports:

aspect-ratio-combined.css
.cards {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

@media (min-width: 48rem) and (min-aspect-ratio: 4/3) {
  .cards {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-aspect-ratio: 4/3) {
  body {
    background-color: #fdf2f8;
  }
}
Try It Yourself

How It Works

Combining min-width and min-aspect-ratio avoids three columns on narrow-but-wide windows or tall tablets.

💬 Usage Tips

  • Prefer ranges — Use min-aspect-ratio and max-aspect-ratio instead of exact ratios for reliable matches.
  • Portrait phonesmax-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.

⚠️ Common Pitfalls

  • Exact ratio fragilityaspect-ratio: 16/9 matches only precisely; one pixel off and styles won’t apply.
  • Confusing with orientationorientation: 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.

♿ 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.

🖥 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 feature 97% supported

Bottom line: aspect-ratio media queries are safe for responsive layout patterns. Test by resizing the browser on real devices.

🎉 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.

💡 Best Practices

✅ Do

  • Use min- and max-aspect-ratio ranges
  • Combine with min-width breakpoints
  • Test portrait and landscape
  • Stack columns on tall viewports
  • Link to aspect-ratio property for elements

❌ Don’t

  • Rely on exact ratio matches alone
  • Confuse media feature with property
  • Replace all width queries with ratio
  • Hide critical content at one ratio
  • Forget ultrawide edge cases

Key Takeaways

Knowledge Unlocked

Five things to remember about aspect-ratio

Use these points when adapting layouts to viewport shape.

5
Core concepts
16:9 02

16/9

Widescreen.

Ratio
min 03

min-/max-

Ranges.

Syntax
1:1 04

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.

Practice in the Live Editor

Open the HTML editor and experiment with @media (min-aspect-ratio: 16/9) and portrait stacking patterns.

HTML Editor →

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.

5 people found this page helpful