Conditional rules

At-rules that apply their contents only when a condition holds — the size or features of the device, whether the browser supports a feature, or the size of an ancestor container.

@media

Applies styles when a media query matches: an optional media type ( all , screen , print ) and any number of feature tests joined with and , or , not and commas (comma means or).

@media (min-width: 48rem) and (prefers-color-scheme: dark) {
  body { background: #111; }
}
Wide viewports that also prefer a dark colour scheme.

Common media features:

FeatureTests
width / heightViewport size. Prefix min- / max-, or use range syntax.
orientationportrait or landscape.
aspect-ratioThe viewport's width-to-height ratio.
resolutionPixel density, e.g. min-resolution: 2dppx.
prefers-color-schemelight or dark.
prefers-reduced-motionreduce when the user limits animation.
hoverhover if the primary pointer can hover.
pointerfine, coarse or none.

Range syntax reads naturally: @media (48rem <= width < 90rem) .

@supports

A feature query: applies styles only if the browser understands a given declaration or selector. Combine tests with and , or and not .

@supports (display: grid) {
  .layout { display: grid; }
}

@supports selector(:has(*)) {
  /* :has() is available */
}
Test a property/value pair, or a selector with selector().

Pair it with not to provide fallbacks: @supports not (aspect-ratio: 1) .

@container

A container query: styles an element by the size of its nearest query container rather than the viewport. First mark an ancestor as a container with container-type (and optionally container-name ).

/* mark the container */
.card { container-type: inline-size; container-name: card; }

/* query it */
@container card (min-width: 30rem) {
  .card h3 { font-size: 1.5rem; }
}
The heading grows when the card itself — not the viewport — is wide enough.

Container query length units resolve against the query container: cqw , cqh , cqi (inline), cqb (block), cqmin and cqmax .