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= "mailto:[email protected]" > John Doe < /a > < /li >
< li > < a data-email= "[email protected]" href= "mailto:[email protected]" > Mary Moe < /a > < /li >
< li > < a data-email= "[email protected]" href= "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= "mailto:[email protected]" > John Doe < /a > < /li >
< li > < a data-email= "[email protected]" href= "mailto:[email protected]" > Mary Moe < /a > < /li >
< li > < a data-email= "[email protected]" href= "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;
}
}
嘗試一下»
更多實例
在一個網頁的側欄上使用郵件列錶鍊接
該實例在網頁的左側欄添加了郵件鏈接列表。