@media 媒體類型 and (媒體特性) { 樣式 }
max-width視口 ≤ 指定值時生效。
@media screen and (max-width: 480px) {
.ads { display: none; } /* 小螢幕隱藏廣告 */
}
min-width視口 ≥ 指定值時生效。
@media screen and (min-width: 900px) {
.wrapper { width: 980px; }
}
@media screen and (min-width: 600px) and (max-width: 900px) {
body { background-color: #f5f5f5; }
}
針對實際裝置解析度(非 CSS 像素):
@media screen and (max-device-width: 480px) {
/* iPhone 等裝置載入 */
}
同樣支援 min-device-width、max-device-width。
not 否定@media not print and (max-width: 1200px) { ... }
/* 除印刷裝置且寬度 ≤ 1200px 以外的所有裝置 */
only 限定@media only screen and (max-width: 480px) { ... }
/* 僅針對支援媒體查詢的螢幕裝置,舊瀏覽器會忽略整條規則 */