普通视图

发现新文章,点击刷新页面。
昨天以前首页

现代CSS开发环境搭建

2025年10月11日 10:56

第2章: 现代CSS开发环境搭建

🎯 本章重点

  • 现代前端工具链配置
  • PostCSS和预处理器的使用
  • 开发工作流优化

📖 内容概述

2.1 基础开发环境

代码编辑器配置 (VS Code)
// .vscode/settings.json
{
  "css.validate": false,
  "less.validate": false,
  "scss.validate": false,
  "editor.formatOnSave": true,
  "files.associations": {
    "*.css": "css"
  }
}
推荐扩展
  • PostCSS Language Support
  • Auto Rename Tag
  • CSS Peek
  • Live Server

2.2 构建工具配置

package.json 依赖
{
  "devDependencies": {
    "postcss": "^8.4.0",
    "postcss-preset-env": "^7.0.0",
    "autoprefixer": "^10.4.0",
    "cssnano": "^5.0.0",
    "vite": "^3.0.0"
  }
}

2.3 PostCSS 配置

postcss.config.js
module.exports = {
  plugins: [
    require('postcss-preset-env')({
      stage: 3,
      features: {
        'nesting-rules': true,
        'custom-media-queries': true,
        'media-query-ranges': true
      }
    }),
    require('autoprefixer'),
    require('cssnano')({
      preset: 'default'
    })
  ]
}

2.4 现代CSS工作流

开发脚本
{
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "css:watch": "postcss src/**/*.css --dir dist --watch"
  }
}

2.5 浏览器兼容性处理

.browserslistrc
last 2 versions
> 1%
not dead
not ie 11
自动前缀示例
/* 输入 */
.container {
  display: grid;
  gap: 20px;
}

/* 输出 */
.container {
  display: -ms-grid;
  display: grid;
  -ms-grid-gap: 20px;
  gap: 20px;
}

2.6 开发服务器配置

vite.config.js
import { defineConfig } from 'vite'

export default defineConfig({
  server: {
    port: 3000,
    open: true
  },
  css: {
    postcss: './postcss.config.js'
  }
})

💡 最佳实践

  1. 使用CSS原生特性 替代预处理器
  2. 配置PostCSS 处理兼容性和优化
  3. 设置浏览器列表 确保目标兼容性
  4. 使用开发服务器 获得实时预览

🛠️ 工具推荐

  • 构建工具: Vite, Parcel, Webpack
  • CSS处理: PostCSS, Lightning CSS
  • 开发服务器: Live Server, BrowserSync

🎯 下一章预览

下一章将深入探讨CSS变量和自定义属性的强大功能。


最后更新: 2024年12月

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

2025年10月11日 10:34

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

🎯 本章重点

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

📖 内容概述

7.1 容器查询介绍

7.1.1 什么是容器查询

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

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

7.2 基础语法

7.2.1 定义容器
/* 创建容器上下文 */
.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 容器查询语法
@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 卡片组件
.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 导航组件
.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 表单组件
.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 容器单位
.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 嵌套容器查询
.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变量
: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 避免过度使用
/* 好的做法:合理的断点 */
@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 使用容器单位
/* 使用容器单位替代多个查询 */
.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 特性检测
/* 现代浏览器支持 */
@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检测
// 检测容器查询支持
if (CSS.supports('container-type', 'inline-size')) {
  console.log('容器查询支持');
} else {
  console.log('容器查询不支持,使用回退方案');
  // 添加回退类
  document.documentElement.classList.add('no-container-queries');
}
/* 回退样式 */
.no-container-queries .component {
  /* 传统响应式设计 */
  max-width: 400px;
}

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

7.7 最佳实践

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

💡 实战技巧

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

🎯 下一章预览

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


最后更新: 2024年12月

❌
❌