Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

@supports — conditional CSS rules

The @supports rule activates nested styles only when the engine satisfies the stated support condition.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

@supports <supports-condition> { <block-contents> }

@supports evaluates a support condition before the block can participate in the cascade. The declaration test (display: grid) is true when the engine accepts that property–value pair, while selector(...) checks support for selector syntax. Conditions can be negated and combined with not, and, and or; and and or cannot be mixed at one level without parentheses.

The example establishes --display-check and --selector-check fallback values. The true grid test changes the first to yes; the deliberately unknown selector does not activate its branch, so the second remains no. Reading both CSSOM conditions and computed custom properties demonstrates the cascade effect rather than only querying a helper API.

A successful syntax test does not prove that a feature behaves suitably in every case or produces an accessible result. In particular, a custom-property declaration accepts almost any token stream, so it is not a reliable test for a specific feature. An unknown or invalid condition evaluates false and its block has no styling effect.

Examples

const style = document.createElement('style');
style.textContent = `.probe { --display-check: no; --selector-check: no; }
@supports (display: grid) { .probe { --display-check: yes; } }
@supports selector(:definitely-unknown) { .probe { --selector-check: yes; } }`;
document.head.append(style);
const probe = document.createElement('div');
probe.className = 'probe';
document.body.append(probe);
const computed = getComputedStyle(probe);
const trueRule = style.sheet.cssRules[1];
const falseRule = style.sheet.cssRules[2];
console.log('true=' + trueRule.conditionText + ':' + computed.getPropertyValue('--display-check').trim() + '; false=' + falseRule.conditionText + ':' + computed.getPropertyValue('--selector-check').trim());

Result

true=(display: grid):yes; false=selector(:definitely-unknown):no

The @supports rule activates nested styles only when the engine satisfies the stated support condition.

Sources