4.2 · Responsive components

Query a value: style queries

Restyle children based on a custom property set on a parent.

Container queries can also ask about styles. @container style(--tone: warning) matches when the nearest container’s --tone custom property is warning.

Every element is a style container by default, so a parent can set one custom property and every child restyles itself. It works like a variant prop in a component framework, but in CSS.

Before
<div class="alert alert--warning">
/* One modifier class per variant,
   repeated on every child */
.alert--warning .icon { … }
.alert--warning strong { … }
Now
:root { --tone: warning; }

@container style(--tone: warning) {
  .alert { --color: orange; }
}