← Tous les articles

CSS container queries: truly responsive components

Media queries look at the screen; container queries look at the space a component actually has. A small change that greatly simplifies WordPress CSS.

CSS container queries: truly responsive components

Key points

  • A container query adapts a component to its parent’s width, not the screen’s.
  • The same card works in a grid, a sidebar or a full-width block.
  • Browser support is now good enough for production.

On a WordPress site, the same post card may appear in the blog grid, a sidebar and an editor block. With media queries you write exceptions for each location. Container queries fix this at the root.

The idea in two lines

Declare an element as a container, then write rules based on its width:

.cards { container-type: inline-size; }

@container (min-width: 520px) {
  .card { display: grid; grid-template-columns: 200px 1fr; }
}

The card switches to a horizontal layout as soon as its container is wider than 520px, whatever the screen size.

Why it matters on WordPress

  • Gutenberg blocks end up anywhere: columns, groups, full width. A block built on container queries adapts on its own.
  • Fewer location-specific selectors like .sidebar .card or .home .card: shorter, more predictable CSS.
  • cqi units also give type that scales with the container: font-size: clamp(1rem, 4cqi, 1.6rem).

Limits to keep in mind

A container cannot depend on its own size: container-type: inline-size stops the element from growing with its content. Apply it to the grid’s parent, not to the cards.

For very old browsers, keep a simple one-column layout as the default: it is progressive enhancement, not a dependency.

FAQ

Should I drop media queries?

No. They remain the right tool for the overall layout (menu, main columns). Container queries are for components.

Does this work with existing themes?

Yes, you can add them gradually, one component at a time, without rewriting all the CSS.

Our custom themes use these techniques to stay light and easy to evolve.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

Agence web : sites WordPress sur mesure, thèmes premium et logiciels SaaS.

Parler de votre projet →