CSS3 latest version reference manual
/ CSS3 多媒體查詢實(shí)例
CSS3 多媒體查詢實(shí)例
本章節(jié)我們將為大家演示一些多媒體查詢實(shí)例。
開始之前我們先制作一個(gè)電子郵箱的鏈接列表。HTML 代碼如下:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <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="johndoe@example.com" href="mailto:johndoe@example.com">John Doe</a></li> <li><a data-email="marymoe@example.com" href="mailto:marymoe@example.com">Mary Moe</a></li> <li><a data-email="amandapanda@example.com" href="mailto:amandapanda@example.com">Amanda Panda</a></li> </ul> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
注意 data-email
屬性。在 HTML 中我們可以使用帶 data-
前綴的屬性來存儲(chǔ)信息。
520 到 699px 寬度 - 添加郵箱圖標(biāo)
當(dāng)瀏覽器的寬度在 520 到 699px, 郵箱鏈接前添加郵件圖標(biāo):
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> ul { list-style-type: none; } ul li a { color: green; text-decoration: none; padding: 3px; display: block; } @media screen and (max-width: 699px) and (min-width: 520px) { ul li a { padding-left: 30px; background: url(../style/images/email-icon.png) left center no-repeat; } } </style> </head> <body> <h1>重置瀏覽器窗口,查看效果!</h1> <ul> <li><a data-email="johndoe@example.com" href="mailto:johndoe@example.com">John Doe</a></li> <li><a data-email="marymoe@example.com" href="mailto:marymoe@example.com">Mary Moe</a></li> <li><a data-email="amandapanda@example.com" href="mailto:amandapanda@example.com">Amanda Panda</a></li> </ul> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
700 到 1000px - 添加文本前綴信息
當(dāng)瀏覽器的寬度在 700 到 1000px, 會(huì)在郵箱鏈接前添加 "Email: ":
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> ul { list-style-type: none; } ul li a { color: green; text-decoration: none; padding: 3px; display: block; } @media screen and (max-width: 699px) and (min-width: 520px) { ul li a { padding-left: 30px; background: url(../style/images/email-icon.png) left center no-repeat; } } @media screen and (max-width: 1000px) and (min-width: 700px) { ul li a:before { content: "Email: "; font-style: italic; color: #666666; } } </style> </head> <body> <h1>重置瀏覽器窗口,查看效果!</h1> <ul> <li><a data-email="johndoe@example.com" href="mailto:johndoe@example.com">John Doe</a></li> <li><a data-email="marymoe@example.com" href="mailto:marymoe@example.com">Mary Moe</a></li> <li><a data-email="amandapanda@example.com" href="mailto:amandapanda@example.com">Amanda Panda</a></li> </ul> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
大于 1001px 寬度 - 添加郵件地址
當(dāng)瀏覽器的寬度大于 1001px 時(shí),會(huì)在鏈接后添加郵件地址接。
我們會(huì)使用 data-
屬性來為每個(gè)人名后添加郵件地址:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> ul { list-style-type: none; } ul li a { color: green; text-decoration: none; padding: 3px; display: block; } @media screen and (max-width: 699px) and (min-width: 520px) { ul li a { padding-left: 30px; background: url(../style/images/email-icon.png) left center no-repeat; } } @media screen and (max-width: 1000px) and (min-width: 700px) { ul li a:before { content: "Email: "; font-style: italic; color: #666666; } } @media screen and (min-width: 1001px) { ul li a:after { content: " (" attr(data-email) ")"; font-size: 12px; font-style: italic; color: #666666; } } </style> </head> <body> <h1>重置瀏覽器窗口,查看效果!</h1> <ul> <li><a data-email="johndoe@example.com" href="mailto:johndoe@example.com">John Doe</a></li> <li><a data-email="marymoe@example.com" href="mailto:marymoe@example.com">Mary Moe</a></li> <li><a data-email="amandapanda@example.com" href="mailto:amandapanda@example.com">Amanda Panda</a></li> </ul> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
大于 1151px 寬度 - 添加圖標(biāo)
當(dāng)瀏覽器的寬度大于 1001px 時(shí),會(huì)在人名前添加圖標(biāo)。
實(shí)例中,我們沒有編寫額外的查詢塊,我們可以在已有的查詢媒體后使用逗號(hào)分隔來添加其他媒體查詢 (類似 OR 操作符):
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> ul { list-style-type: none; } ul li a { color: green; text-decoration: none; padding: 3px; display: block; } @media screen and (max-width: 699px) and (min-width: 520px), (min-width: 1151px) { ul li a { padding-left: 30px; background: url(../style/images/email-icon.png) left center no-repeat; } } @media screen and (max-width: 1000px) and (min-width: 700px) { ul li a:before { content: "Email: "; font-style: italic; color: #666666; } } @media screen and (min-width: 1001px) { ul li a:after { content: " (" attr(data-email) ")"; font-size: 12px; font-style: italic; color: #666666; } } </style> </head> <body> <h1>重置瀏覽器窗口,查看效果!</h1> <ul> <li><a data-email="johndoe@example.com" href="mailto:johndoe@example.com">John Doe</a></li> <li><a data-email="marymoe@example.com" href="mailto:marymoe@example.com">Mary Moe</a></li> <li><a data-email="amandapanda@example.com" href="mailto:amandapanda@example.com">Amanda Panda</a></li> </ul> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
更多實(shí)例
在一個(gè)網(wǎng)頁的側(cè)欄上使用郵件列表鏈接
該實(shí)例在網(wǎng)頁的左側(cè)欄添加了郵件鏈接列表。