跳到主要内容

HTML <style> media 属性

针对两种不同媒介类型(屏幕和打印)的两个不同的样式表:

<style type="text/css" media="print">
h1 {
color: #000000;
}

p {
color: #000000;
}

body {
background-color: #FFFFFF;
}
</style>

定义和用法

media 属性规定目前资源是为何种媒介/设备优化的。

该属性大多用在为不同媒介类型规定不同样式的 CSS 样式表。

提示

该属性可以接受多个值。


HTML 4.01 与 HTML5之间的差异

无。


语法

<style media="value">

可能的运算符

描述
and规定 AND 操作符。
not规定 NOT 操作符。
,规定 OR 操作符。

设备

描述
all默认。适应所有设备。
aural语音合成器。
braille盲人用点字法反馈设备。
handheld手持设备(小屏幕、有限的带宽)。
projection放映机。
print打印预览模式 / 打印页。
screen计算机屏幕(默认值)。
tty电传打字机以及使用等宽字符网格的类似媒介。
tv电视类型设备(低分辨率、有限的屏幕翻滚能力)。

描述
width规定目标显示区域的宽度。可使用 "min-" 和 "max-" 前缀。例子:media="screen and (min-width:500px)"
height规定目标显示区域的高度。可使用 "min-" 和 "max-" 前缀。例子:media="screen and (max-height:700px)"
device-width规定目标显示器/纸张的宽度。可使用 "min-" 和 "max-" 前缀。例子:media="screen and (device-width:500px)"
device-height规定目标显示器/纸张的高度。可使用 "min-" 和 "max-" 前缀。例子:media="screen and (device-height:500px)"
orientation规定目标显示器/纸张的方向。可能的值:"portrait" or "landscape"例子:media="all and (orientation: landscape)"
aspect-ratio规定目标显示区域的宽度/高度比可使用 "min-" 和 "max-" 前缀。例子:media="screen and (aspect-ratio:16/9)"
device-aspect-ratio规定目标显示器/纸张的 device-width/device-height 比率可使用 "min-" 和 "max-" 前缀。例子:media="screen and (aspect-ratio:16/9)"
color规定目标显示器的 bits/color可使用 "min-" 和 "max-" 前缀。例子:media="screen and (color:3)"
color-index规定目标显示器可以处理的颜色数。可使用 "min-" 和 "max-" 前缀。例子:media="screen and (min-color-index:256)"
monochrome规定单色帧缓冲中的 bits/pixel。可使用 "min-" 和 "max-" 前缀。例子:media="screen and (monochrome:2)"
resolution规定目标显示器/纸张的像素密度 (dpi 或 dpcm)。可使用 "min-" 和 "max-" 前缀。例子:media="print and (resolution:300dpi)"
scan规定 tv 显示器的扫描方式。可能的值:"progressive" 和 "interlace"。例子:media="tv and (scan:interlace)"
grid规定输出设备是否是网格或位图。可能的值:"1" 为网格,否则为 "0"。例子:media="handheld and (grid:1)"

浏览器兼容性

Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
media