跳到主要内容

HTML <a> media 属性

以下实例展示了使用media属性的链接:

<a href="/html/element/att-a-media?output=print" media="print and (resolution:300dpi)">可打印的媒体属性页</a>

定义和用法

media 属性规定目标 URL 是为什么类型的媒介/设备进行优化的。

该属性用于规定目标 URL 是为特殊设备(比如 iPhone)、语音或打印媒介设计的。

该属性可接受多个值。

只能在 href 属性存在时使用。


HTML 4.01 与 HTML5 之间的差异

该属性是 HTML5 中的新属性。


语法

<a 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" 或 "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 per color。可使用 "min-" 和 "max-" 前缀。例子:media="screen and (color:3)"
color-index规定目标显示器能够处理的颜色数。可使用 "min-" 和 "max-" 前缀。例子:media="screen and (min-color-index:256)"
monochrome规定在单色帧缓冲中的每像素比特。可使用 "min-" 和 "max-" 前缀。例子:media="screen and (monochrome:2)"
resolution规定目标显示器/纸张的像素密度 (dpi or 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