扫码一下
查看教程更方便
本章节我们将为大家演示一些多媒体查询实例。
开始之前我们先制作一个电子邮箱的链接列表。html 代码如下:
注意: data-email 属性。在 html 中我们可以使用带 data- 前缀的属性来存储信息。
当浏览器的宽度在 520 到 699px, 邮箱链接前添加邮件图标:
@media screen and (max-width: 699px) and (min-width: 520px) { ul li a { padding-left: 30px; background: left center no-repeat; } }
当浏览器的宽度在 700 到 1000px, 会在邮箱链接前添加 "email: ":
@media screen and (max-width: 1000px) and (min-width: 700px) { ul li a:before { content: "email: "; font-style: italic; color: #666666; } }
当浏览器的宽度大于 1001px 时,会在链接后添加邮件地址接。
我们会使用 data- 属性来为每个人名后添加邮件地址:
@media screen and (min-width: 1001px) { ul li a:after { content: " (" attr(data-email) ")"; font-size: 12px; font-style: italic; color: #666666; } }
当浏览器的宽度大于 1001px 时,会在人名前添加图标。
实例中,我们没有编写额外的查询块,我们可以在已有的查询媒体后使用逗号分隔来添加其他媒体查询 (类似 or 操作符):
@media screen and (max-width: 699px) and (min-width: 520px), (min-width: 1151px) { ul li a { padding-left: 30px; background: left center no-repeat; } }
该实例在网页的左侧栏添加了邮件链接列表。