Spec-Zone.ru › CSS

`@media` CSS at-rule

Baseline Widely available

This feature is well established and works across many devices and browser versions. It's been available across browsers since July 2015.

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.

Try it

abbr {
  color: #860304;
  font-weight: bold;
  transition: color 0.5s ease;
}

@media (hover: hover) {
  abbr:hover {
    color: #001ca8;
    transition-duration: 0.5s;
  }
}

@media not all and (hover: hover) {
  abbr::after {
    content: " (" attr(title) ")";
  }
}
<p>
  <abbr title="National Aeronautics and Space Administration">NASA</abbr> is a
  U.S. government agency that is responsible for science and technology related
  to air and space.
</p>

Syntax

/* At the top level of your code */
@media screen and (width >= 900px) {
  article {
    padding: 1rem 3rem;
  }
}

/* Nested within another conditional at-rule */
@supports (display: flex) {
  @media screen and (width >= 900px) {
    article {
      display: flex;
    }
  }
}

The @media at-rule may be placed at the top level of your code or nested inside any other conditional group at-rule.

For a discussion of media query syntax, please see Using media queries.

Description

A media query's <media-query-list> includes <media-type>s, <media-feature>s, and logical operators.

Media types

A <media-type> describes the general category of a device. Except when using the only logical operator, the media type is optional and the all type is 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.

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.

Media features

A <media feature> describes specific characteristics of the user agent, output device, or environment. Media feature expressions test for their presence, value, or range of values, and are entirely optional. Each media feature expression must be surrounded by parentheses.

any-hover
Does any available input mechanism allow the user to hover over elements?
any-pointer
Is any available input mechanism a pointing device, and if so, how accurate is it?
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.
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-posture
Detects the device's current posture, that is, whether the viewport is in a flat or folded state. Defined in the Device Posture API.
device-width
Width of the rendering surface of the output device. Deprecated in Media Queries Level 4.
display-mode
The mode in which an application is being displayed: for example, fullscreen or picture-in-picture mode. Added in Media Queries Level 5.
dynamic-range
Combination of brightness, contrast ratio, and color depth that are supported by the user agent and the output device. Added in Media Queries Level 5.
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.
horizontal-viewport-segments
Detects whether the device has a specified number of viewport segments laid out horizontally.
hover
Does the primary input mechanism allow the user to hover over elements?
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?
overflow-inline
Can content that overflows the viewport along the inline axis be scrolled?
pointer
Is the primary input mechanism a pointing device, and if so, how accurate is it?
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-data
Detects if the user has requested the web content that consumes less internet traffic.
prefers-reduced-motion
The user prefers less motion on the page. Added in Media Queries Level 5.
prefers-reduced-transparency
Detects if a user has enabled a setting on their device to reduce the transparent or translucent layer effects used on the device.
resolution
Pixel density of the output device.
scan
Whether display output is progressive or interlaced.
scripting
Detects whether scripting (i.e., JavaScript) is available. Added in Media Queries Level 5.
shape
Detects the shape of the device to distinguish rectangular and round displays.
update
How frequently the output device can modify the appearance of content.
vertical-viewport-segments
Detects whether the device has a specified number of viewport segments laid out vertically. Added in Media Queries Level 5.
video-dynamic-range
Combination of brightness, contrast ratio, and color depth that are supported by the video plane of user agent and the output device. Added in Media Queries Level 5.
width
Width of the viewport including width of scrollbar.
-moz-device-pixel-ratio
The number of device pixels per CSS pixel. Use the resolution feature with the dppx unit instead.
-webkit-animation
The browser supports -webkit prefixed CSS animation. Use the @supports (animation) feature query instead.
-webkit-device-pixel-ratio
The number of device pixels per CSS pixel. Use the resolution feature with the dppx unit instead.
-webkit-transform-2d
The browser supports -webkit prefixed 2D CSS transform. Use the @supports (transform) feature query instead.
-webkit-transform-3d
The browser supports -webkit prefixed 3D CSS transform. Use the @supports (transform) feature query instead.
-webkit-transition
The browser supports -webkit prefixed CSS transition. Use the @supports (transition) feature query instead.

Logical operators

The logical operators not, and, only, and or can be used to compose a complex media query. You can also combine multiple media queries into a single rule by separating them with commas.

and
Used for combining multiple media features together into a single media query, requiring each chained feature to return true for the query to be true. It is also used for joining media features with media types.
not

Used to negate a media query, returning true if the query would otherwise return false. If present in a comma-separated list of queries, it will only negate the specific query to which it is applied.

Note: In Level 3, the not keyword can't be used to negate an individual media feature expression, only an entire media query.

only
Applies a style only if an entire query matches. It is useful for preventing older browsers from applying selected styles. When not using only, older browsers would interpret the query screen and (width <= 500px) as screen, ignoring the remainder of the query, and applying its styles on all screens. If you use the only operator, you must also specify a media type.
, (comma)
Commas are used to combine multiple media queries into a single rule. Each query in a comma-separated list is treated separately from the others. Thus, if any of the queries in a list is true, the entire media statement returns true. In other words, lists behave like a logical or operator.
or
Equivalent to the , operator. Added in Media Queries Level 4.

User agent client hints

Some media queries have corresponding user agent client hints. These are HTTP headers that request content that is pre-optimized for the particular media requirement. They include Sec-CH-Prefers-Color-Scheme and Sec-CH-Prefers-Reduced-Motion.

Formal syntax

Accessibility

To best accommodate people who adjust a site's text size, use ems 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 media queries or HTTP user agent client hints to improve the user's experience. For example, the media query prefers-reduced-motion or the equivalent HTTP header Sec-CH-Prefers-Reduced-Motion) can be used to minimize the amount of animation or motion used based on user preferences.

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.

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;
  }
}

The range syntax 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-descriptor-table
CSS Conditional Rules Module Level 3
# at-media

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on iOS Samsung Internet WebView Android WebView on iOS
media
1
12
1
9.2
3
18
4
10.1
1
1.0
2
1
-moz-device-pixel-ratio
No
No
4
No
No
No
4
No
No
No
No
No
-webkit-animation
2–36
No
No
15–23
4
18–36
No
14–24
3.2
1.0–3.0
2–37
3.2
-webkit-device-pixel-ratio
1
12
63Implemented as an alias for -moz-device-pixel-ratio.
15
3
18
63Implemented as an alias for -moz-device-pixel-ratio.
14
1
1.0
4.4
1
-webkit-max-device-pixel-ratio
1
12
63Implemented as an alias for max--moz-device-pixel-ratio.
15
3
18
63Implemented as an alias for max--moz-device-pixel-ratio.
14
1
1.0
4.4
1
-webkit-min-device-pixel-ratio
1
12
63Implemented as an alias for min--moz-device-pixel-ratio.
15
3
18
63Implemented as an alias for min--moz-device-pixel-ratio.
14
1
1.0
4.4
1
-webkit-transform-2d
2–36
No
No
15–23
4
18–36
No
14–24
3.2
1.0–3.0
2–37
3.2
-webkit-transform-3d
2
12
49
15
4
18
49
14
3.2
1.0
4.4
3.2
-webkit-transition
2–36
No
No
15–23
4
18–36
No
14–24
3.2
1.0–3.0
2–37
3.2
any-hover
41
16
64
28
9
41
64
28
9
5.0
41
9
any-pointer
41
12
64
28
9
41
64
28
9
4.0
41
9
aspect-ratio
3
12
3.5
10
5
18
4
10.1
4.2
1.0
4.4
4.2
calc
66
79
59
53
12
66
59
47
12
9.0
66
12
color
1
12
2
10
3
18
4
10.1
1
1.0
4.4
1
color-gamut
58
79
110
45
10
58
110
43
10
7.0
58
10
color-index
29
79
No
16
8
29
No
16
8
2.0
4.4
8
device-aspect-ratio
1
12
2
10
3
18
4
10.1
1
1.0
4.4
1
device-height
1
12
2
10
3
18
4
10.1
1
1.0
4.4
1
device-posture
132
132
No
117
No
132
No
87
No
16.2
132
No
device-width
1
12
2
10
3
18
4
10.1
1
1.0
4.4
1
display-mode
42
79
47
29
13
42
47
29
12.2
4.0
42
12.2
dynamic-range
98
98
100
84
13.1
98
100
68
13.4
18.0
98
13.4
forced-colors
89
79
89
75
16
89
89
63
16
15.0
89
16
grid
1
12
2
10
3
18
4
10.1
1
1.0
4.4
1
height
1
12
2
10
3
18
4
10.1
1
1.0
4.4
1
horizontal-viewport-segments
138
138
No
122
No
138
No
91
No
30.0
138
No
hover
38Before Chrome 41, the implementation was buggy and reported (hover: none) on non-touch-based computers with a mouse/trackpad. See bug 40397980.
12
64
25Before Opera 28, the implementation was buggy and reported (hover: none) on non-touch-based computers with a mouse/trackpad. See bug 40397980.
9
50On some Android devices, such as certain Samsung models, the (hover: hover) media query may incorrectly match. See bug 41445959.
64
37On some Android devices, such as certain Samsung models, the (hover: hover) media query may incorrectly match. See bug 41445959.
9
5.0On some Android devices, such as certain Samsung models, the (hover: hover) media query may incorrectly match. See bug 41445959.
50On some Android devices, such as certain Samsung models, the (hover: hover) media query may incorrectly match. See bug 41445959.
9
inverted-colors
No
No
114
No
9.1
No
No
No
10
No
No
10
media_features
1
12
1
9.2
3
18
4
10.1
1
1.0
4.4
1
media_query_values
66
79
59
53
No
66
59
47
No
9.0
66
No
monochrome
1
79
2
10
3
18
4
10.1
1
1.0
4.4
1
nested-queries
26
12
11
12.1
7
26
14
12.1
7
1.5
4.4
7
or_syntax
104
104
64
90
16.4
104
64
71
16.4
20.0
104
16.4
orientation
3
12
2
10.6
5
18
4
11
4.2
1.0
4.4
4.2
overflow-block
113
113
66
99
17
113
66
76
17
23.0
113
17
overflow-inline
113
113
66
99
17
113
66
76
17
23.0
113
17
pointer
41
12
64
28
9
50
64
28
9
5.0
41
9
prefers-color-scheme
76
79
67
62
12.1
76
67
54
13
14.2
76
13
prefers-contrast
96
96
101
82
14.1
96
101
No
14.5
17.0
96
14.5
prefers-reduced-data
85
85
No
71
No
85
No
No
No
No
No
No
prefers-reduced-motion
74
79
63
62
10.1
74
64
53
10.3
11.0
74
10.3
prefers-reduced-transparency
118
118
113
104
No
118
No
79
No
25.0
118
No
range_syntax
104
104
102
63–102Only supports range notations where the feature name comes before any value (width > 500px)
90
16.4
104
102
63–102Only supports range notations where the feature name comes before any value (width > 500px)
71
16.4
20.0
104
16.4
resolution
29
12
8
3.5–8Supports <integer> values only.
16
10–15
16
29
8
4–8Supports <integer> values only.
16
10.1–14
16
2.0
4.4
16
scan
104
104
63
90
16.4
104
63
71
16.4
20.0
104
16.4
scripting
120
120
113
106
17
120
113
80
17
25.0
120
17
update
113
113
102
99
17
113
102
76
17
23.0
113
17
vertical-viewport-segments
138
138
No
122
No
138
No
91
No
30.0
138
No
video-dynamic-range
98
98
100
84
No
98
100
No
No
No
No
No
width
1
12
2
10
3
18
4
10.1
1
1.0
4.4
1

See also

  • CSS media queries module
  • Using media queries
  • CSSMediaRule interface
  • CSS @custom-media at-rule
  • Extended Mozilla media features
  • Extended WebKit media features

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@media

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API