Skip to content

Container Queries and Cascade Layers ​

Style a component by the space it is given rather than by the size of the window, and win a specificity argument without !important.

In this chapter: why the viewport was always the wrong input · container queries · container units · cascade layers · ordering third-party CSS · the two mistakes

💡 The Core Idea ​

Two features shipped in 2023, and each replaced a long-running workaround. Both do the same kind of thing. They let you say something once, at the top, instead of defending it in every rule underneath.

Container queries change what a component responds to. A media query asks about the window, but the component does not live in the window. It lives in a sidebar, a modal or a grid cell. Cascade layers change how the browser resolves conflicts. Without them, whoever wrote the longer selector wins, so teams keep escalating. With them, you declare the order of your stylesheets once. Specificity (the weight of a selector) then only decides fights inside a layer.

Both features move a decision out of individual rules and into one declaration at the top of the stylesheet. That is why they are architecture rather than syntax.

How It Works ​

Container queries ​

A component queries the element it sits inside, once that element has declared itself a container.

Declaring a container and querying it:

css
/* The parent opts in. `inline-size` means "measure the width only". */
.card-slot {
  container-type: inline-size;
  container-name: card;
}

/* The card now responds to its slot, not to the window. */
@container card (min-width: 30rem) {
  .card {
    display: grid;
    grid-template-columns: 8rem 1fr;
  }
}

The same card can now sit in a 900px main column and a 280px sidebar on one page. It is correct in both. You need no prop, no JavaScript measurement, and no page-level breakpoint that knows where the component was placed.

Container query units measure against the container rather than the viewport:

UnitIs 1% of the container'sReplaces
cqw / cqhwidth / heightvw / vh inside a component
cqi / cqbinline size / block sizeThe writing-mode-safe version, and the one to prefer
cqmin / cqmaxsmaller / larger of the twoA clamp built out of viewport units

⚠️ A container cannot query itself. container-type: inline-size makes an element a query target for its descendants, not for its own rules. It also creates containment. The element no longer sizes itself from its content in the contained axis. So a container-type: size element with no explicit height collapses.

Cascade layers ​

@layer gives you an ordering that beats specificity. A rule in a later layer wins over a rule in an earlier one no matter how specific either is.

Declaring the order once, at the top:

css
/* Everything after this is resolved in this order, whatever the file order is. */
@layer reset, vendor, base, components, utilities;

@layer components {
  /* A specificity of 0,2,0 … */
  .card .title { font-size: 1.25rem; }
}

@layer utilities {
  /* … loses to a specificity of 0,1,0, because utilities is the later layer. */
  .text-sm { font-size: 0.875rem; }
}

That is what makes a utility class reliable without !important. You declare the utility layer last, so a one-class utility beats a three-class component rule by position.

The layered cascade, in the order the browser resolves it:

Where layers sit in the cascade: above specificity, below importance.

Unlayered styles win over every layer. That is deliberate. It means adding layers to an existing codebase cannot break the unlayered CSS already there. It is also the rule that surprises people.

When to Use It ​

SituationReach forWhy
A component used in slots of different widthsContainer queriesThe component stops needing to know where it was placed
Page-level layout: one column or twoA media queryThe viewport genuinely is the input
Importing a third-party stylesheet you cannot edit@layer on the importIts specificity stops competing with yours
A utility class that must always winA last-declared utilities layerRemoves the !important argument permanently
A design system consumed by other teamsBoth, plus custom propertiesThe layer order is the contract for who overrides whom

Layering an import you do not control:

css
/* Everything in the file lands in the `vendor` layer, whatever its own specificity. */
@import url("some-widget.css") layer(vendor);

Common Mistakes ​

❌ Turning every wrapper into a container. container-type creates containment, and containment has a cost and changes behaviour. Declare it on the few elements that are real layout slots, such as the grid cell, the sidebar and the modal body. Do not declare it on every div on the way down.

❌ Reaching for container-type: size. It contains both axes. The element takes its height from its own rules rather than from its content, so content that grows overflows silently. inline-size is the one you almost always want.

❌ Declaring layers in the file that uses them. If the @layer order statement lives in a component file, the order depends on which file the bundler put first. That is exactly the randomness layers exist to remove. Declare the full order once, in the entry stylesheet, before anything else.

❌ Assuming a layer beats !important. The browser resolves importance before layer order. For !important declarations the layer order is reversed, so the earliest layer wins. A reset layer using !important will beat an important utility, which is almost never what anyone expects.

🔑 Key Takeaways ​

  • A media query asks about the window. A container query asks about the space the component was actually given, which is what a reusable component needs.
  • container-type: inline-size is the safe default. size contains both axes and breaks content-driven height.
  • Cascade layers resolve conflicts by declared order, above specificity. So a one-class utility can beat a three-class component rule by design rather than by !important.
  • Unlayered CSS beats every layer, which is what makes layers safe to adopt in an existing codebase.
  • Both features move a decision from individual rules into one declaration at the top of the stylesheet.

Interview Questions ​

Q: A card component looks wrong in the sidebar but right in the main column. How do you fix it?

The card is responding to the viewport when it should be responding to its slot. Give the slot container-type: inline-size and rewrite the card's breakpoints as @container queries. People offer two other answers: a variant="compact" prop, or a page-level media query that knows the sidebar exists. Both push knowledge of the layout into the component or into the page. Both stop working the next time someone places the card somewhere new.

Q: What problem do cascade layers solve that BEM did not?

BEM keeps specificity flat by agreement. Nothing enforces it, and it does nothing about CSS you did not write. Layers turn the ordering into a declaration the browser enforces. A third-party stylesheet imported into a vendor layer cannot outrank your components, however specific its selectors are. BEM is a convention about names, and layers are a mechanism about precedence. Most codebases want both.

Q: Where do cascade layers sit relative to specificity and !important?

Origin and importance first, then layer order, then specificity within a layer, then source order. So a later layer beats an earlier one regardless of specificity, and unlayered styles beat all layers. The catch is that !important reverses the layer order, so an important declaration in the first layer wins. That is why !important inside a reset layer is a trap.

Q: When is a container query the wrong tool?

When the thing you are styling really depends on the viewport. Examples are a page-level choice between one column and two, a fixed header that changes on small screens, or anything driven by device capability rather than available space. Container queries also cannot look upwards past a container boundary. So a rule that needs to know about the whole page still belongs in a media query.

The whole book, free to read. Generated from the manuscript on every change.

Your progress

Tick each chapter in the sidebar once you have read and understood it. Your name and email keep the ticks on any device.

Your name and email are stored to save your progress. Nothing else is done with them.