容器查询 - 组件级响应式设计

第7章: 容器查询 - 组件级响应式设计

🎯 本章重点

  • 容器查询基础概念
  • 与媒体查询的区别与优势
  • 实际应用场景和最佳实践
  • 浏览器兼容性处理

📖 内容概述

7.1 容器查询介绍

7.1.1 什么是容器查询

容器查询(Container Queries)允许组件根据其容器尺寸而非视口尺寸来调整样式,实现真正的组件级响应式设计。

7.1.2 解决的问题
  • 媒体查询的局限性: 只能基于视口尺寸
  • 组件独立性: 组件可以在不同容器中自适应
  • 布局灵活性: 组件无需知道外部布局结构

7.2 基础语法

7.2.1 定义容器
css 复制代码
/* 创建容器上下文 */
.component-container {
  container-type: inline-size;
  container-name: main-container;
}

/* 简写形式 */
.component-container {
  container: main-container / inline-size;
}

/* 多个容器属性 */
.component-container {
  container-type: size;        /* 支持尺寸查询 */
  container-name: card-layout; /* 容器名称 */
}
7.2.2 容器类型
  • inline-size: 只查询内联方向尺寸(水平方向)
  • size: 查询两个方向的尺寸(水平和垂直)
  • normal: 不创建容器上下文(默认)
7.2.3 容器查询语法
css 复制代码
@container main-container (min-width: 400px) {
  .component {
    /* 当容器宽度 ≥ 400px 时的样式 */
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}

@container (max-width: 300px) {
  .component {
    /* 当容器宽度 ≤ 300px 时的样式 */
    flex-direction: column;
  }
}

7.3 实际应用案例

7.3.1 卡片组件
css 复制代码
.card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 16px;
  background: white;
}

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

/* 小尺寸卡片 */
@container card (max-width: 300px) {
  .card {
    display: flex;
    flex-direction: column;
    text-align: center;
  }
  
  .card-image {
    width: 100%;
    height: 120px;
    object-fit: cover;
  }
  
  .card-title {
    font-size: 1rem;
    margin: 8px 0;
  }
  
  .card-description {
    display: none;
  }
}

/* 中等尺寸卡片 */
@container card (min-width: 301px) and (max-width: 500px) {
  .card {
    display: grid;
    grid-template-areas:
      "image title"
      "image description"
      "button button";
    grid-template-columns: 100px 1fr;
    gap: 12px;
  }
  
  .card-image {
    grid-area: image;
    width: 100%;
    height: 80px;
    object-fit: cover;
    border-radius: 4px;
  }
  
  .card-title {
    grid-area: title;
    font-size: 1.1rem;
  }
  
  .card-description {
    grid-area: description;
    font-size: 0.9rem;
    color: #666;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  
  .card-button {
    grid-area: button;
  }
}

/* 大尺寸卡片 */
@container card (min-width: 501px) {
  .card {
    display: grid;
    grid-template-areas:
      "image title button"
      "image description button";
    grid-template-columns: 150px 1fr auto;
    grid-template-rows: auto 1fr;
    gap: 16px;
  }
  
  .card-image {
    grid-area: image;
    width: 100%;
    height: 120px;
    object-fit: cover;
    border-radius: 6px;
  }
  
  .card-title {
    grid-area: title;
    font-size: 1.2rem;
    margin: 0;
  }
  
  .card-description {
    grid-area: description;
    font-size: 1rem;
    line-height: 1.4;
  }
  
  .card-button {
    grid-area: button;
    align-self: center;
  }
}
7.3.2 导航组件
css 复制代码
.nav-container {
  container-type: inline-size;
  container-name: navigation;
}

.navigation {
  display: flex;
  background: #2c3e50;
  padding: 0 20px;
}

/* 小尺寸导航 - 汉堡菜单 */
@container navigation (max-width: 600px) {
  .navigation {
    justify-content: space-between;
    padding: 0 16px;
    height: 60px;
  }
  
  .nav-logo {
    font-size: 1.2rem;
    color: white;
  }
  
  .nav-menu {
    display: none;
    position: absolute;
    top: 60px;
    left: 0;
    right: 0;
    background: #34495e;
    flex-direction: column;
    padding: 16px;
  }
  
  .nav-menu.open {
    display: flex;
  }
  
  .nav-item {
    padding: 12px 0;
    border-bottom: 1px solid #4a6278;
  }
  
  .hamburger {
    display: block;
    color: white;
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
  }
}

/* 大尺寸导航 - 水平菜单 */
@container navigation (min-width: 601px) {
  .navigation {
    justify-content: space-between;
    align-items: center;
    height: 70px;
  }
  
  .nav-logo {
    font-size: 1.5rem;
    color: white;
    font-weight: bold;
  }
  
  .nav-menu {
    display: flex;
    gap: 30px;
    list-style: none;
    margin: 0;
    padding: 0;
  }
  
  .nav-item {
    color: white;
    text-decoration: none;
    padding: 8px 16px;
    border-radius: 4px;
    transition: background-color 0.2s;
  }
  
  .nav-item:hover {
    background-color: #34495e;
  }
  
  .hamburger {
    display: none;
  }
}
7.3.3 表单组件
css 复制代码
.form-container {
  container-type: inline-size;
  container-name: form;
}

.form-group {
  margin-bottom: 20px;
}

/* 小尺寸表单 - 垂直布局 */
@container form (max-width: 400px) {
  .form-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }
  
  .form-label {
    font-weight: bold;
    font-size: 0.9rem;
  }
  
  .form-input {
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 1rem;
  }
  
  .form-button {
    width: 100%;
    padding: 12px;
    background: #3498db;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 1rem;
  }
}

/* 大尺寸表单 - 水平布局 */
@container form (min-width: 401px) {
  .form-group {
    display: grid;
    grid-template-columns: 120px 1fr;
    gap: 16px;
    align-items: center;
  }
  
  .form-label {
    text-align: right;
    font-weight: bold;
  }
  
  .form-input {
    padding: 12px;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 1rem;
  }
  
  .form-button {
    grid-column: 2;
    justify-self: start;
    padding: 12px 24px;
    background: #3498db;
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
  }
}

7.4 高级特性

7.4.1 容器单位
css 复制代码
.component {
  /* 使用容器相对单位 */
  font-size: clamp(1rem, 5cqi, 2rem);
  padding: clamp(1rem, 10cqi, 2rem);
  gap: clamp(0.5rem, 2cqi, 1rem);
}

/* 可用单位 */
.example {
  width: 50cqi;    /* 容器内联尺寸的50% */
  height: 30cqb;   /* 容器块尺寸的30% */
  font-size: 5cqi;  /* 容器内联尺寸的5% */
  padding: 10cqmin; /* 容器最小尺寸的10% */
  margin: 5cqmax;   /* 容器最大尺寸的5% */
}
7.4.2 嵌套容器查询
css 复制代码
.layout-container {
  container-type: inline-size;
  container-name: layout;
}

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

/* 外层容器查询 */
@container layout (min-width: 800px) {
  .card-container {
    /* 在大布局中调整卡片容器 */
    container-type: size;
  }
}

/* 内层容器查询 */
@container card (min-width: 300px) and (min-height: 200px) {
  .card {
    /* 基于卡片容器尺寸的样式 */
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  }
}
7.4.3 容器查询与CSS变量
css 复制代码
:root {
  --card-padding: 16px;
  --card-gap: 12px;
}

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

@container card (min-width: 400px) {
  .card {
    --card-padding: 24px;
    --card-gap: 20px;
    --image-size: 120px;
  }
}

@container card (min-width: 600px) {
  .card {
    --card-padding: 32px;
    --card-gap: 24px;
    --image-size: 160px;
  }
}

.card {
  padding: var(--card-padding);
  gap: var(--card-gap);
}

.card-image {
  width: var(--image-size, 80px);
  height: var(--image-size, 80px);
}

7.5 性能优化

7.5.1 避免过度使用
css 复制代码
/* 好的做法:合理的断点 */
@container (min-width: 300px) { /* ... */ }
@container (min-width: 600px) { /* ... */ }
@container (min-width: 900px) { /* ... */ }

/* 避免的做法:过多断点 */
@container (min-width: 100px) { /* ... */ }
@container (min-width: 200px) { /* ... */ }
@container (min-width: 300px) { /* ... */ }
/* ... 太多类似的查询 */
7.5.2 使用容器单位
css 复制代码
/* 使用容器单位替代多个查询 */
.component {
  /* 替代多个min-width查询 */
  font-size: clamp(1rem, 4cqi, 1.5rem);
  padding: clamp(1rem, 5cqi, 2rem);
}

/* 响应式间距 */
.spacing {
  gap: clamp(0.5rem, 2cqi, 1.5rem);
}

7.6 浏览器兼容性

7.6.1 特性检测
css 复制代码
/* 现代浏览器支持 */
@supports (container-type: inline-size) {
  .component-container {
    container-type: inline-size;
  }
  
  @container (min-width: 400px) {
    .component {
      /* 容器查询样式 */
    }
  }
}

/* 传统浏览器回退 */
@supports not (container-type: inline-size) {
  .component {
    /* 基于视口的回退样式 */
    max-width: 400px;
  }
  
  @media (min-width: 768px) {
    .component {
      /* 媒体查询替代 */
    }
  }
}
7.6.2 JavaScript检测
javascript 复制代码
// 检测容器查询支持
if (CSS.supports('container-type', 'inline-size')) {
  console.log('容器查询支持');
} else {
  console.log('容器查询不支持,使用回退方案');
  // 添加回退类
  document.documentElement.classList.add('no-container-queries');
}
css 复制代码
/* 回退样式 */
.no-container-queries .component {
  /* 传统响应式设计 */
  max-width: 400px;
}

.no-container-queries .component--large {
  /* 手动控制的大尺寸样式 */
}

7.7 最佳实践

  1. 语义化命名: 使用有意义的容器名称
  2. 合理断点: 基于内容需求设置断点
  3. 性能意识: 避免不必要的容器查询
  4. 渐进增强: 提供适当的回退方案
  5. 测试覆盖: 在不同容器尺寸下测试组件

💡 实战技巧

  • 使用容器查询实现真正的组件独立性
  • 结合CSS变量创建灵活的组件系统
  • 利用容器单位实现流畅的尺寸变化
  • 为不支持的环境提供优雅降级

🎯 下一章预览

下一章将探索CSS网格布局的高级技巧,包括子网格、自动布局算法和复杂网格模式。


最后更新: 2024年12月

相关推荐
Python私教3 小时前
React + Ant Design + Tailwind CSS 打造「无痕」垂直滚动区域:功能全上,滚动条隐身
前端·css·react.js
PFinal社区_南丞4 小时前
Flexbox布局实战指南
css
PFinal社区_南丞4 小时前
CSS3简介和历史发展
css
PFinal社区_南丞4 小时前
CSS选择器
css
Strawberry_rabbit5 小时前
路由配置中的svg图标如何匹配
前端·css
千码君20161 天前
React Native:关于react自定义css属性的位置
css·react native·react.js·前端框架·ecmascript·组件嵌套
超级大只老咪1 天前
字段行居中(HTML基础语法)
前端·css·html
三月暖阳1 天前
分享并记录日常开发中的一些CSS布局和样式技巧(持续更新)
css·scss
汤姆Tom1 天前
现代 CSS 架构与组件化:构建可扩展的样式系统
前端·css