AdvancedLesson 26 of 306 min read

Container queries and modern layout features

Build components that adapt to their own space with container queries, plus scroll snap, logical properties and @supports.

In this lesson you will

  • Declare containers and write @container rules
  • Choose between container and media queries
  • Build a snap carousel
  • Use @supports for safe enhancement

About 16 minutes

Components that know their own size

Here is a puzzle that media queries cannot solve. You build a card. In the main column of a page it is wide, so it shows an image beside the text. The very same card, placed in a narrow sidebar, should stack the image on top. But a media query looks at the screen width, and the screen is just as wide in both places. The card needs to respond to the space it has, not the size of the window. Container queries do exactly that, and they are one of the biggest changes to CSS in years.

Step 1: declare a container

.card-wrap {
  container-type: inline-size;
  container-name: card;
}

container-type: inline-size tells the browser to track the width of this element so children can ask about it. The name is optional and helps you target a particular container.

Step 2: query the container

.card { display: grid; gap: 12px; }

@container card (min-width: 420px) {
  .card { grid-template-columns: 160px 1fr; align-items: center; }
}

Inside @container, conditions refer to the nearest matching ancestor container, not the window. The same .card markup now adapts anywhere you place it: in a wide column it shows the horizontal layout, in a narrow one it stays stacked, with no change to the HTML.

The same card sits in two containers: one wide, one narrow. Add the container query so the wide one shows image and text side by side while the narrow one stays stacked.

<style>
 body { font-family:Arial; padding:12px; background:#F4F1FC; }
 .wrap { container-type: inline-size; margin-bottom:14px; background:#fff; padding:10px; border-radius:12px; }
 .wrap.narrow { width: 190px; }
 .card { display:grid; gap:8px; }
 .pic { background:#5A28C8; height:60px; border-radius:8px; }
 /* container query here */
</style>
<div class="wrap"><div class="card"><div class="pic"></div><div><b>Wide container</b><br>Text beside the picture.</div></div></div>
<div class="wrap narrow"><div class="card"><div class="pic"></div><div><b>Narrow</b><br>Stacked.</div></div></div>

@container (min-width: 300px) { .card { grid-template-columns: 80px 1fr; align-items:center; } }

<style>
 body { font-family:Arial; padding:12px; background:#F4F1FC; }
 .wrap { container-type: inline-size; margin-bottom:14px; background:#fff; padding:10px; border-radius:12px; }
 .wrap.narrow { width: 190px; }
 .card { display:grid; gap:8px; }
 .pic { background:#5A28C8; height:60px; border-radius:8px; }
 @container (min-width: 300px) { .card { grid-template-columns: 80px 1fr; align-items:center; } }
</style>
<div class="wrap"><div class="card"><div class="pic"></div><div><b>Wide container</b><br>Text beside the picture.</div></div></div>
<div class="wrap narrow"><div class="card"><div class="pic"></div><div><b>Narrow</b><br>Stacked.</div></div></div>

Container units

.card h3 { font-size: clamp(1rem, 5cqi, 1.6rem); }

Units such as cqi (1 percent of the container’s inline size), cqw and cqh work like viewport units, but relative to the container. A heading can scale with the card, not with the window.

Container queries versus media queries

  • Use media queries for page-level decisions: the overall layout, the navigation, global spacing.
  • Use container queries for components that appear in several places: cards, widgets, pricing boxes, comment threads.
  • They work together. A page layout may change at a media breakpoint, and the card inside adapts to whatever space it ends up with.

Style queries and other new tools

Container style queries let a component react to a custom property of its container, for example switching to a compact look when --density: compact is set. Support is newer, so treat them as an enhancement. A few other modern layout features deserve a quick mention:

  • aspect-ratio keeps boxes in shape without the old padding hack.
  • gap now works in flexbox, grid and multi-column layouts.
  • Subgrid lets the contents of nested cards line up with the parent grid, so titles, texts and buttons align across a row.
  • Logical properties such as margin-inline, padding-block and inset-inline-start replace left and right, so layouts flip correctly for right-to-left languages.
  • scroll-snap makes carousels that stop neatly at each item with no JavaScript.
  • position: sticky and scroll-margin-top keep anchored headings clear of a fixed header.
.carousel {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: min(80%, 320px);
  gap: 16px;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
}
.carousel > * { scroll-snap-align: start; }

h2[id] { scroll-margin-top: 90px; }

Turn the row into a scroll-snap carousel: make it a horizontally scrolling flex row where each card snaps to the start.

<style>
 body { font-family:Arial; padding:12px; }
 .car { display:flex; gap:12px; /* scroll + snap */ }
 .car div { flex:0 0 70%; background:#EFEAFD; padding:30px 12px; border-radius:12px; text-align:center; /* snap align */ }
</style>
<div class="car"><div>One</div><div>Two</div><div>Three</div><div>Four</div></div>

.car{overflow-x:auto; scroll-snap-type:x mandatory} .car div{scroll-snap-align:start}

<style>
 body { font-family:Arial; padding:12px; }
 .car { display:flex; gap:12px; overflow-x:auto; scroll-snap-type:x mandatory; }
 .car div { flex:0 0 70%; background:#EFEAFD; padding:30px 12px; border-radius:12px; text-align:center; scroll-snap-align:start; }
</style>
<div class="car"><div>One</div><div>Two</div><div>Three</div><div>Four</div></div>

Feature queries with @supports

.cards { display: flex; flex-wrap: wrap; gap: 16px; }

@supports (display: grid) {
  .cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
}

@supports applies a block only if the browser understands the feature in the brackets. It lets you write a safe baseline and then enhance. This is called progressive enhancement, and it is how professionals use new CSS without leaving anyone behind.

Logical properties in practice

.note {
  padding-inline: 1rem;       /* left and right in English; flips in Urdu or Arabic */
  padding-block: .75rem;
  margin-inline-start: auto;
  border-inline-start: 4px solid #5A28C8;
  text-align: start;
}

If you plan to publish in Urdu, Arabic or Hebrew, logical properties save you from writing a whole second stylesheet. With dir="rtl" on the page, start and end swap automatically.

Common mistake

Forgetting to set container-type. A container query without a declared container silently does nothing. If your @container rule never applies, check the parent first.

Good to know

An element cannot query itself. The container must be an ancestor of the element being styled, which is why components usually get a thin wrapper.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What problem do container queries solve?

  1. Slow loading
  2. A component adapting to the space it has, not the window
  3. Font loading
  4. Image formats

They let a component respond to its container’s size.

What must you declare on the parent?

  1. container-type: inline-size
  2. display: container
  3. position: container
  4. query: on

Without it, the container query has nothing to measure.

Which is best for page-level layout changes?

  1. Container queries
  2. Media queries
  3. Neither
  4. Floats

Media queries handle the overall page layout.

What does @supports (display: grid) do?

  1. Adds grid
  2. Applies rules only if the browser supports grid
  3. Disables grid
  4. Prints grid

Feature queries allow progressive enhancement.

Why use logical properties like margin-inline?

  1. They are shorter
  2. They flip correctly for right-to-left languages
  3. They are faster
  4. They add shadows

Start and end swap with the writing direction.

Summary

  • Container queries respond to the parent’s size.
  • Use media queries for page layout and container queries for components.
  • Logical properties support all writing directions.
  • @supports enables safe progressive enhancement.

Next lesson

Next: dark mode and theming, using the variables you learned earlier.

Questions

Are container queries supported?

Yes, size container queries work in all current major browsers.

Can a container query use height?

Only with container-type: size, which has more layout restrictions. Inline-size is the common choice.

Do I still need media queries?

Yes. They remain the right tool for page-level changes and user preferences.

Progress is saved in this browser.