CSS 容器查詢徹底指南:元件級響應式設計的終極方案

前端工程(更新於 2026年6月2日)

容器查詢:響應式設計的範式轉移

傳統媒體查詢基於視口寬度,容器查詢基於父容器寬度——這看似微小的差異,徹底改變了元件的複用方式。

維度 媒體查詢 容器查詢
參照物 視口(viewport) 父容器(container)
複用性 元件依賴頁面佈局 元件完全獨立
側邊欄場景 需額外斷點 自動適配
巢狀元件 無法感知父級 天然支援
瀏覽器支援 全部 Chrome 105+, Firefox 110+, Safari 16+

基礎語法

定義容器

.card-container {
  container-type: inline-size;
  container-name: card;
}

/* 簡寫 */
.card-container {
  container: card / inline-size;
}

container-type 取值:

含義 可查詢維度
inline-size 行內方向容器 寬度
size 雙軸容器 寬度 + 高度
normal 非容器(預設)

編寫容器查詢

@container card (min-width: 400px) {
  .card {
    flex-direction: row;
  }
  .card-title {
    font-size: 1.5rem;
  }
}

@container card (max-width: 399px) {
  .card {
    flex-direction: column;
  }
  .card-title {
    font-size: 1.2rem;
  }
}

實戰案例:自適應卡片元件

HTML 結構

<div class="sidebar">
  <div class="card-container">
    <article class="card">
      <img class="card-img" src="photo.jpg" alt="">
      <div class="card-body">
        <h3 class="card-title">文章標題</h3>
        <p class="card-desc">文章摘要內容</p>
        <span class="card-tag">前端</span>
      </div>
    </article>
  </div>
</div>

<div class="main-content">
  <div class="card-container">
    <article class="card">
      <!-- 同樣的卡片元件 -->
    </article>
  </div>
</div>

CSS 實作

.card-container {
  container: card / inline-size;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1rem;
}

.card-img {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  border-radius: 0.5rem;
}

/* 容器寬度 > 400px:水平佈局 */
@container card (min-width: 400px) {
  .card {
    flex-direction: row;
    align-items: center;
  }

  .card-img {
    width: 200px;
    aspect-ratio: 1;
    flex-shrink: 0;
  }

  .card-title {
    font-size: 1.25rem;
  }
}

/* 容器寬度 > 600px:大卡片佈局 */
@container card (min-width: 600px) {
  .card-img {
    width: 280px;
  }

  .card-title {
    font-size: 1.5rem;
  }

  .card-desc {
    -webkit-line-clamp: 3;
  }
}

效果:同一個 .card 元件,放在 300px 側邊欄自動豎排,放在 700px 主區域自動橫排——無需任何媒體查詢。


容器查詢單位

類似 vw/vh,但參照容器而非視口:

單位 參照 等價關係
cqw 容器寬度的 1% 1cqw = 容器寬度 × 1%
cqh 容器高度的 1% 1cqh = 容器高度 × 1%
cqi 容器行內尺寸的 1% 等於 cqw(水平書寫模式)
cqb 容器塊尺寸的 1% 等於 cqh(水平書寫模式)
cqmin min(cqi, cqb) 取較小值
cqmax max(cqi, cqb) 取較大值
.card-title {
  font-size: clamp(1rem, 3cqw, 2rem);
  padding: 2cqw;
}

實戰案例:響應式導航欄

.nav-container {
  container: nav / inline-size;
}

.nav {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* 窄容器:漢堡選單 */
@container nav (max-width: 500px) {
  .nav-links {
    display: none;
  }
  .nav-hamburger {
    display: block;
  }
}

/* 中等容器:水平導航 */
@container nav (min-width: 501px) {
  .nav-links {
    display: flex;
    gap: 1rem;
  }
  .nav-hamburger {
    display: none;
  }
}

/* 寬容器:導航 + 搜尋框 */
@container nav (min-width: 800px) {
  .nav-search {
    display: flex;
  }
}

與媒體查詢的協同

容器查詢不是替代媒體查詢,而是分工協作

/* 媒體查詢:處理頁面級佈局 */
@media (min-width: 1024px) {
  .layout {
    grid-template-columns: 280px 1fr;
  }
}

/* 容器查詢:處理元件級佈局 */
@container card (min-width: 400px) {
  .card {
    flex-direction: row;
  }
}
場景 用什麼
頁面整體佈局切換 媒體查詢
元件內部自適應 容器查詢
全域字型縮放 媒體查詢
元件內文字隨容器縮放 容器查詢單位

容器樣式查詢

CSS Container Query Level 3 引入了樣式查詢,可以基於容器自訂屬性值來切換樣式:

.card-container {
  container-type: inline-size;
  --theme: light;
}

.card-container.dark {
  --theme: dark;
}

@container style(--theme: dark) {
  .card {
    background: #1a1a2e;
    color: #e0e0e0;
  }
}

@container style(--theme: light) {
  .card {
    background: #ffffff;
    color: #333333;
  }
}

效能最佳化

避免過度巢狀

/* 不推薦:多層容器巢狀 */
.outer { container: outer / inline-size; }
.middle { container: middle / inline-size; }
.inner { container: inner / inline-size; }

/* 推薦:只在需要自適應的層級設定容器 */
.card-wrapper { container: card / inline-size; }

容器尺寸計算

容器查詢的尺寸計算不包含子元素的溢出內容,但包含 padding 和 border。設定 box-sizing: border-box 確保一致行為。

降級策略

/* 基礎樣式:行動端優先 */
.card {
  flex-direction: column;
}

/* 容器查詢增強 */
@supports (container-type: inline-size) {
  .card-container {
    container: card / inline-size;
  }
  @container card (min-width: 400px) {
    .card {
      flex-direction: row;
    }
  }
}

/* 不支援時的媒體查詢回退 */
@supports not (container-type: inline-size) {
  @media (min-width: 768px) {
    .card {
      flex-direction: row;
    }
  }
}

瀏覽器相容性(2026 年)

瀏覽器 容器查詢 容器查詢單位 樣式查詢
Chrome 105+ ✅ (117+)
Firefox 110+
Safari 16+ ✅ (17+)
Edge 105+

結論:容器查詢已獲得主流瀏覽器全面支援,可以在生產環境放心使用。樣式查詢作為增強功能,建議配合 @supports 漸進使用。

本站提供瀏覽器本地工具,免註冊即可試用 →

#CSS#容器查询#响应式设计#组件化