同一網站需同時適配桌面大螢幕與手機小螢幕。CSS3 Media Queries 讓我們在不改 HTML、不靠 JS 的前提下,根據裝置特性(寬度、高度、方向、解析度)切換佈局與樣式。
width/height)device-width/device-height)orientation: portrait | landscape)resolution / -webkit-device-pixel-ratio)桌面版:兩欄佈局、大尺寸 Header 圖片。
手機版:單欄佈局、縮小 Header 圖片、導航收合。


/* 桌面版基礎樣式(手機優先寫法則反過來) */
#wrapper { width: 960px; margin: 0 auto; }
#header { height: 180px; background: url(header-lg.jpg) no-repeat center; }
#content { float: left; width: 640px; }
#navigation { float: right; width: 300px; }
/* 手機專用覆蓋樣式(置於 CSS 末尾或獨立檔案) */
@media only screen and (max-device-width: 480px) {
#wrapper { width: 400px; }
#header {
background-image: url(header-sm.jpg);
height: 93px;
position: relative;
}
#header h1 { font-size: 140%; }
#content { float: none; width: 100%; }
#navigation { float: none; width: auto; }
}
<link rel="stylesheet" type="text/css"
media="only screen and (max-device-width: 480px)"
href="small-device.css" />
優點:大型專案職責分離,手機樣式獨立維護,桌面瀏覽器不下載手機 CSS(部分舊瀏覽器仍會下載但不應用)。
輸入網址即可模擬 iPhone、iPad、Android 等裝置顯示效果,支援自訂視窗尺寸測試 Media Queries 觸發點。

/* 在桌面瀏覽器縮放視窗也能觸發測試 */
@media only screen and (max-width: 480px),
only screen and (max-device-width: 480px) { ... }
max-width 針對視窗寬度(桌面測試用),max-device-width 針對裝置螢幕寬度(實機用)。上線前建議移除 max-width,避免桌面用戶縮放視窗時誤觸發手機版佈局。
<link rel="stylesheet" type="text/css"
media="only screen and (max-width: 480px), only screen and (max-device-width: 480px)"
href="/assets/css/small-device.css" />
body { background-image: url(/img/small-bg.png); }
#wrapper {
width: auto; margin: auto; text-align: left;
background-image: url(/img/small-logo.png);
background-position: left 5px; background-repeat: no-repeat;
min-height: 400px;
}
float: none; width: auto;.article #aside {
float: none;
width: auto;
}
/* 減少 margin/padding,增大點擊區域 */
#nav a { padding: 12px 16px; display: block; }
.content { padding: 1rem; line-height: 1.6; }
min-width 斷點:基礎樣式即手機版,逐步增強。@media (min-width: 30em) 而非 480px。@container (min-width: 400px) 讓元件根據父容器適配,解決頁面級 Media Queries 侷限性。<picture> + srcset + sizes 配合 CSS object-fit。