Media features describe the specific characteristics of a given user agent, output device, or environment. For instance, you can apply specific styles to widescreen monitors, computers that use mice, or devices that are being used in low-light conditions. This example applies styles when the user's primary input mechanism (such as a mouse) can hover over elements:
@media (hover: hover) {
/* … */
}
Media features are either range or discrete.
Discrete features take their value from an enumerated set of possible keyword values. For example, the discrete orientation
feature accepts either landscape
or portrait
.
@media print and (orientation: portrait) {
/* … */
}
Many range features can be prefixed with "min-" or "max-" to express "minimum condition" or "maximum condition" constraints. For example, this CSS will apply styles only if your browser's viewport width is equal to or narrower than 1250px:
@media (max-width: 1250px) {
/* … */
}
This can also be written as:
@media (width <= 1250px) {
/* … */
}
With media query range features, you can either use the inclusive min-
and max-
prefixes or the more concise range syntax operators <=
and =>
.
The following media queries are equivalent:
@media (min-width: 30em) and (max-width: 50em) {
/* … */
}
@media (30em <= width <= 50em) {
/* … */
}
The range comparisons above are inclusive. To not include the comparison value, use <
and >
.
@media (30em < width < 50em) {
/* … */
}
If you create a media feature query without specifying a value, the nested styles will be used as long as the feature's value is not 0 or none
. For example, this CSS will apply to any device with a color screen:
@media (color) {
/* … */
}
If a feature doesn't apply to the device on which the browser is running, expressions involving that media feature are always false.
For more Media feature examples, please see the reference page for each specific feature.