@media

 

概述

@media CSS at-rule 将一组嵌套语句关联在由大括号分隔的CSS块中,使用由media query定义的一个条件。@media 规则不仅可以在一个CSS的顶层使用,也可以在任何CSS conditional-group at-rule中使用。

@media 规则可以通过CSS对象模型接口 CSSMediaRule 访问.

语法

@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-query> 由一个可选的媒体类型加上若干媒体特性组成。

媒体类型

all
适用于所有设备。
print
为了加载合适的文档到当前使用的可视窗口. 需要提前咨询 paged media(媒体屏幕尺寸), 以满足个别设备网页尺寸不匹配等问题。
screen
主要适用于彩色的电脑屏幕
speech
解析speech这个合成器. 注意: CSS2已经有一个相似的媒体类型叫aural.可以看以下附录
Note: CSS2.1 and Media Queries 3 defined several additional media types (tty, tv, projection, handheld, braille, embossed, aural), but they were deprecated in Media Queries 4 and shouldn't be used.

媒体特性

针对每个浏览器和不同设备都有各自的媒体特性

Name Summary Notes
width 可视宽度  
height 可视高度  
aspect-ratio 视口宽高的比值  
orientation 屏幕的横屏或竖屏?  
resolution Pixel density of the output device  
scan Scanning process of the output device  
grid Is the device a grid or bitmap?  
update-frequency How quickly (if at all) can the output device modify the appearance of the 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-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 0 if the device is not monochrome.  
inverted-colors Is the user agent or underlying OS inverting colors? 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
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?  
any-hover Does any available input mechanism allow the user to hover over elements?  
light-level Current ambient light level Added in Media Queries Level 4
scripting Is scripting (e.g. JavaScript) available? Added in Media Queries Level 4
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
-webkit-device-pixel-ratio Number of physical device pixels per CSS pixel Nonstandard; WebKit/Blink-specific. If possible, use the resolution media feature instead.
-webkit-transform-3d Are CSS 3D transforms supported? Nonstandard; WebKit/Blink-specific
-webkit-transform-2d Are CSS 2D transforms supported? Nonstandard; WebKit-specific
-webkit-transition Are CSS transitions supported? Nonstandard; WebKit-specific
-webkit-animation Are CSS animations supported? Nonstandard; WebKit-specific

示例

@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-device-width: 320px)
  and (max-device-width: 480px)
  and (-webkit-min-device-pixel-ratio: 2) {
    body { line-height: 1.4 }
}

规范

Specification Status Comment
Compatibility Standard
CSS Media Queries
Living Standard Standardizes the -webkit-device-pixel-ratio and -webkit-transform-3d media features.
CSS Conditional Rules Module Level 3
@media
Candidate Recommendation Defines the basic syntax of the @media rule.
Media Queries Level 4
@media
Working Draft

Added scripting, pointer, hover, light-level, update-frequency, overflow-block, and overflow-inline media features.
Deprecated all media types except for screen, print, speech, and all.

Media Queries
@media
Recommendation No change.
CSS Level 2 (Revision 1)
@media
Recommendation Initial definition.

浏览器支持

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (all, print, screen) 1.0 1.0 (1.7 or earlier) 6.0 9.2 1.3
speech 未实现 未实现 未实现 9.2 未实现
Media features 1.0 1.0 (1.7 or earlier) 9.0 9.2 1.3
display-mode media feature ? 47 (47) ? ? ?
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (all, print, screen) 1.0 1.0 (1.7) (Yes) 9.0 3.1
speech 未实现 未实现 未实现 9.0 未实现
Media features 1.0 1.0 (1.7) (Yes) 9.0 3.1
display-mode media feature ? (Yes) ? ? ?

相关链接

文档标签和贡献者