CSS3 latest version reference manual
/ CSS3 多媒體查詢實例
CSS3 多媒體查詢實例
本章節(jié)我們將為大家演示一些多媒體查詢實例。
開始之前我們先制作一個電子郵箱的鏈接列表。HTML 代碼如下:
實例
<!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>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
注意 data-email
屬性。在 HTML 中我們可以使用帶 data-
前綴的屬性來存儲信息。
520 到 699px 寬度 - 添加郵箱圖標
當瀏覽器的寬度在 520 到 699px, 郵箱鏈接前添加郵件圖標:
實例
<!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>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
700 到 1000px - 添加文本前綴信息
當瀏覽器的寬度在 700 到 1000px, 會在郵箱鏈接前添加 "Email: ":
實例
<!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>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
大于 1001px 寬度 - 添加郵件地址
當瀏覽器的寬度大于 1001px 時,會在鏈接后添加郵件地址接。
我們會使用 data-
屬性來為每個人名后添加郵件地址:
實例
<!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>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
大于 1151px 寬度 - 添加圖標
當瀏覽器的寬度大于 1001px 時,會在人名前添加圖標。
實例中,我們沒有編寫額外的查詢塊,我們可以在已有的查詢媒體后使用逗號分隔來添加其他媒體查詢 (類似 OR 操作符):
實例
<!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>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
更多實例
在一個網(wǎng)頁的側欄上使用郵件列表鏈接
該實例在網(wǎng)頁的左側欄添加了郵件鏈接列表。