在线文档教程
CSS

@媒体 | @media

@媒体

@media CSSat-rule可以根据一个或多个媒体查询的结果来应用样式,这些查询会测试设备的类型、特定特征和环境。

在CSS中,@media规则可以放在代码的顶层,也可以嵌套在任何其他条件组。=(conditional group at-rule.)

/* Media query */ @media screen and (min-width: 900px) { article {   padding: 1rem 3rem;   } } /* Nested media query */ @supports (display: flex) {   @media screen and (min-width: 900px) {   article {   display: flex;   } } }

除了在@media规则中应用时,媒体查询也可以应用于HTML。<link>元素将整个样式表限制为特定媒体。

<!-- Media-dependent style sheet included in HTML --> <link rel="stylesheet" media="screen and (min-width: 900px)" href="widescreen-styles.css" />

注:在JavaScript中,@media可以通过CSSMediaRuleCSS对象模型接口。

语法

@media at-rule由一个或多个媒体查询组成,每个查询都包含一个可选的媒体类型和任意数量的媒体特征表达式。可以通过使用逻辑运算符以各种方式组合多个查询,而且不区分大小写。

只有当媒体查询计算为true时,即当指定的媒体类型与显示在其上的设备类型匹配时,才会应用相应的样式。以及当 所有媒体功能表达式都计算为真时,同上。

注:即使查询返回false,带有媒体查询的样式表<link>标签仍将下载。不过,除非查询结果更改为true,否则它的内容将不适用。

媒体类型

媒体类型 描述设备的一般类别。除非您使用notonly逻辑运算符,媒体类型是可选的,all类型将被隐藏。

all适用于所有设备。print用于分页材料和在打印预览模式下在屏幕上查看的文档。请参阅传呼媒体,以及入门教程的媒体部分有关特定于分页媒体的格式化问题的信息。screen主要用于彩色电脑屏幕。speech用于语音合成器。

已弃用的媒体类型: CSS2.1和 Media Queries 3定义了一些额外的媒体类型(tty, tv, projection, handheld, braille, embossed, and aural),但它们在Media Queries 4中被弃用,不应该被使用。aural型已经被speech型取代,这是相似的。

媒体特征

媒体特征表达式的特定特性的测试用户代理、输出设备或环境。它们完全是可选的。每个媒体特性表达式必须被括号包围。

NameSummaryNotes
widthWidth of the viewport
heightHeight of the viewport
aspect-ratioWidth-to-height aspect ratio of the viewport
orientationOrientation of the viewport
resolutionPixel density of the output device
scanScanning process of the output device
gridDoes the device use a grid or bitmap screen?
updateHow frequently the output device can modify the appearance of contentAdded in Media Queries Level 4.
overflow-blockHow does the output device handle content that overflows the viewport along the block axis?Added in Media Queries Level 4.
overflow-inlineCan content that overflows the viewport along the inline axis be scrolled?Added in Media Queries Level 4.
colorNumber of bits per color component of the output device, or zero if the device isn't color
color-gamutApproximate range of colors that are supported by the user agent and output deviceAdded in Media Queries Level 4.
color-indexNumber of entries in the output device's color lookup table, or zero if the device does not use such a table
display-modeThe display mode of the application, as specified in the web app manifest's display memberDefined in the Web App Manifest spec.
monochromeBits per pixel in the output device's monochrome frame buffer, or zero if the device isn't monochrome
inverted-colorsIs the user agent or underlying OS inverting colors?Deferred to Media Queries Level 5.
pointerIs the primary input mechanism a pointing device, and if so, how accurate is it?Added in Media Queries Level 4.
hoverDoes the primary input mechanism allow the user to hover over elements?Added in Media Queries Level 4.
any-pointerIs any available input mechanism a pointing device, and if so, how accurate is it?Added in Media Queries Level 4.
any-hoverDoes any available input mechanism allow the user to hover over elements?Added in Media Queries Level 4.
light-levelLight level of the environmentDeferred to Media Queries Level 5.
scriptingIs scripting (e.g., JavaScript) available?Deferred to Media Queries Level 5.
device-widthWidth of the rendering surface of the output deviceDeprecated in Media Queries Level 4.
device-heightHeight of the rendering surface of the output deviceDeprecated in Media Queries Level 4.
device-aspect-ratioWidth-to-height aspect ratio of the output deviceDeprecated in Media Queries Level 4.

逻辑运算符

逻辑运算符not、and,和only可用于编写复杂的媒体查询。您还可以使用逗号分隔的列表将多个媒体查询组合成一个规则。

and

and 运算符用于将多个媒体特性组合到一个单独的媒体查询中,要求每个链接的特性返回true,以便查询为真。它还被用于加入媒体类型的媒体功能。

not

not运算符用于否定媒体查询,如果查询不返回false,则返回true。如果出现在逗号分隔的列表中,它只会否定应用它的特定查询。如果使用not运算符,则必须指定显式媒体类型。

注:not关键字不能用于否定单个功能表达式,仅用于整个媒体查询。

only

only运算符 仅用于整个查询匹配应用样式,并且对防止旧的浏览器应用从选定的样式很有用。如果使用not运算符,则必须指定显式媒体类型。

逗号分隔列表

逗号分隔的媒体查询列表中的每个查询,都与其他查询分开处理。如果列表中的任何查询为真,则整个媒体语句返回true。换句话说,列表的行为就像逻辑运算符or

形式语法

@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 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 (resolution: 150dpi) { body { line-height: 1.4; } }

有关更多媒体特性示例,请参见每个特定功能的参考页面。有关更多逻辑运算符示例,请参见使用媒体查询...

规范

SpecificationStatusComment
CSS Conditional Rules Module Level 3The definition of '@media' in that specification.Candidate RecommendationDefines the basic syntax of the @media rule.
Media Queries Level 4The definition of '@media' in that specification.Working DraftAdds scripting, pointer, hover, light-level, update-frequency, 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.
Media QueriesThe definition of '@media' in that specification.RecommendationNo change.
CSS Level 2 (Revision 1)The definition of '@media' in that specification.RecommendationInitial definition.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support (all, print, screen media types)1.0(Yes)1.0 (1.7 or earlier)6.09.21.3
speech media typeNo supportNo supportNo supportNo support9.2No support
Media features1.0(Yes)1.0 (1.7 or earlier)9.09.21.3
resolution media feature29?3.5 (1.9.1) 2 8.0 (8.0) 3?(Yes)?
display-mode media feature?No support47 (47)1???

FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support (all, print, screen media types)1.0(Yes)1.0 (1.7)(Yes)9.03.1
speech media typeNo supportNo supportNo supportNo support9.0No support
Media features1.0(Yes)1.0 (1.7)(Yes)9.03.1
resolution media feature??????
display-mode media feature?No support47.0 (47)1???