@media CSS @规则 可根据一个或多个 媒体查询 的结果应用样式表的一部分。
 
注: 在JavaScript中, @media 可通过CSSMediaRule CSS 对象模型接口访问。

 

语法

@media 规则可置于您代码的顶层或位于其它任何@条件规则组内。

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

有关媒体查询语法的讨论,请参阅使用媒体查询

正式语法

@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>

媒体特性

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
width Width of the viewport  
height Height of the viewport  
aspect-ratio Width-to-height aspect ratio of the viewport  
orientation Orientation of the viewport  
resolution Pixel density of the output device  
scan Scanning process of the output device  
grid Does the device use a grid or bitmap screen?  
update How frequently the output device can modify the appearance of content Added in Media Queries Level 4.
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.
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  
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.
monochrome Bits per pixel in the output device's monochrome frame buffer, or zero if the device isn't monochrome  
inverted-colors Is the user agent or underlying OS inverting colors? Added in Media Queries Level 5.
pointer Is the primary input mechanism a pointing device, and if so, how accurate is it? Added in Media Queries Level 4.
hover Does the primary 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.
any-hover Does any available input mechanism allow the user to hover over elements? Added in Media Queries Level 4.
light-level Light level of the environment Added in Media Queries Level 5.
prefers-reduced-motion The user prefers less motion on the page Added in Media Queries Level 5.
prefers-reduced-transparency The user prefers reduced transparency 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-color-scheme Detect if the user prefers a light or dark color scheme Added in Media Queries Level 5.
scripting Detects whether scripting (i.e. JavaScript) is available Added in Media Queries Level 5.
device-width Width of the rendering surface 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-aspect-ratio Width-to-height aspect ratio of the output device Deprecated in Media Queries Level 4.

示例

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

媒体查询第4级引入了一种新的范围语法,在测试接受范围的任何特性时允许更简洁的媒体查询,如下面的示例所示:

@media (height > 600px) {
    body { line-height: 1.4; }
}

@media (400px <= width <= 700px) {
    body { line-height: 1.4; }
}

更多示例,请查看使用媒体查询

可访问性

为了最好地调整网站文本大小,当您需要<length> 进行媒体查询时,请使用em

empx都是有效单位,但如果用户更改浏览器文本大小,em的效果会更好。

考虑使用4级媒体查询来改善用户体验。 例如,使用prefers-reduced-motion检测用户是否已请求系统最小化其使用的动画或动作量。

规范

Specification Comment Feedback
Media Queries Level 5
@media
Reinstates light-level, inverted-colors and Custom Media Queries, which were removed from Level 4.
Adds prefers-reduced-motion, prefers-reduced-transparency, prefers-contrast, and prefers-color-scheme media features.
CSS Working Group drafts GitHub issues
CSS Conditional Rules Module Level 3
@media
Defines the basic syntax of the @media rule. CSS Working Group drafts GitHub issues
Media Queries Level 4
@media

Adds scripting, pointer, hover, update, overflow-block, and overflow-inline media features.
Deprecates all media types except for screen, print, speech, and all.
Makes the syntax more flexible by adding, among other things, the or keyword.

CSS Working Group drafts GitHub issues
Media Queries
@media
   
CSS Level 2 (Revision 1)
@media
Initial definition.  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
@mediaChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 6Opera Full support 9.2Safari Full support 1.3WebView Android Full support 1Chrome Android Full support 18Edge Mobile Full support 12Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3.1Samsung Internet Android ?
any-hover media featureChrome Full support 41Edge Full support 16Firefox Full support 64IE No support NoOpera Full support 28Safari Full support 9WebView Android Full support 41Chrome Android Full support 41Edge Mobile No support NoFirefox Android Full support 64Opera Android Full support 28Safari iOS Full support 9.2Samsung Internet Android Full support 5.0
any-pointer media featureChrome Full support 41Edge Full support 12Firefox Full support 64IE No support NoOpera Full support 28Safari Full support 9WebView Android Full support 41Chrome Android Full support 41Edge Mobile Full support 12Firefox Android Full support 64Opera Android Full support 28Safari iOS Full support 9.2Samsung Internet Android ?
aspect-ratio media featureChrome Full support 4Edge Full support 12Firefox Full support 3.5IE Full support 9Opera Full support 53Safari Full support YesWebView Android Full support YesChrome Android Full support 18Edge Mobile Full support 12Firefox Android ? Opera Android ? Safari iOS Full support YesSamsung Internet Android Full support Yes
calc() expressionsChrome Full support 66Edge No support NoFirefox Full support 59IE No support NoOpera Full support 53Safari Full support 12WebView Android Full support 66Chrome Android Full support 66Edge Mobile No support NoFirefox Android Full support 59Opera Android Full support 47Safari iOS Full support 12Samsung Internet Android Full support Yes
color media featureChrome Full support YesEdge Full support 12Firefox Full support 2IE Full support 9Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android ?
color-gamut media featureChrome Full support 58Edge ? Firefox No support NoIE No support NoOpera Full support 45Safari Full support YesWebView Android Full support 58Chrome Android Full support 58Edge Mobile ? Firefox Android No support NoOpera Android Full support 43Safari iOS ? Samsung Internet Android ?
color-index media feature
Non-standard
Chrome No support NoEdge ? Firefox No support NoIE No support NoOpera No support NoSafari ? WebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android ? Opera Android No support NoSafari iOS ? Samsung Internet Android No support No
device-aspect-ratio media feature
DeprecatedNon-standard
Chrome Full support YesEdge Full support 12Firefox Full support 2IE Full support 9Opera ? Safari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android ?
device-height media feature
DeprecatedNon-standard
Chrome Full support YesEdge Full support 12Firefox Full support 2IE Full support 9Opera ? Safari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android ?
device-width media featureChrome Full support YesEdge Full support 12Firefox Full support 2IE Full support 9Opera ? Safari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android ?
display-mode media featureChrome Full support 46Edge No support NoFirefox Full support 47
Notes
Full support 47
Notes
Notes Firefox 47 and later support display-mode values fullscreen and browser. Firefox 57 added support for minimal-ui and standalone values.
IE No support NoOpera ? Safari ? WebView Android Full support 46Chrome Android Full support 46Edge Mobile No support NoFirefox Android Full support 47
Notes
Full support 47
Notes
Notes Firefox 47 and later support display-mode values fullscreen and browser. Firefox 57 added support for minimal-ui and standalone values.
Opera Android ? Safari iOS ? Samsung Internet Android No support No
grid media featureChrome Full support YesEdge Full support 12Firefox Full support 2IE Full support 10Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android ?
height media featureChrome ? Edge Full support 12Firefox Full support 2IE Full support 9Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile Full support 12Firefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android ?
hover media featureChrome Full support 38
Notes
Full support 38
Notes
Notes Before Chrome 41, the implementation was buggy and reported (hover: none) on non-touch-based computers with a mouse/trackpad. See bug 441613.
Edge Full support 12Firefox Full support 64IE No support NoOpera Full support 28Safari Full support 9WebView Android Full support 38
Notes
Full support 38
Notes
Notes Before Chrome 41, the implementation was buggy and reported (hover: none) on non-touch-based computers with a mouse/trackpad. See bug 441613.
Chrome Android Full support 50Edge Mobile Full support 12Firefox Android Full support 64Opera Android Full support 28Safari iOS Full support 9.2Samsung Internet Android ?
inverted-colors media feature
ExperimentalNon-standard
Chrome No support NoEdge ? Firefox No support NoIE No support NoOpera No support NoSafari Full support YesWebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android No support NoOpera Android No support NoSafari iOS Full support YesSamsung Internet Android ?
light-level media feature
ExperimentalNon-standard
Chrome No support NoEdge ? Firefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android ?
Media feature expressionsChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 9Opera Full support 9.2Safari Full support 1.3WebView Android Full support 1Chrome Android Full support 18Edge Mobile Full support 12Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3.1Samsung Internet Android ?
Media query value supportChrome Full support 66Edge No support NoFirefox Full support 59IE No support NoOpera Full support 53Safari No support NoWebView Android No support NoChrome Android Full support 66Edge Mobile No support NoFirefox Android No support NoOpera Android Full support 47Safari iOS No support NoSamsung Internet Android No support No
monochrome media featureChrome Full support YesEdge ? Firefox Full support 2IE No support NoOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android ?
orientation media featureChrome Full support YesEdge Full support 12Firefox Full support 2IE Full support 9Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android ?
overflow-block media feature
Experimental
Chrome No support NoEdge No support NoFirefox Full support 66IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android Full support 66Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
overflow-inline media feature
Experimental
Chrome No support NoEdge No support NoFirefox Full support 66IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android Full support 66Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
pointer media featureChrome Full support 41Edge Full support 12Firefox Full support 64IE No support NoOpera Full support 28Safari Full support 9WebView Android Full support 41Chrome Android Full support 50Edge Mobile Full support 12Firefox Android Full support 64Opera Android Full support 28Safari iOS Full support 9.2Samsung Internet Android ?
prefers-color-scheme media feature
Experimental
Chrome No support NoEdge No support NoFirefox Full support 67IE No support NoOpera No support NoSafari Full support 12.1WebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android Full support 67Opera Android No support NoSafari iOS No support NoSamsung Internet Android ?
prefers-reduced-motion media feature
Experimental
Chrome Full support 74Edge No support NoFirefox Full support 63IE No support NoOpera No support NoSafari Full support 10.1WebView Android Full support 74Chrome Android Full support 74Edge Mobile No support NoFirefox Android Full support 64Opera Android No support NoSafari iOS Full support 10.3Samsung Internet Android ?
resolution media featureChrome Full support 29Edge Full support 12Firefox Full support 8
Full support 8
Partial support 3.5
Notes
Notes Supports <integer> values only.
IE Full support 9Opera Full support YesSafari ? WebView Android Full support 37Chrome Android Full support 29Edge Mobile Full support 12Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
scan media featureChrome No support NoEdge ? Firefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android No support NoOpera Android ? Safari iOS ? Samsung Internet Android ?
scripting media feature
ExperimentalNon-standard
Chrome No support No
Notes
No support No
Notes
Notes See bug 489957.
Edge ? Firefox No support No
Notes
No support No
Notes
Notes See bug 1166581.
IE No support NoOpera No support NoSafari No support NoWebView Android No support No
Notes
No support No
Notes
Notes See bug 489957.
Chrome Android No support No
Notes
No support No
Notes
Notes See bug 489957.
Edge Mobile ? Firefox Android No support No
Notes
No support No
Notes
Notes See bug 1166581.
Opera Android No support NoSafari iOS No support NoSamsung Internet Android ?
speech media typeChrome No support NoEdge No support NoFirefox No support NoIE No support NoOpera Full support 9.2Safari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android Full support 10.1Safari iOS No support NoSamsung Internet Android No support No
update media feature
Experimental
Chrome No support NoEdge ? Firefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
width media featureChrome Full support YesEdge Full support 12Firefox Full support 2IE Full support 9Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android ?
-moz-device-pixel-ratio media feature
DeprecatedNon-standard
Chrome No support NoEdge ? Firefox Full support 4IE No support NoOpera ? Safari ? WebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
-webkit-animation media feature
Non-standard
Chrome No support ? — 36Edge ? Firefox No support NoIE No support NoOpera No support ? — 23Safari ? WebView Android No support ? — 37Chrome Android No support ? — 36Edge Mobile ? Firefox Android No support NoOpera Android No support ? — 24Safari iOS ? Samsung Internet Android No support ? — ?
-webkit-device-pixel-ratio media feature
Non-standard
Chrome Full support YesEdge ? Firefox Full support 49
Notes Disabled
Full support 49
Notes Disabled
Notes Implemented as an alias for for -moz-device-pixel-ratio.
Disabled From version 49: this feature is behind the layout.css.prefixes.device-pixel-ratio-webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Full support 45
Notes Disabled
Notes Implemented as an alias for for -moz-device-pixel-ratio.
Disabled From version 45: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true) and the layout.css.prefixes.device-pixel-ratio-webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
-webkit-max-device-pixel-ratio media feature
Non-standard
Chrome Full support YesEdge ? Firefox Full support 49
Notes Disabled
Full support 49
Notes Disabled
Notes Implemented as an alias for for max--moz-device-pixel-ratio.
Disabled From version 49: this feature is behind the layout.css.prefixes.device-pixel-ratio-webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Full support 45
Notes Disabled
Notes Implemented as an alias for for max--moz-device-pixel-ratio.
Disabled From version 45: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true) and the layout.css.prefixes.device-pixel-ratio-webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
-webkit-min-device-pixel-ratio media feature
Non-standard
Chrome Full support YesEdge ? Firefox Full support 63
Notes
Full support 63
Notes
Notes Implemented as an alias for for -moz-device-pixel-ratio.
Full support 49
Notes Disabled
Notes Implemented as an alias for for min--moz-device-pixel-ratio.
Disabled From version 49: this feature is behind the layout.css.prefixes.device-pixel-ratio-webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Full support 45
Notes Disabled
Notes Implemented as an alias for for min--moz-device-pixel-ratio.
Disabled From version 45: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true) and the layout.css.prefixes.device-pixel-ratio-webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
-webkit-transform-2d media feature
Non-standard
Chrome No support NoEdge No support NoFirefox No support NoIE No support NoOpera Full support YesSafari Full support 1.3WebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android ? Safari iOS ? Samsung Internet Android ?
-webkit-transform-3d media feature
Non-standard
Chrome Full support YesEdge ? Firefox Full support 49
Full support 49
Full support 46
Disabled
Disabled From version 46: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support YesSafari Full support 1.3WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
-webkit-transition media feature
DeprecatedNon-standard
Chrome No support NoEdge ? Firefox No support NoIE No support NoOpera Full support YesSafari Full support 1.3WebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

更多资料

文档标签和贡献者

标签: 
最后编辑者: zjffun,