CSS3 多媒體查詢例項
本章節我們將為大家演示一些多媒體查詢例項。
開始之前我們先製作一個電子郵箱的連結列表。HTML 程式碼如下:
例項 1
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
}
ul li a {
color: green;
text-decoration: none;
padding: 3px;
display: block;
}
</style>
</head>
<body>
<ul>
<li><a data-email="[email protected]" href=https://www.itread01.com/css3/"mailto:[email protected]">John Doe</a></li>
<li><a data-email="[email protected]" href=https://www.itread01.com/css3/"mailto:[email protected]">Mary Moe</a></li>
<li><a data-email="[email protected]" href=https://www.itread01.com/css3/"mailto:[email protected]">Amanda Panda</a></li>
</ul>
</body>
</html>
<html>
<head>
<style>
ul {
list-style-type: none;
}
ul li a {
color: green;
text-decoration: none;
padding: 3px;
display: block;
}
</style>
</head>
<body>
<ul>
<li><a data-email="[email protected]" href=https://www.itread01.com/css3/"mailto:[email protected]">John Doe</a></li>
<li><a data-email="[email protected]" href=https://www.itread01.com/css3/"mailto:[email protected]">Mary Moe</a></li>
<li><a data-email="[email protected]" href=https://www.itread01.com/css3/"mailto:[email protected]">Amanda Panda</a></li>
</ul>
</body>
</html>
嘗試一下 ?
注意 data-email
屬性。在 HTML 中我們可以使用帶 data-
字首的屬性來儲存資訊。
520 到 699px 寬度 - 新增郵箱圖示
當瀏覽器的寬度在 520 到 699px, 郵箱連結前新增郵件圖示:
例項 2
@media screen and (max-width: 699px) and (min-width: 520px) {
ul li a {
padding-left: 30px;
background: url(email-icon.png) left center no-repeat;
}
}
ul li a {
padding-left: 30px;
background: url(email-icon.png) left center no-repeat;
}
}
嘗試一下 ?
700 到 1000px - 新增文字字首資訊
當瀏覽器的寬度在 700 到 1000px, 會在郵箱連結前新增 "Email: ":
例項 3
@media screen and (max-width: 1000px) and (min-width: 700px) {
ul li a:before {
content: "Email: ";
font-style: italic;
color: #666666;
}
}
ul li a:before {
content: "Email: ";
font-style: italic;
color: #666666;
}
}
嘗試一下 ?
大於 1001px 寬度 - 新增郵件地址
當瀏覽器的寬度大於 1001px 時,會在連結後新增郵件地址接。
我們會使用 data-
屬性來為每個人名後新增郵件地址:
例項 4
@media screen and (min-width: 1001px) {
ul li a:after {
content: " (" attr(data-email) ")";
font-size: 12px;
font-style: italic;
color: #666666;
}
}
ul li a:after {
content: " (" attr(data-email) ")";
font-size: 12px;
font-style: italic;
color: #666666;
}
}
嘗試一下 ?
大於 1151px 寬度 - 新增圖示
當瀏覽器的寬度大於 1001px 時,會在人名前新增圖示。
例項中,我們沒有編寫額外的查詢塊,我們可以在已有的查詢媒體後使用逗號分隔來新增其他媒體查詢 (類似 OR 操作符):
例項 5
@media screen and (max-width: 699px) and (min-width: 520px), (min-width:
1151px) {
ul li a {
padding-left: 30px;
background: url(email-icon.png) left center no-repeat;
}
}
ul li a {
padding-left: 30px;
background: url(email-icon.png) left center no-repeat;
}
}
嘗試一下 ?

更多例項
在一個網頁的側欄上使用郵件列表連結
該例項在網頁的左側欄添加了郵件連結列表。