@custom-media - CSS At-rule
Summary of characteristics of the @custom-media at-rule
Description of the @custom-media at-rule.
@custom-media sets the conditions for a media query, which can then be included in a @media at-rule. This helps avoid repeating the
definition of a media query multiple times in the style-sheet. We know that repeating a definition leads to errors and makes maintaining style-sheets
more complicated.
The example below defines the name --dinosaur and matches it to a series of conditions describing low-performing or old hardware: a monochrome
or small-sized screen. This name can then be used in one or more @media directives.
@custom-media --dinosaur (monochrome), (width < 780px), (height < 600px);
@media (--dinosaur) {
...
}
The comma is equivalent to or, but it's possible to use this keyword instead. Similarly, the keywords and and not can be used.
If a custom media query is included in an expression with other conditions, the custom media query is evaluated first.
@custom-media --small-screen (width < 780px) or (height < 600px);
@media (monochrome) and (--small-screen) { ... }
/* is equivalent to */
@media ((width < 780px) or (height < 600px)) and (monochrome) { ... }
A custom media query can call other custom media queries. But be careful not to create loops. If a loop is detected by browsers, all the media queries that are part of the loop might become invalid.
If several custom media requests define the same name, only the last version is considered. Previous definitions are ignored.
The keywords true or false can be used in a custom media query (in this case, there’s no need to include other conditions).
These keywords can be used to write a custom media query that is always true or always false, for debugging purposes, for example.
@custom-media --active true;
@custom-media --inactive false;
The syntax for conditions is the same as that of @media. Check out the @media directive.
Browsers compatibility with the @custom-media at-rule..
The directive @custom-media is still poorly supported (2026). It can't be used. Only handles it, and you still need to enable a flag (see
how access flags on Firefox).
@custom-media directive, which lets you define a custom @media-query rule.Notes:
(1) Disabled by default. Can be enabled by setting the flag layout.css.custom-media.enabled to true.
@custom-mediaat-rule
Browsers on computers :
Mobile browsers :
Outdated or marginal browsers :

Internet Explorer

UC Browser pour Androïd

Opéra Mobile

QQ Browser

Baidu Browser

Samsung Internet

Androïd Brower

Chrome

Chrome pour Androïd

Edge

Firefox

Firefox pour Androïd

KaiOS Browser

Opéra

Safari

Safari sur IOS

Opéra mini
Evolution of the @custom-media at-rule.
-
Media Queries Level 5
Level 5 introduces the ability to test a wide range of features, such as the number of screen segments, video characteristics, the presence of a scripting language, and more.
Custom@mediaqueries are also introduced.Regarding@custom-mediaFirst description of the@custom-mediaat-rule.March 03, 2020Working Draft.
See also, about media queries.
@custom-media is described in the specification "Media Queries". See also, in this same module:



