Ethan Marcotte 2010 年提出的三大支柱:
max-width: 100%; height: auto; 防止溢出核心理念:單一代碼庫,適配所有裝置,而非為每種裝置維護獨立版本。
@media [媒體類型] [and|not|only] (媒體特性) { CSS 規則 }
screen:螢幕(預設)print:列印speech:螢幕閱讀器all:所有類型| 特性 | 說明 | 單位/值 | 範例 |
|---|---|---|---|
width / height | 視口寬/高 | px, rem, em | (min-width: 768px) |
device-width / device-height | 螢幕物理寬/高 | px | (max-device-width: 480px)(棄用,改用 width) |
orientation | 裝置方向 | portrait | landscape | (orientation: landscape) |
aspect-ratio | 視口寬高比 | ratio | (min-aspect-ratio: 16/9) |
device-aspect-ratio | 螢幕物理寬高比 | ratio | (棄用) |
resolution | 裝置像素密度 | dpi, dppx | (min-resolution: 2dppx) 視網膜屏 |
color / color-index | 色深/色表大小 | integer | (color: 8) |
pointer | 主要指向裝置精度 | none | coarse | fine | (pointer: coarse) 觸控裝置 |
hover | 主要指向裝置懸停能力 | none | hover | (hover: none) 觸控裝置 |
prefers-reduced-motion | 用戶偏好減少動畫 | reduce | no-preference | (prefers-reduced-motion: reduce) |
prefers-color-scheme | 用戶偏好色系 | light | dark | (prefers-color-scheme: dark) |
prefers-contrast | 用戶偏好對比度 | more | less | custom | (prefers-contrast: more) |
❌ 裝置導向:@media (max-width: 320px) /* iPhone SE */
✅ 內容導向:在版面「破裂」的寬度加入斷點
/* 典型內容導向斷點 */
@media (max-width: 1200px) { /* 大桌機:側邊欄收合 */ }
@media (max-width: 992px) { /* 筆電/平板橫向:導航改漢堡選單 */ }
@media (max-width: 768px) { /* 平板直向:雙欄變單欄 */ }
@media (max-width: 576px) { /* 手機:字體縮小、間距壓縮 */ }
@media (max-width: 380px) { /* 小螢幕手機:極致精簡 */ }
:root {
--space-xs: 0.25rem; --space-sm: 0.5rem; --space-md: 1rem; --space-lg: 1.5rem; --space-xl: 2rem;
--font-size-base: 1rem; --line-height-base: 1.6;
--container-max: 1200px;
--color-primary: #3498db; --color-bg: #fff; --color-text: #1a1a1a;
}
@media (max-width: 768px) {
:root { --space-md: 0.75rem; --font-size-base: 0.9rem; --container-max: 100%; }
}
.container { max-width: var(--container-max); margin: 0 auto; padding: 0 var(--space-md); }
body { font-size: var(--font-size-base); line-height: var(--line-height-base); }
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); /* 自動填充,無需媒體查詢 */
gap: var(--space-md);
}
.navbar {
display: flex; justify-content: space-between; align-items: center; gap: var(--space-md);
flex-wrap: wrap; /* 空間不足自動換行 */
}
.card-container { container-type: inline-size; }
@container (min-width: 400px) {
.card { display: flex; flex-direction: row; }
.card img { width: 40%; }
}
@container (max-width: 399px) {
.card { display: block; }
.card img { width: 100%; height: 200px; object-fit: cover; }
}
元件根據父容器寬度而非視口寬度調整,實現真正的可複用元件。
clamp() 流體排版:無斷點字體縮放h1 { font-size: clamp(2rem, 5vw + 1rem, 4rem); } /* 最小 2rem、理想 5vw+1rem、最大 4rem */
.box {
margin-inline-start: 1rem; /* 替代 margin-left */
padding-inline-end: 2rem; /* 替代 padding-right */
border-inline-start: 2px solid; /* 替代 border-left */
inset-inline-start: 0; /* 替代 left: 0 */
}
<picture>
<source type="image/avif" srcset="hero-400.avif 400w, hero-800.avif 800w, hero-1200.avif 1200w">
<source type="image/webp" srcset="hero-400.webp 400w, hero-800.webp 800w, hero-1200.webp 1200w">
<img
src="hero-800.jpg"
srcset="hero-400.jpg 400w, hero-800.jpg 800w, hero-1200.jpg 1200w"
sizes="(max-width: 480px) 100vw, (max-width: 1024px) 50vw, 800px"
alt="產品主視覺"
width="1200" height="675"
loading="lazy"
>
</picture>
npm i -D stylelint stylelint-config-responsive:CI 中強制響應式規範| 症狀 | 原因 | 修正 |
|---|---|---|
| 手機出現橫向滾動條 | 固定寬度元素、width: 100vw 包含捲軸寬度、負 margin 溢出 | max-width: 100%、overflow-x: hidden(謹慎)、改用 width: 100% |
| 字體在手機太小/太大 | 未設 viewport、用 px 固定字體、未用 clamp() | 加 viewport meta、改 rem/clamp、設定 text-size-adjust: 100% |
| 圖片模糊/變形 | 無 width/height 導致 CLS、未提供高 DPI 版本、object-fit 缺失 | 標註原始尺寸、picture/srcset 提供 2x/3x、object-fit: cover |
| 導航選單手機無法展開 | JS 依賴 hover、z-index 層級錯誤、焦點陷阱缺失 | 用 pointer: coarse 媒體查詢調整、語義化 <button> 控制、Focus Trap |
響應式佈局不再是「寫一堆 Media Queries」,而是流式網格 + 彈性媒體 + 容器查詢 + 流體排版 + 邏輯屬性 + 現代圖片語法的系統性工程。掌握這套工具鏈,你能用最少代碼、最高維護性、最佳體驗,支撐從智慧手錶到 8K 電視的所有裝置。