@media
The @media
CSS at-rule can be used to apply part of a style sheet based on the result of one or more media queries. With it, you specify a media query and a block of CSS to apply to the document if and only if the media query matches the device on which the content is being used.
Note: In JavaScript, the rules created using @media
can be accessed with the CSSMediaRule
CSS object model interface.
Syntax
The @media
at-rule may be placed at the top level of your code or nested inside any other conditional group at-rule.
/* At the top level of your code */ @media screen and (min-width: 900px) { article { padding: 1rem 3rem; } } /* Nested within another conditional at-rule */ @supports (display: flex) { @media screen and (min-width: 900px) { article { display: flex; } } }
For a discussion of media query syntax, please see Using media queries.
Description
Media types
Media types describe the general category of a device. Except when using the not
or only
logical operators, the media type is optional and the all
type will be implied.
all
-
Suitable for all devices.
print
-
Intended for paged material and documents viewed on a screen in print preview mode. (Please see paged media for information about formatting issues that are specific to these formats.)
screen
-
Intended primarily for screens.
speech
-
Intended for speech synthesizers.
Note: CSS2.1 and Media Queries 3 defined several additional media types (tty
, tv
, projection
, handheld
, braille
, embossed
, and aural
), but they were deprecated in Media Queries 4 and shouldn't be used. The aural
type has been replaced by speech
, which is similar.
Media features
Media features describe specific characteristics of the user agent, output device, or environment. Media feature expressions test for their presence or value, and are entirely optional. Each media feature expression must be surrounded by parentheses.
Name | Summary | Notes |
---|---|---|
any-hover | Does any available input mechanism allow the user to hover over elements? | Added in Media Queries Level 4. |
any-pointer | Is any available input mechanism a pointing device, and if so, how accurate is it? | Added in Media Queries Level 4. |
aspect-ratio | Width-to-height aspect ratio of the viewport | |
color | Number of bits per color component of the output device, or zero if the device isn't color | |
color-gamut | Approximate range of colors that are supported by the user agent and output device | Added in Media Queries Level 4. |
color-index | Number of entries in the output device's color lookup table, or zero if the device does not use such a table | |
device-aspect-ratio
| Width-to-height aspect ratio of the output device | Deprecated in Media Queries Level 4. |
device-height
| Height of the rendering surface of the output device | Deprecated in Media Queries Level 4. |
device-width
| Width of the rendering surface of the output device | Deprecated in Media Queries Level 4. |
display-mode | The display mode of the application, as specified in the web app manifest's display member | Defined in the Web App Manifest spec. |
forced-colors | Detect whether user agent restricts color palette | Added in Media Queries Level 5. |
grid | Does the device use a grid or bitmap screen? | |
height | Height of the viewport | |
hover | Does the primary input mechanism allow the user to hover over elements? | Added in Media Queries Level 4. |
inverted-colors | Is the user agent or underlying OS inverting colors? | Added in Media Queries Level 5. |
monochrome | Bits per pixel in the output device's monochrome frame buffer, or zero if the device isn't monochrome | |
orientation | Orientation of the viewport | |
overflow-block | How does the output device handle content that overflows the viewport along the block axis? | Added in Media Queries Level 4. |
overflow-inline | Can content that overflows the viewport along the inline axis be scrolled? | Added in Media Queries Level 4. |
pointer | Is the primary input mechanism a pointing device, and if so, how accurate is it? | Added in Media Queries Level 4. |
prefers-color-scheme | Detect if the user prefers a light or dark color scheme | Added in Media Queries Level 5. |
prefers-contrast | Detects if the user has requested the system increase or decrease the amount of contrast between adjacent colors | Added in Media Queries Level 5. |
prefers-reduced-motion | The user prefers less motion on the page | Added in Media Queries Level 5. |
resolution | Pixel density of the output device | |
scripting | Detects whether scripting (i.e. JavaScript) is available | Added in Media Queries Level 5. |
update | How frequently the output device can modify the appearance of content | Added in Media Queries Level 4. |
width | Width of the viewport including width of scrollbar |
Accessibility concerns
To best accommodate people who adjust a site's text size, use em
s when you need a <length>
for your media queries.
Both em
and px
are valid units, but em
works better if the user changes the browser text size.
Also consider using Level 4 media queries to improve the user's experience. For example, prefers-reduced-motion
to detect if the user has requested that the system minimize the amount of animation or motion it uses.
Security
Because media queries provide insights into the capabilities—and by extension, the features and design—of the device the user is working with, there is the potential that they could be abused to construct a "fingerprint" which identifies the device, or at least categorizes it to some degree of detail that may be undesirable to users.
Because of this potential, a browser may opt to fudge the returned values in some manner in order to prevent them from being used to precisely identify a computer. A browser might also offer additional measures in this area; for example, if Firefox's "Resist Fingerprinting" setting is enabled, many media queries report default values rather than values representing the actual device state.
Formal syntax
@media <media-query-list> { <group-rule-body> }where
<media-query-list> = <media-query>#where
<media-query> = <media-condition> | [ not | only ]? <media-type> [ and <media-condition-without-or> ]?where
<media-condition> = <media-not> | <media-and> | <media-or> | <media-in-parens>
<media-type> = <ident>
<media-condition-without-or> = <media-not> | <media-and> | <media-in-parens>where
<media-not> = not <media-in-parens>
<media-and> = <media-in-parens> [ and <media-in-parens> ]+
<media-or> = <media-in-parens> [ or <media-in-parens> ]+
<media-in-parens> = ( <media-condition> ) | <media-feature> | <general-enclosed>where
<media-feature> = ( [ <mf-plain> | <mf-boolean> | <mf-range> ] )
<general-enclosed> = [ <function-token> <any-value> ) ] | ( <ident> <any-value> )where
<mf-plain> = <mf-name> : <mf-value>
<mf-boolean> = <mf-name>
<mf-range> = <mf-name> [ '<' | '>' ]? '='? <mf-value> | <mf-value> [ '<' | '>' ]? '='? <mf-name> | <mf-value> '<' '='? <mf-name> '<' '='? <mf-value> | <mf-value> '>' '='? <mf-name> '>' '='? <mf-value>where
<mf-name> = <ident>
<mf-value> = <number> | <dimension> | <ident> | <ratio>
Examples
Testing for print and screen media types
@media print { body { font-size: 10pt; } } @media screen { body { font-size: 13px; } } @media screen, print { body { line-height: 1.2; } } @media only screen and (min-width: 320px) and (max-width: 480px) and (resolution: 150dpi) { body { line-height: 1.4; } }
Introduced in Media Queries Level 4 is a new range syntax that allows for less verbose media queries when testing for any feature accepting a range, as shown in the below examples:
@media (height > 600px) { body { line-height: 1.4; } } @media (400px <= width <= 700px) { body { line-height: 1.4; } }
For more examples, please see Using media queries.
Specifications
Specification |
---|
Media Queries Level 4 (Media Queries 4) # media-descriptor-table |
CSS Conditional Rules Module Level 3 (CSS Conditional 3) # at-media |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
@media |
1 |
12 |
1 |
6 |
9.2 |
3 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
any-hover |
41 |
16 |
64 |
No |
28 |
9 |
41 |
41 |
64 |
28 |
9 |
5.0 |
any-pointer |
41 |
12 |
64 |
No |
28 |
9 |
41 |
41 |
64 |
28 |
9 |
4.0 |
aspect-ratio |
3 |
12 |
3.5 |
9 |
10 |
5 |
≤37 |
18 |
4 |
10.1 |
4.2 |
1.0 |
calc |
66 |
79 |
59 |
No |
53 |
12 |
66 |
66 |
59 |
47 |
12 |
9.0 |
color |
1 |
12 |
2 |
9 |
10 |
3 |
≤37 |
18 |
4 |
10.1 |
1 |
1.0 |
color-gamut |
58 |
79 |
No |
No |
45 |
10 |
58 |
58 |
No |
43 |
10 |
7.0 |
color-index |
29 |
79 |
No |
No |
16 |
8 |
≤37 |
29 |
No |
16 |
8 |
2.0 |
device-aspect-ratio |
1 |
12 |
2 |
9 |
10 |
3 |
≤37 |
18 |
4 |
10.1 |
1 |
1.0 |
device-height |
1 |
12 |
2 |
9 |
10 |
3 |
≤37 |
18 |
4 |
10.1 |
1 |
1.0 |
device-width |
1 |
12 |
2 |
9 |
10 |
3 |
≤37 |
18 |
4 |
10.1 |
1 |
1.0 |
display-mode |
45 |
79 |
47
Firefox 47 and later support
display-mode values fullscreen and browser . Firefox 57 added support for minimal-ui and standalone values. |
No |
32 |
13 |
45 |
45 |
47
Firefox 47 and later support
display-mode values fullscreen and browser . Firefox 57 added support for minimal-ui and standalone values. |
32 |
12.2 |
5.0 |
forced-colors |
89
79
See bug 970285.
|
79 |
89
81
|
No |
No |
No |
No
See bug 970285.
|
No
See bug 970285.
|
89 |
No |
No |
No
See bug 970285.
|
grid |
1 |
12 |
2 |
10 |
10 |
3 |
≤37 |
18 |
4 |
10.1 |
1 |
1.0 |
height |
1 |
12 |
2 |
9 |
10 |
3 |
≤37 |
18 |
4 |
10.1 |
1 |
1.0 |
hover |
38
Before Chrome 41, the implementation was buggy and reported
(hover: none) on non-touch-based computers with a mouse/trackpad. See bug 441613. |
12 |
64 |
No |
28 |
9 |
38
Before Chrome 41, the implementation was buggy and reported
(hover: none) on non-touch-based computers with a mouse/trackpad. See bug 441613. |
50 |
64 |
28 |
9 |
5.0 |
inverted-colors |
No |
No |
No |
No |
No |
9.1 |
No |
No |
No |
No |
10 |
No |
media_features |
1 |
12 |
1 |
9 |
9.2 |
3 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
media_query_values |
66 |
79 |
59 |
No |
53 |
No |
66 |
66 |
59 |
47 |
No |
9.0 |
monochrome |
1 |
79 |
2 |
No |
10 |
3 |
≤37 |
18 |
4 |
10.1 |
1 |
1.0 |
nested-queries |
26 |
12 |
11 |
No |
12.1 |
7 |
≤37 |
26 |
14 |
12.1 |
7 |
1.5 |
orientation |
3 |
12 |
2 |
9 |
10.6 |
5 |
≤37 |
18 |
4 |
11 |
4.2 |
1.0 |
overflow-block |
No |
No |
66 |
No |
No |
No |
No |
No |
66 |
No |
No |
No |
overflow-inline |
No |
No |
66 |
No |
No |
No |
No |
No |
66 |
No |
No |
No |
pointer |
41 |
12 |
64 |
No |
28 |
9 |
41 |
50 |
64 |
28 |
9 |
5.0 |
prefers-color-scheme |
76 |
79 |
67 |
No |
62 |
12.1 |
76 |
76 |
67 |
54 |
13 |
14.2 |
prefers-contrast |
96 |
96 |
80 |
No |
No |
14.1 |
96 |
96 |
No |
No |
14.5 |
No |
prefers-reduced-data |
85
See bug 1051189.
|
85
See bug 1051189.
|
No |
No |
No |
No |
No
See bug 1051189.
|
85
See bug 1051189.
|
No |
No |
No |
No |
prefers-reduced-motion |
74 |
79 |
63 |
No |
62 |
10.1 |
74 |
74 |
64 |
53 |
10.3 |
11.0 |
range_syntax |
No
See bug 1034465.
|
No |
63 |
No |
No |
No |
No |
No |
63 |
No |
No |
No |
resolution |
29 |
12 |
8
3.5
Supports
<integer> values only. |
9 |
16
10-15
|
No
See bug 78087.
|
≤37 |
29 |
8
4
Supports
<integer> values only. |
16
10.1-14
|
No
See bug 78087.
|
2.0 |
scripting |
No
See bug 489957.
|
No
See bug 489957.
|
No
See bug 1166581.
|
No |
No |
No |
No
See bug 489957.
|
No
See bug 489957.
|
No
See bug 1166581.
|
No |
No |
No
See bug 489957.
|
speech_type |
No |
No |
No |
No |
9.2 |
No |
No |
No |
No |
10.1 |
No |
No |
width |
1 |
12 |
2 |
9 |
10 |
3 |
≤37 |
18 |
4 |
10.1 |
1 |
1.0 |
-moz-device-pixel-ratio |
No |
No |
4 |
No |
No |
No |
No |
No |
4 |
No |
No |
No |
-webkit-animation |
2-36 |
No |
No |
No |
15-23 |
4 |
2-37 |
18-36 |
No |
14-24 |
3.2 |
1.0-3.0 |
-webkit-device-pixel-ratio |
1 |
12 |
63
Implemented as an alias for for
-moz-device-pixel-ratio . |
No |
15 |
3 |
≤37 |
18 |
63
Implemented as an alias for for
-moz-device-pixel-ratio . |
14 |
1 |
1.0 |
-webkit-max-device-pixel-ratio |
1 |
12 |
63
Implemented as an alias for for
max--moz-device-pixel-ratio . |
No |
15 |
3 |
≤37 |
18 |
63
Implemented as an alias for for
max--moz-device-pixel-ratio . |
14 |
1 |
1.0 |
-webkit-min-device-pixel-ratio |
1 |
12 |
63
Implemented as an alias for for
min--moz-device-pixel-ratio . |
No |
15 |
3 |
≤37 |
18 |
63
Implemented as an alias for for
min--moz-device-pixel-ratio . |
14 |
1 |
1.0 |
-webkit-transform-2d |
2-36 |
No |
No |
No |
15-23 |
4 |
2-37 |
18-36 |
No |
14-24 |
3.2 |
1.0-3.0 |
-webkit-transform-3d |
2-36 |
12-79 |
49 |
No |
15-23 |
4 |
2-37 |
18-36 |
49 |
14-24 |
3.2 |
1.0-3.0 |
-webkit-transition |
2-36 |
No |
No |
No |
15-23 |
4 |
2-37 |
18-36 |
No |
14-24 |
3.2 |
1.0-3.0 |
See also
- Using media queries
- In JavaScript,
@media
can be accessed via the CSS object model interfaceCSSMediaRule
. - Extended Mozilla media features
- Extended WebKit media features
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/@media