22 Using Media Queries - CSS - Cascading Style Sheets - MDN
22 Using Media Queries - CSS - Cascading Style Sheets - MDN
Syntax
A media query is composed of an optional media type and any
number of media feature expressions, which may optionally be
combined in various ways using logical operators. Media queries are
case-insensitive.
Media types define the broad category of device for which the
media query applies: all , print , screen . The type is optional
(assumed to be all ) except when using the not or only logical
operators.
Media features describe a specific characteristic of the user
agent, output device, or environment:
any-hover
any-pointer
aspect-ratio
color
color-gamut
color-index
device-aspect-ratio
device-height
device-width
display-mode
dynamic-range
forced-colors
grid
height
hover
inverted-colors
monochrome
orientation
overflow-block
overflow-inline
pointer
prefers-color-scheme
prefers-contrast
prefers-reduced-motion
resolution
scripting
update
video-dynamic-range
width .
For example, the hover feature allows a query to test against
whether the device supports hovering over elements. Media
feature expressions test for their presence or value, and are
entirely optional. Each media feature expression must be
surrounded by parentheses.
Logical operators can be used to compose a complex media
query: not , and , and only . You can also combine multiple media
queries into a single rule by separating them with commas.
A media query computes to true when the media type (if specified)
matches the device on which a document is being displayed and all
media feature expressions compute as true. Queries involving
unknown media types are always false.
You can also target multiple devices. For instance, this @media rule
uses two media queries to target both screen and print devices:
@media screen, print {
/* … */
}
See media type for a list of all media types. Because they describe
devices in only very broad terms, just a few are available; to target
more specific attributes, use media features instead.
its normal meaning. The only operator prevents older browsers from
applying the styles.
type.
To limit the styles to devices with a screen, you can chain the media
features to the screen media type:
@media screen and (min-width: 30em) and (orientation: landscape)
{
/* … */
}
Taking the above example, if the user had a printer with a page
height of 800px, the media statement would return true because the
first query would apply. Likewise, if the user were on a smartphone in
portrait mode with a viewport height of 480px, the second query
would apply and the media statement would still return true.
Inverting a query's meaning
The not keyword inverts the meaning of an entire media query. It will
only negate the specific media query it is applied to. (Thus, it will not
apply to every media query in a comma-separated list of media
queries.) The not keyword can't be used to negate an individual
feature query, only an entire media query. The not is evaluated last
in the following query:
@media not all and (monochrome) {
/* … */
}
Using min- and max- we might test for a width between two values
like so:
@media (min-width: 30em) and (max-width: 50em) {
/* … */
}
See also
@media
Container queries
Testing media queries programmatically
CSS Animations Between Media Queries
Extended Mozilla media features
Extended WebKit media features