Course contentsShow
← Web Development
Lesson 396 of 2,9827. Responsive DesignFree lesson

Aspect Ratio Media Features

Use aspect-ratio, min-aspect-ratio, and max-aspect-ratio to target specific viewport proportions.

Aspect Ratio Media Features

You'll learn how to target specific viewport shapes using aspect ratio media queries.

What Are Aspect Ratio Media Features?

After learning about width, height, and orientation, you can also target the shape of the viewport itself. The aspect ratio describes the proportional relationship between width and height (like 16:9 for widescreen or 4:3 for older displays).

CSS provides three media features for this:

  • aspect-ratio: Targets an exact ratio
  • min-aspect-ratio: Applies when viewport is at least this wide relative to height
  • max-aspect-ratio: Applies when viewport is at most this wide relative to height

Think of it like describing TV screen shapes: a widescreen monitor (16:9) is much more horizontal than a portrait phone (9:16).

How They Work

These features use a fraction-like syntax: width / height.

/* Targets exactly 16:9 viewports (widescreen) */
@media (aspect-ratio: 16/9) {
  .hero {
    background-position: center;
  }
}

/* Targets viewports wider than 16:9 (ultrawide) */
@media (min-aspect-ratio: 16/9) {
  .sidebar {
    display: flex;
  }
}

/* Targets narrower viewports (portrait-ish) */
@media (max-aspect-ratio: 1/1) {
  .grid {
    grid-template-columns: 1fr;
  }
}

You can combine these with other media features using logical operators (and, or) for precise control.

When to Use Them

Aspect ratio queries shine when your layout needs differ based on viewport shape rather than just size—like optimizing video players, image galleries, or dashboard layouts for ultrawide monitors versus square tablets.

Key Takeaway: Aspect ratio media features let you style based on viewport proportions, giving you control over how layouts adapt to different screen shapes, not just sizes.