Conditional rules

At-rules that apply their contents only when a condition holds: the features of the device or viewport, whether the browser supports a feature, or the size and state of an ancestor container. Below the three rules are the media features and container features they can test. Layers and scoping are under Cascade & scope; everything else is under Other rules.

Conditional group rules

@media

Applies the rules inside it when a media query matches. A query is an optional media type (all, screen or print) and any number of feature tests joined with and, or and not. A comma-separated list matches when any one query in it matches.

Syntax@media <media-query-list> { <rule-list> }
SupportChrome 1 · Firefox 1 · Safari 3
Speccss-conditional-3
FeatureSupport
Range syntax, e.g. (48rem <= width < 90rem)Chrome 104 · Firefox 102 · Safari 16.4
or between feature testsChrome 104 · Firefox 64 · Safari 16.4
calc() in feature valuesChrome 66 · Firefox 59 · Safari 12
@media (width >= 48rem) and (prefers-color-scheme: dark) {
  body { background: #111; }
}
Wide viewports that also prefer a dark colour scheme.

Before range syntax, range features were tested with min- and max- prefixes: (min-width: 48rem) is the same as (width >= 48rem). Both forms still work.

@supports

A feature query: applies the rules inside it only if the browser understands a declaration, selector or other feature. Tests combine with and, or and not, and a browser that does not recognise a test treats it as false.

Syntax@supports <supports-condition> { <rule-list> }
SupportChrome 28 · Firefox 22 · Safari 9
Speccss-conditional-3
TestTrue when the browser supportsSupport
(property: value)That declarationChrome 28 · Firefox 22 · Safari 9
selector(<complex-selector>)That selectorChrome 83 · Firefox 69 · Safari 14.1
font-tech(<font-tech>)A font technology, such as color-COLRv1 or variationsChrome 108 · Firefox 106 · Safari 17
font-format(<font-format>)A font format, such as woff2Chrome 108 · Firefox 106 · Safari 17
at-rule(<at-keyword-token>)An at-rule, such as at-rule(@starting-style)Chrome 148 · Firefox 157 · Safari no
named-feature(<ident>)A browser behaviour that has no syntax of its own to test, named by an identifierChrome 150 · Firefox no · Safari no
@supports (display: grid) {
  .layout { display: grid; }
}

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

Pair it with not to write a fallback: @supports not (aspect-ratio: 1).

@container

A container query: styles an element by the size or state of its nearest query container rather than the viewport. Mark an ancestor as a container with container-type, and optionally name it with container-name so a query can target it.

Syntax@container <container-condition># { <rule-list> }
SupportChrome 105 · Firefox 110 · Safari 16
Speccss-conditional-5
FeatureSupport
Size queriesChrome 105 · Firefox 110 · Safari 16
style() queries on custom propertiesChrome 111 · Firefox 151 · Safari 18
Range syntax inside style()Chrome 142 · Firefox 151 · Safari no
scroll-state() queriesChrome 133 · Firefox no · Safari no
anchored() queriesChrome 143 · Firefox no · Safari no
A container name with no query, e.g. @container card { … }Chrome 148 · Firefox 149 · Safari 26.4
/* mark the container */
.card { container-type: inline-size; container-name: card; }

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

An element is never its own query container, so the rules inside a query style the container's descendants. Container query length units resolve against the query container: cqw, cqh, cqi (inline), cqb (block), cqmin and cqmax.

Media features

The tests used inside @media (and in the media attribute and matchMedia()). A range feature compares a value, so it accepts range syntax such as (width >= 40rem) and the min- and max- prefixes. A discrete feature matches one of a set of keywords. Any feature written on its own, such as (hover), is true unless its value would be 0, none or no-preference.

any-hover

Whether any of the available input devices can hover over elements. Use hover to ask about the primary input only.

TypeDiscrete
Valuesnone | hover
SupportChrome 41 · Firefox 64 · Safari 9
Specmediaqueries-5

any-pointer

Whether any of the available input devices is a pointer, and how accurate it is. With a mouse and a touchscreen both present, (any-pointer: fine) and (any-pointer: coarse) both match.

TypeDiscrete
Valuesnone | coarse | fine
SupportChrome 41 · Firefox 64 · Safari 9
Specmediaqueries-5

aspect-ratio

The width-to-height ratio of the viewport, written as a ratio such as 16 / 9.

TypeRange
Values<ratio>
SupportChrome 3 · Firefox 3.5 · Safari 5
Specmediaqueries-5

color

The number of bits per colour component of the output device, or 0 if it cannot show colour. (color) on its own matches any colour device.

TypeRange
Values<integer>
SupportChrome 1 · Firefox 2 · Safari 3
Specmediaqueries-5

color-gamut

The approximate range of colours the browser and output device can show. Each value matches when the display covers at least that gamut, so a P3 display matches both srgb and p3.

TypeDiscrete
Valuessrgb | p3 | rec2020
SupportChrome 58 · Firefox 110 · Safari 10
Specmediaqueries-5

color-index

The number of entries in the output device's colour lookup table, or 0 if it does not use one.

TypeRange
Values<integer>
SupportChrome 29 · Firefox no · Safari 8
Specmediaqueries-5

device-aspect-ratio Deprecated

The width-to-height ratio of the whole output device rather than the viewport. Use aspect-ratio instead.

TypeRange
Values<ratio>
SupportChrome 1 · Firefox 2 · Safari 3
Specmediaqueries-5

device-height Deprecated

The height of the output device's whole rendering surface rather than the viewport. Use height instead.

TypeRange
Values<length>
SupportChrome 1 · Firefox 2 · Safari 3
Specmediaqueries-5

device-posture

The posture of a foldable device: continuous when it is flat or not foldable, folded when it is bent like a book or a laptop.

TypeDiscrete
Valuescontinuous | folded
SupportChrome 132 · Firefox no · Safari no
Specdevice-posture

device-width Deprecated

The width of the output device's whole rendering surface rather than the viewport. Use width instead.

TypeRange
Values<length>
SupportChrome 1 · Firefox 2 · Safari 3
Specmediaqueries-5

display-mode

How an installed web app is being shown, such as in a normal browser tab, in its own window (standalone) or full screen. Most of the values correspond to the display member of the web app manifest.

TypeDiscrete
Valuesapplication | fullscreen | standalone | minimal-ui | browser | picture-in-picture
SupportChrome 42 · Firefox 47 · Safari 13
Specmediaqueries-5

dynamic-range

Whether the browser and output device can show high dynamic range: high peak brightness, contrast and colour depth.

TypeDiscrete
Valuesstandard | high
SupportChrome 98 · Firefox 100 · Safari 13.1
Specmediaqueries-5

forced-colors

Whether the browser is replacing page colours with a limited palette chosen by the user, as in Windows contrast themes. Use it to repair details that forced colours remove, not to undo the user's choice.

TypeDiscrete
Valuesnone | active
SupportChrome 89 · Firefox 89 · Safari 16
Specmediaqueries-5

grid

Whether the output device is grid-based, such as a text terminal, rather than bitmap-based. <mq-boolean> is the integer 0 or 1.

TypeDiscrete
Values<mq-boolean>
SupportChrome 1 · Firefox 2 · Safari 3
Specmediaqueries-5

height

The height of the viewport, or of the page box when printing.

TypeRange
Values<length>
SupportChrome 1 · Firefox 2 · Safari 3
Specmediaqueries-5

horizontal-viewport-segments

The number of side-by-side segments the viewport is split into, as on a dual-screen or folding device. An ordinary screen has one.

TypeRange
Values<integer>
SupportChrome 138 · Firefox no · Safari no
Specmediaqueries-5

hover

Whether the primary input device can hover over elements. A mouse can; a touchscreen usually cannot.

TypeDiscrete
Valuesnone | hover
SupportChrome 38 · Firefox 64 · Safari 9
Specmediaqueries-5
@media (hover: hover) {
  .card:hover { translate: 0 -2px; }
}
Only add hover effects where the user can actually hover.

inverted-colors

Whether the operating system is inverting all colours on screen, so that images and video can be inverted back.

TypeDiscrete
Valuesnone | inverted
SupportChrome no · Firefox no · Safari 9.1
Specmediaqueries-5

monochrome

The number of bits per pixel on a monochrome (greyscale) device, or 0 if the device is not monochrome.

TypeRange
Values<integer>
SupportChrome 1 · Firefox 2 · Safari 3
Specmediaqueries-5

orientation

portrait when the viewport is at least as tall as it is wide, otherwise landscape.

TypeDiscrete
Valuesportrait | landscape
SupportChrome 3 · Firefox 2 · Safari 5
Specmediaqueries-5

overflow-block

How the device handles content that overflows the viewport in the block axis: not at all, by scrolling, or by breaking it into pages as in print.

TypeDiscrete
Valuesnone | scroll | paged
SupportChrome 113 · Firefox 66 · Safari 17
Specmediaqueries-5

overflow-inline

Whether content that overflows the viewport in the inline axis can be scrolled into view.

TypeDiscrete
Valuesnone | scroll
SupportChrome 113 · Firefox 66 · Safari 17
Specmediaqueries-5

pointer

Whether the primary input device is a pointer and how accurate it is: fine for a mouse or stylus, coarse for a finger on a touchscreen. Use it to give coarse pointers larger targets.

TypeDiscrete
Valuesnone | coarse | fine
SupportChrome 41 · Firefox 64 · Safari 9
Specmediaqueries-5

prefers-color-scheme

Whether the user has asked for a light or a dark colour scheme in their system or browser settings.

TypeDiscrete
Valueslight | dark
SupportChrome 76 · Firefox 67 · Safari 12.1
Specmediaqueries-5
@media (prefers-color-scheme: dark) {
  :root { --surface: #111; --text: #eee; }
}
Swap design tokens for users who prefer dark mode.

prefers-contrast

Whether the user has asked for more or less contrast between colours. custom matches when forced colours are active with a palette that is neither high nor low contrast.

TypeDiscrete
Valuesno-preference | less | more | custom
SupportChrome 96 · Firefox 101 · Safari 14.1
Specmediaqueries-5

prefers-reduced-motion

Whether the user has asked the system to minimise non-essential motion. When it is reduce, tone down or remove large movements, parallax and autoplaying animation.

TypeDiscrete
Valuesno-preference | reduce
SupportChrome 74 · Firefox 63 · Safari 10.1
Specmediaqueries-5
@media (prefers-reduced-motion: no-preference) {
  .hero { animation: slide-in 0.6s ease-out; }
}
Add the motion only for users who have not asked to reduce it.

prefers-reduced-transparency

Whether the user has asked the system to reduce transparent and translucent layers, such as frosted-glass panels.

TypeDiscrete
Valuesno-preference | reduce
SupportChrome 118 · Firefox no · Safari no
Specmediaqueries-5

resolution

The pixel density of the output device, such as 2dppx or 192dpi. infinite matches devices with no physical pixel limit, such as vector displays.

TypeRange
Values<resolution> | infinite
SupportChrome 29 · Firefox 8 · Safari 16
Specmediaqueries-5

scan

Whether the display draws its image interlaced or progressively. Almost every screen in use today is progressive.

TypeDiscrete
Valuesinterlace | progressive
SupportChrome 104 · Firefox 63 · Safari 16.4
Specmediaqueries-5

scripting

Whether scripting such as JavaScript is available: enabled, none, or initial-only when scripts run only while the page loads, as in printed output.

TypeDiscrete
Valuesnone | initial-only | enabled
SupportChrome 120 · Firefox 113 · Safari 17
Specmediaqueries-5

update

How quickly the device can change what it shows: none for paper, slow for screens such as e-ink, fast for ordinary screens.

TypeDiscrete
Valuesnone | slow | fast
SupportChrome 113 · Firefox 102 · Safari 17
Specmediaqueries-5

vertical-viewport-segments

The number of stacked segments the viewport is split into, top to bottom, as on a folding device held with the fold horizontal. An ordinary screen has one.

TypeRange
Values<integer>
SupportChrome 138 · Firefox no · Safari no
Specmediaqueries-5

video-dynamic-range

Like dynamic-range, but for the plane that video is drawn on, which on some devices supports high dynamic range when the rest of the page does not.

TypeDiscrete
Valuesstandard | high
SupportChrome no · Firefox 100 · Safari no
Specmediaqueries-5

width

The width of the viewport including any scrollbar, or of the page box when printing. The most common media feature for responsive layout.

TypeRange
Values<length>
SupportChrome 1 · Firefox 2 · Safari 3
Specmediaqueries-5

em and rem in a media query are relative to the browser's initial font size, not to any font size set in the page.

Container features

The tests used inside @container. Size features are written in parentheses like media features and need an ancestor with a container-type of size or inline-size. State tests are wrapped in a function: style(), scroll-state() or anchored().

width, height, inline-size, block-size

The size of the query container's content box in one axis. A container with container-type: inline-size answers only queries in its inline axis (inline-size, and width in horizontal writing modes); queries in the block axis need container-type: size.

TypeRange
Values<length>
SupportChrome 105 · Firefox 110 · Safari 16
Speccss-conditional-5

aspect-ratio

The width-to-height ratio of the query container's content box. It needs a container with container-type: size.

TypeRange
Values<ratio>
SupportChrome 105 · Firefox 110 · Safari 16
Speccss-conditional-5

orientation

portrait when the query container is at least as tall as it is wide, otherwise landscape. It needs a container with container-type: size.

TypeDiscrete
Valuesportrait | landscape
SupportChrome 105 · Firefox 110 · Safari 16
Speccss-conditional-5

style()

Tests the computed value of a custom property on the container. Every element is a style container, so no container-type is needed; the query looks at the nearest ancestor, or the nearest one with the given container-name. Browsers support custom properties only, not ordinary properties.

Syntaxstyle( <style-query> )
SupportChrome 111 · Firefox 151 · Safari 18
Range syntax: Chrome 142 · Firefox 151 · Safari no
Speccss-conditional-5
.card { --variant: featured; }

@container style(--variant: featured) {
  .card-title { color: crimson; }
}
Restyle descendants from a custom property set on an ancestor.

A bare name such as style(--variant) is true when the property has any value other than its initial value. With range syntax a query can compare values, as in style(--columns > 2).

scroll-state()

Tests the scrolling state of a container that has container-type: scroll-state, using the features stuck, snapped, scrollable and scrolled below. The rules inside style the container's descendants.

Syntaxscroll-state( <scroll-state-query> )
SupportChrome 133 · Firefox no · Safari no
Speccss-conditional-5
header { position: sticky; top: 0; container-type: scroll-state; }

@container scroll-state(stuck: top) {
  header nav { box-shadow: 0 2px 8px rgb(0 0 0 / 0.2); }
}
Add a shadow only while the sticky header is stuck to the top.

stuck

Inside scroll-state(): whether a sticky-positioned container is currently stuck against the given edge of its scroll container.

TypeDiscrete
Valuesnone | top | right | bottom | left | block-start | inline-start | block-end | inline-end
SupportChrome 133 · Firefox no · Safari no
Speccss-conditional-5

snapped

Inside scroll-state(): whether the container is the element its scroll container is currently snapped to, in the given axis.

TypeDiscrete
Valuesnone | x | y | block | inline | both
SupportChrome 133 · Firefox no · Safari no
Speccss-conditional-5

scrollable

Inside scroll-state(): whether the container, as a scroll container, can be scrolled further in the given direction. Use it to show scroll hints or arrows only when there is more to see.

TypeDiscrete
Valuesnone | top | right | bottom | left | block-start | inline-start | block-end | inline-end | x | y | block | inline
SupportChrome 133 · Firefox no · Safari no
Speccss-conditional-5

scrolled

Inside scroll-state(): the direction the container was most recently scrolled in, for example to hide a header while the user scrolls down.

TypeDiscrete
Valuesnone | top | right | bottom | left | block-start | inline-start | block-end | inline-end | x | y | block | inline
SupportChrome 144 · Firefox no · Safari no
Speccss-conditional-5

anchored()

Tests the state of an anchor-positioned element that has container-type: anchored. Its fallback feature matches the position option currently in use, so descendants can adapt when the browser flips the element to another side of its anchor.

TypeDiscrete
Valuesfallback: none | [ [<dashed-ident> || <try-tactic>] | <position-area-query> ]
SupportChrome 143 · Firefox no · Safari no
Speccss-anchor-position-2

A <try-tactic> is one or more of flip-block, flip-inline, flip-start, flip-x and flip-y, as used in position-try-fallbacks.