> in its prelude.
For example, the following rule
@supports ( display: flex ) {
body, #navigation, #content { display: flex; }
#navigation { background: blue; color: white; }
#article { background: white; color: black; }
}
applies the rules inside the ''@supports'' rule only when
''display: flex'' is supported.
The following example shows an additional ''@supports'' rule that can
be used to provide an alternative for when ''display: flex'' is not
supported:
@supports not ( display: flex ) {
body { width: 100%; height: 100%; background: white; color: black; }
#navigation { width: 25%; }
#article { width: 75%; }
}
Note that the 'width' declarations may be harmful to the
flex-based layout, so it is important that they be present only in
the non-flex styles.
The following example checks for support for the 'box-shadow'
property, including checking for support for vendor-prefixed versions of
it. When the support is present, it specifies both 'box-shadow' (with
the prefixed versions) and 'border' in a way what would cause the box to
become invisible were 'box-shadow' not supported.
.noticebox {
border: 1px solid black;
padding: 1px;
}
@supports ( box-shadow: 0 0 2px black inset ) or
( -moz-box-shadow: 0 0 2px black inset ) or
( -webkit-box-shadow: 0 0 2px black inset ) or
( -o-box-shadow: 0 0 2px black inset ) {
.noticebox {
-moz-box-shadow: 0 0 2px black inset;
-webkit-box-shadow: 0 0 2px black inset;
-o-box-shadow: 0 0 2px black inset;
box-shadow: 0 0 2px black inset; /* unprefixed last */
/* override the rule above the @supports rule */
border: none;
padding: 2px;
}
}
To avoid confusion between and and or, the syntax requires
that both and and or be specified explicitly (rather than, say,
using commas or spaces for one of them). Likewise, to avoid confusion
caused by precedence rules, the syntax does not allow and, or,
and not operators to be mixed without a layer of parentheses.
For example, the following rule is not valid:
@supports (transition-property: color) or
(animation-name: foo) and
(transform: rotate(10deg)) {
/* ... */
}
Instead, authors must write one of the following:
@supports ((transition-property: color) or
(animation-name: foo)) and
(transform: rotate(10deg)) {
/* ... */
}
@supports (transition-property: color) or
((animation-name: foo) and
(transform: rotate(10deg))) {
/* ... */
}
at-supports-016.html
css-supports-013.xht
css-supports-014.xht
The declaration being tested must always occur within parentheses,
when it is the only thing in the expression.
For example, the following rule is not valid:
@supports display: flex {
/* ... */
}
Instead, authors must write:
@supports (display: flex) {
/* ... */
}
at-supports-011.html
at-supports-034.html
at-supports-035.html
at-supports-036.html
at-supports-037.html
css-supports-002.xht
The syntax allows extra parentheses when they are not needed. This
flexibility is sometimes useful for authors (for example, when
commenting out parts of an expression) and may also be useful for
authoring tools.
For example, authors may write:
@supports ((display: flex)) {
/* ... */
}
at-supports-006.html
css-supports-003.xht
A trailing ''!important'' on a declaration being tested is allowed,
though it won't change the validity of the declaration.
For example, the following rule is valid:
@supports (display: flex !important) {
/* ... */
}
at-supports-007.html
css-supports-004.xht
Definition of support
For forward-compatibility,
section 4.1.8
(Declarations and properties) of [[!CSS21]]
defines rules for handling invalid properties and values.
CSS processors that
do not implement or partially implement a specification
must treat any part of a value that they
do not implement, or
do not have a usable level of support for,
as invalid according to this rule
for handling invalid properties and values,
and therefore must discard the declaration as a parse error.
A CSS processor is considered to support
a declaration (consisting of a property and value) if it accepts that
declaration (rather than discarding it as a parse error)
within a [=style rule=].
If a processor does not implement, with a usable level of support,
both the property and the value given,
then it must not
accept the declaration or claim support for it.
Note: Note that properties or values
whose support is effectively disabled by user preferences
are still considered as supported by this definition.
For example, if a user has enabled a high-contrast mode
that causes colors to be overridden,
the CSS processor is still considered to support the 'color' property
even though declarations of the 'color' property may have no effect.
On the other hand, a developer-facing preference
whose purpose is to enable or disable support for an experimental CSS feature
does affect this definition of support.
These rules (and the equivalence between them) allow
authors to use fallback (either in the [[CSS1]] sense of declarations
that are overridden by later declarations or with the new capabilities
provided by the ''@supports'' rule in this specification) that works
correctly for the features implemented. This applies especially to
compound values; implementations must implement all parts of the value
in order to consider the declaration supported, either inside a style rule
or in the declaration condition of an ''@supports'' rule.
css-supports-005.xht
css-supports-020.xht
css-supports-024.xht
js/CSS-supports-CSSStyleDeclaration.html
at-supports-044.html
APIs
idlharness.html
js/001.html
Extensions to the CSSRule
interface
The CSSRule
interface is extended as follows:
partial interface CSSRule {
const unsigned short SUPPORTS_RULE = 12;
};
js/conditional-CSSGroupingRule.html
The CSSConditionRule
interface
The {{CSSConditionRule}} interface represents
all the “conditional” at-rules,
which consist of a condition and a statement block.
[Exposed=Window]
interface CSSConditionRule : CSSGroupingRule {
readonly attribute CSSOMString conditionText;
};
js/conditional-CSSGroupingRule.html
conditionText
of type CSSOMString
-
The
conditionText
attribute represents
the condition of the rule.
Since what this condition does
varies between the derived interfaces of CSSConditionRule
,
those derived interfaces
may specify different behavior for this attribute
(see, for example, CSSMediaRule
below).
In the absence of such rule-specific behavior,
the following rules apply:
The conditionText
attribute, on getting, must return
the result of serializing the associated condition.
The {{CSSMediaRule}} interface represents a ''@media'' at-rule:
[Exposed=Window]
interface CSSMediaRule : CSSConditionRule {
[SameObject, PutForwards=mediaText] readonly attribute MediaList media;
readonly attribute boolean matches;
};
media
of type {{MediaList}}, readonly
- The
media
attribute must return a {{MediaList}} object
for the list of media queries specified with the ''@media'' at-rule.
matches
of type {{boolean}}, readonly
- The
matches
attribute returns true
if the rule is in an stylesheet attached to a document
whose {{Window}} matches this rule’s {{CSSMediaRule/media}} [=media query=],
and returns false otherwise.
conditionText
of type CSSOMString
(CSSMediaRule-specific definition for attribute on CSSConditionRule)
- The
conditionText
attribute (defined on the CSSConditionRule
parent rule),
on getting, must return the value of media.mediaText
on the rule.
The CSSSupportsRule
interface
The {{CSSSupportsRule}} interface represents a ''@supports'' rule.
[Exposed=Window]
interface CSSSupportsRule : CSSConditionRule {
readonly attribute boolean matches;
};
matches
of type {{boolean}}, readonly
- The
matches
attribute returns the evaluation of
the [=CSS feature query=] represented in {{CSSConditionRule/conditionText}}.
conditionText
of type CSSOMString
(CSSSupportsRule-specific definition for attribute on CSSConditionRule)
- The
conditionText
attribute (defined on the CSSConditionRule
parent rule),
on getting, must return the condition that was specified,
without any logical simplifications,
so that the returned condition will evaluate to the same result
as the specified condition
in any conformant implementation of this specification
(including implementations that implement future extensions
allowed by the <> extensibility mechanism in this specification).
In other words,
token stream simplifications are allowed
(such as reducing whitespace to a single space
or omitting it in cases where it is known to be optional),
but logical simplifications (such as removal of unneeded parentheses,
or simplification based on evaluating results) are not allowed.
The CSS
namespace, and the supports()
function
The {{CSS}} namespace holds useful CSS-related functions that do not belong elsewhere.
partial namespace CSS {
boolean supports(CSSOMString property, CSSOMString value);
boolean supports(CSSOMString conditionText);
};
supports(CSSOMString property, CSSOMString value)
, returns boolean
-
When the {{supports(property, value)}} method is invoked
with two arguments property and value:
1. If |property| is an [=ASCII case-insensitive=] match
for any defined CSS property that the UA supports,
or is a [=custom property name string=],
and |value| successfully [=CSS/parses=] according to that property's grammar,
return true
.
2. Otherwise, return false
.
Note: No CSS escape or whitespace processing is performed on the property name,
so CSS.supports(" width", "5px")
will return false
,
as " width" isn't the name of any property due to the leading space.
Note: ''!important'' flags are not part of property grammars,
and will cause |value| to parse as invalid,
just as they would in the value argument to ''element.style.setProperty()''.
supports(CSSOMString conditionText)
, returns boolean
-
When the {{supports(conditionText)}} method is invoked
with a single conditionText argument:
1. If |conditionText|,
[=CSS/parsed=] and evaluated as a <>,
would return true,
return true
.
2. Otherwise,
If |conditionText|,
wrapped in parentheses
and then [=CSS/parsed=] and evaluated as a <>,
would return true,
return true
.
3. Otherwise, return false
.
All namespaces in the conditionText argument
are considered invalid,
just as they are in document.querySelector("a|b")
.
js/supports-conditionText.html
js/CSS-supports-L3.html
Security Considerations
This spec introduces no new security considerations.
Privacy Considerations
Various features in this specification,
associated mainly with the ''@media'' rule
but also to some degree with the ''@supports'' rule,
provide information to Web content about
the user's hardware and software and their configuration and state.
Most of the information is provided through the features in [[MEDIAQUERIES-4]]
rather than through the features in this specification.
However, the ''@supports'' rule may provide some additional details about the user's software
and whether it is running with non-default settings that may enable or disable certain features.
Most of this information can also be determined through other APIs.
However, the features in this specification are one of the ways this information
is exposed on the Web.
This information can also, in aggregate, be used to improve the accuracy of
fingerprinting of the user.
Changes
The following (non-editorial) changes were made to this specification since the
13 January 2022 Candidate Recommendation Snapshot:
- Clarified that supports() must return false for invalid custom property values
- Fixed Web IDL, "bool" should have been "boolean"
- Clarified that a processor must support both the property and the value (Issue 8795)
- Added .matches to @media and @supports (Issue 4240)
- Updated to the new parsing algo names and block production names.
- Removed procedure to set readonly CSSMediaRule.conditionText (PR 8796)
- Made conditionText readonly.
The following (non-editorial) changes were made to this specification since the
8 December 2020 Candidate Recommendation Snapshot:
- Clarified that discarding property declarations only applies to style rules, not at-rules
- Clarified that !important is not part of the property grammar
- Split Security and Privacy into separate sections
- Defined the terms [=CSS feature queries=] and [=supports queries=]
to refer to the conditional syntax of the ''@supports'' rules,
to allow better cross-referencing.
- Removed the “unknown” value in [=CSS feature queries=]’ boolean logic,
defining unrecognized syntaxes as “false” instead.
(Issue 6175)
- Clarified [[#use|placement]] of [=conditional group rules=].
(Issue 5697)
Conditional group rules are allowed wherever style rules are allowed
(at the top-level of a style sheet, and inside as well as within
other conditional group rules).
CSS processors must process such rules as described above.
Any at-rules that are not allowed after a style rule
(e.g., @charset , @import , or @namespace rules)
are also not allowed after a conditional group rule.
Therefore, style sheets must not place such rules after a conditional group rule,
and CSS processors must ignore such rules.,
and are therefore invalid when so placed.
The following (non-editorial) changes were made to this specification since the
4 April 2013 Candidate Recommendation:
- Clarified that namespaces in conditionText are invalid
- New editors added
- Added explicit call to [=CSS/parse=] rather than "matches the grammar"
- Removed duplicate CSSGroupingRule, which is already defined by CSSOM
- Rewrote the supports() text into algorithm form,
to make it easier to express that you pay attention to
the syntax of registered custom properties in the supports(prop, val) form.
- Moved the definition of @supports selector to css-conditional-4.
- ''@supports''' is no longer at risk.
- Rewrote to use CSS Syntax grammars, not CSS 2.1 grammars
- Changed from CSS Interface to WebIDL-compatible CSS namespace
- Dropped requirement for spaces around ''and'', ''or'', and ''not'' keywords
for consistency with Media Queries
(which are themselves constrained by compatibility with the output of some CSS minimizers
that rely on some of the more arcane aspects of CSS tokenization).
Note that white space--or a comment--is still required after these keywords,
since without it they and the ensuing opening parenthesis will be tokenized as a function opening token.
- Allowed the
supports()
method
to imply parentheses for simple declarations,
for consistency with the @import rule’s supports() function.
- Fixed missing semicolons in IDL code.
- Updated links, terminology, and example code in response to changes to other modules.
- Spelling and grammatical corrections
- Added section on privacy and security considerations.
Acknowledgments
Thanks to the ideas and feedback from
Tab Atkins,
Arthur Barstow,
Ben Callahan,
Tantek Çelik,
Alex Danilo,
Elika Etemad,
Pascal Germroth,
Björn Höhrmann,
Paul Irish,
Brad Kemper,
Anne van Kesteren,
Vitor Menezes,
Alex Mogilevsky,
Chris Moschini,
James Nurthen,
Simon Pieters,
Florian Rivoal,
Simon Sapin,
Nicholas Shanks,
Ben Ward,
Zack Weinberg,
Estelle Weyl,
Boris Zbarsky,
and all the rest of the www-style community.
at-supports-selector-001.html
at-supports-selector-002.html
at-supports-selector-003.html
at-supports-selector-004.html
at-supports-selector-detecting-invalid-in-logical-combinations.html
at-supports-selector-file-selector-button.html
at-supports-selector-placeholder.html
js/CSS-supports-L4.html
js/CSS-supports-selector-detecting-invalid-in-logical-combinations.html
at-supports-namespace-002.html
at-supports-font-format-001.html
at-supports-font-tech-001.html
container-queries/animation-container-size.html
container-queries/animation-container-type-dynamic.html
container-queries/animation-nested-animation.html
container-queries/animation-nested-transition.html
container-queries/aspect-ratio-feature-evaluation.html
container-queries/at-container-parsing.html
container-queries/at-container-serialization.html
container-queries/at-container-style-parsing.html
container-queries/at-container-style-serialization.html
container-queries/auto-scrollbars.html
container-queries/backdrop-invalidation.html
container-queries/calc-evaluation.html
container-queries/canvas-as-container-001.html
container-queries/canvas-as-container-002.html
container-queries/canvas-as-container-003.html
container-queries/canvas-as-container-004.html
container-queries/canvas-as-container-005.html
container-queries/canvas-as-container-006.html
container-queries/change-display-in-container.html
container-queries/chrome-legacy-skip-recalc.html
container-queries/column-spanner-in-container.html
container-queries/conditional-container-status.html
container-queries/container-computed.html
container-queries/container-for-cue.html
container-queries/container-for-shadow-dom.html
container-queries/container-inheritance.html
container-queries/container-inner-at-rules.html
container-queries/container-inside-multicol-with-table.html
container-queries/container-longhand-animation-type.html
container-queries/container-name-computed.html
container-queries/container-name-invalidation.html
container-queries/container-name-parsing.html
container-queries/container-name-tree-scoped.html
container-queries/container-nested.html
container-queries/container-parsing.html
container-queries/container-selection-unknown-features.html
container-queries/container-selection.html
container-queries/container-size-invalidation-after-load.html
container-queries/container-size-invalidation.html
container-queries/container-size-nested-invalidation.html
container-queries/container-size-shadow-invalidation.html
container-queries/container-type-computed.html
container-queries/container-type-containment.html
container-queries/container-type-invalidation.html
container-queries/container-type-layout-invalidation.html
container-queries/container-type-parsing.html
container-queries/container-units-animation.html
container-queries/container-units-basic.html
container-queries/container-units-computational-independence.html
container-queries/container-units-content-box.html
container-queries/container-units-gradient-invalidation.html
container-queries/container-units-gradient.html
container-queries/container-units-in-at-container-dynamic.html
container-queries/container-units-in-at-container-fallback.html
container-queries/container-units-in-at-container.html
container-queries/container-units-ineligible-container.html
container-queries/container-units-invalidation.html
container-queries/container-units-media-queries.html
container-queries/container-units-rule-cache.html
container-queries/container-units-selection.html
container-queries/container-units-shadow.html
container-queries/container-units-sharing-via-rule-node.html
container-queries/container-units-small-viewport-fallback.html
container-queries/container-units-svglength.html
container-queries/container-units-typed-om.html
container-queries/counters-flex-circular.html
container-queries/counters-in-container-dynamic.html
container-queries/counters-in-container.html
container-queries/crashtests/br-crash.html
container-queries/crashtests/canvas-as-container-crash.html
container-queries/crashtests/chrome-bug-1289718-000-crash.html
container-queries/crashtests/chrome-bug-1289718-001-crash.html
container-queries/crashtests/chrome-bug-1346969-crash.html
container-queries/crashtests/chrome-bug-1362391-crash.html
container-queries/crashtests/chrome-bug-1429955-crash.html
container-queries/crashtests/chrome-bug-1505250-crash.html
container-queries/crashtests/chrome-bug-346264227-crash.html
container-queries/crashtests/chrome-custom-highlight-crash.html
container-queries/crashtests/chrome-layout-root-crash.html
container-queries/crashtests/chrome-quotes-crash.html
container-queries/crashtests/chrome-remove-insert-evaluator-crash.html
container-queries/crashtests/columns-in-table-001-crash.html
container-queries/crashtests/columns-in-table-002-crash.html
container-queries/crashtests/container-in-canvas-crash.html
container-queries/crashtests/container-type-change-chrome-legacy-crash.html
container-queries/crashtests/dialog-backdrop-crash.html
container-queries/crashtests/dirty-rowgroup-crash.html
container-queries/crashtests/flex-in-columns-000-crash.html
container-queries/crashtests/flex-in-columns-001-crash.html
container-queries/crashtests/flex-in-columns-002-crash.html
container-queries/crashtests/flex-in-columns-003-crash.html
container-queries/crashtests/focus-inside-content-visibility-crash.html
container-queries/crashtests/force-sibling-style-crash.html
container-queries/crashtests/grid-in-columns-000-crash.html
container-queries/crashtests/grid-in-columns-001-crash.html
container-queries/crashtests/grid-in-columns-002-crash.html
container-queries/crashtests/grid-in-columns-003-crash.html
container-queries/crashtests/iframe-init-crash.html
container-queries/crashtests/inline-multicol-inside-container-crash.html
container-queries/crashtests/inline-with-columns-000-crash.html
container-queries/crashtests/inline-with-columns-001-crash.html
container-queries/crashtests/input-column-group-container-crash.html
container-queries/crashtests/input-placeholder-inline-size-crash.html
container-queries/crashtests/marker-gcs-after-disconnect-crash.html
container-queries/crashtests/math-block-container-child-crash.html
container-queries/crashtests/mathml-container-type-crash.html
container-queries/crashtests/orthogonal-replaced-crash.html
container-queries/crashtests/pseudo-container-crash.html
container-queries/crashtests/remove-dom-child-change-style.html
container-queries/crashtests/reversed-ol-crash.html
container-queries/crashtests/size-change-during-transition-crash.html
container-queries/crashtests/svg-layout-root-crash.html
container-queries/crashtests/svg-text-crash.html
container-queries/crashtests/table-in-columns-000-crash.html
container-queries/crashtests/table-in-columns-001-crash.html
container-queries/crashtests/table-in-columns-002-crash.html
container-queries/crashtests/table-in-columns-003-crash.html
container-queries/crashtests/table-in-columns-004-crash.html
container-queries/crashtests/table-in-columns-005-crash.html
container-queries/custom-layout-container-001.https.html
container-queries/custom-property-style-queries.html
container-queries/custom-property-style-query-change.html
container-queries/deep-nested-inline-size-containers.html
container-queries/dialog-backdrop-create.html
container-queries/dialog-backdrop-remove.html
container-queries/display-contents-dynamic-style-queries.html
container-queries/display-contents.html
container-queries/display-in-container.html
container-queries/display-none.html
container-queries/fieldset-legend-change.html
container-queries/font-relative-calc-dynamic.html
container-queries/font-relative-units-dynamic.html
container-queries/font-relative-units.html
container-queries/fragmented-container-001.html
container-queries/get-animations.html
container-queries/grid-container.html
container-queries/grid-item-container.html
container-queries/idlharness.html
container-queries/iframe-in-container-invalidation.html
container-queries/iframe-invalidation.html
container-queries/ineligible-containment.html
container-queries/inheritance-from-container.html
container-queries/inline-size-and-min-width.html
container-queries/inline-size-bfc-floats.html
container-queries/inline-size-containment-vertical-rl.html
container-queries/inline-size-containment.html
container-queries/inner-first-line-non-matching.html
container-queries/layout-dependent-focus.html
container-queries/multicol-container-001.html
container-queries/multicol-inside-container.html
container-queries/nested-query-containers.html
container-queries/nested-size-style-container-invalidation.html
container-queries/never-match-container.html
container-queries/no-layout-containment-abspos-dynamic.html
container-queries/no-layout-containment-abspos.html
container-queries/no-layout-containment-baseline.html
container-queries/no-layout-containment-fixedpos-dynamic.html
container-queries/no-layout-containment-fixedpos.html
container-queries/no-layout-containment-scroll.html
container-queries/orthogonal-wm-container-query.html
container-queries/percentage-padding-orthogonal.html
container-queries/pseudo-elements-001.html
container-queries/pseudo-elements-002.html
container-queries/pseudo-elements-002b.html
container-queries/pseudo-elements-003.html
container-queries/pseudo-elements-004.html
container-queries/pseudo-elements-005.html
container-queries/pseudo-elements-006.html
container-queries/pseudo-elements-007.html
container-queries/pseudo-elements-008.html
container-queries/pseudo-elements-009.html
container-queries/pseudo-elements-010.html
container-queries/pseudo-elements-011.html
container-queries/pseudo-elements-012.html
container-queries/pseudo-elements-013.html
container-queries/query-content-box.html
container-queries/query-evaluation-style.html
container-queries/query-evaluation.html
container-queries/reattach-container-with-dirty-child.html
container-queries/registered-color-style-queries.html
container-queries/resize-while-content-visibility-hidden.html
container-queries/scrollbar-container-units-block.html
container-queries/scrollbar-container-units-inline.html
container-queries/sibling-layout-dependency.html
container-queries/size-container-no-principal-box.html
container-queries/size-container-with-quotes.html
container-queries/size-feature-evaluation.html
container-queries/style-change-in-container.html
container-queries/style-container-for-shadow-dom.html
container-queries/style-container-invalidation-inheritance.html
container-queries/style-not-sharing-float.html
container-queries/style-query-with-unknown-width.html
container-queries/svg-foreignobject-child-container.html
container-queries/svg-foreignobject-no-size-container.html
container-queries/svg-g-no-size-container.html
container-queries/svg-root-size-container.html
container-queries/table-inside-container-changing-display.html
container-queries/top-layer-dialog-backdrop.html
container-queries/top-layer-dialog-container.html
container-queries/top-layer-dialog.html
container-queries/top-layer-nested-dialog.html
container-queries/transition-scrollbars.html
container-queries/transition-style-change-event-002.html
container-queries/transition-style-change-event.html
container-queries/unsupported-axis.html
container-queries/viewport-units-dynamic.html
container-queries/viewport-units.html
container-queries/whitespace-update-after-removal.html
js/CSS-supports-L5.html