Skip to content

CSS Responsive 设计

构建 responsive 布局使用 media 查询, mobile-首先, and fluid 单元.

你将学到

  • 什么 responsive 设计是 and 为什么 mobile-首先是 正确方法
  • 如何 media 查询 adapt 样式 at 不同 breakpoints
  • Fluid 单元: 百分比, vw/vh, rem/em, and clamp()
  • 常见 breakpoint 策略 and role 的容器查询

概念讲解

CSS Responsive Design

Responsive design means your layout adapts to the screen size — from a 320px phone to a 2560px desktop monitor — without serving separate pages or requiring pinch-to-zoom. The goal is one codebase that looks good at every size. CSS provides the tools: media queries, fluid units, and flexible layout systems like Flexbox and Grid.

Mobile-First

The mobile-first approach means you write your base styles for the smallest screens, then add media queries to enhance the layout for larger screens. This is the opposite of building a desktop site and "shrinking 它下." Mobile-first is better because mobile styles are simpler (usually stacked, single-column), and adding complexity for larger screens is easier than removing it. It also produces smaller CSS for mobile devices, which often have slower networks.

Media Queries

A media query applies styles only when a condition is met — usually the viewport width:

/* Base: mobile (single column) */
.card-grid { display: grid; grid-template-columns: 1fr; }

/* Tablet and up: two columns */
@media (min-width: 768px) {
  .card-grid { grid-template-columns: 1fr 1fr; }
}

/* Desktop and up: three columns */
@media (min-width: 1024px) {
  .card-grid { grid-template-columns: repeat(3, 1fr); }
}

Using 最小值-宽度 (not 最大值-宽度) is the mobile-first pattern. Each breakpoint *adds* to the base, never overrides it with simpler styles.

Fluid Units

Hardcoded pixel sizes break responsiveness. Prefer fluid units:

  • % — relative to the parent.
  • vw / vh — relative to the viewport width/height. 100vw is the full width.
  • rem — relative to the root font size. Scales with user preferences.
  • em — relative to the element's own font size.
  • clamp(最小值, ideal, 最大值) — a value that grows fluidly but never goes below min or above max. Perfect for font sizes: 字体-大小: clamp(1rem, 2.5vw, 2rem).

Breakpoints

Common breakpoints align with device classes: 640px (large phone), 768px (tablet), 1024px (laptop), 1280px (desktop). But the best breakpoints are content-based — choose them where your layout breaks, not where devices happen to fall. Test by slowly resizing the browser and adding a breakpoint only when the design looks wrong.

示例代码

              /* Mobile-first: base styles for phones */
.container {
  width: 100%;
  padding: 0 16px;
}

.hero-title {
  font-size: clamp(1.75rem, 5vw, 3.5rem);
  line-height: 1.2;
}

.layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

/* Tablet: 768px and up */
@media (min-width: 768px) {
  .container { padding: 0 24px; }
  .layout {
    grid-template-columns: 240px 1fr;
    gap: 24px;
  }
}

/* Desktop: 1024px and up */
@media (min-width: 1024px) {
  .container { max-width: 1200px; margin: 0 auto; }
  .layout { gap: 32px; }
}
            

基础样式目标 mobile. clamp() 制作 hero title scale fluidly 之间 1.75rem and 3.5rem. 每个最小值-宽度 media 查询添加 enhancements for larger screens — wider padding, sidebar 列, and 最大值-宽度容器 on desktop.

动手试一试

  • 镜像颜色 Picker

    选取 responsive 颜色值 for 你的 media 查询样式.

  • SVG Optimizer

    Optimize SVGs 那个 scale responsively 使用 vw 单元 and viewBox.

常见错误

错误写法

Using 最大值-宽度 media 查询 (desktop-首先) and overriding 使用 simpler mobile 样式.

正确做法

编写 mobile-首先使用最小值-宽度查询. 基础样式目标 smallest screen; 每个 breakpoint 添加复杂度 for larger screens. 这个 produces smaller CSS for mobile and cleaner cascade.

错误写法

Using fixed 像素宽度那个 cause horizontal scrolling on 小 screens.

正确做法

Use 百分比, fr 单元, or 最大值-宽度相反的 fixed 宽度. For 容器, use 宽度: 100% 使用最大值-宽度 in px and margin: 0 auto 到中心 on 大 screens 不使用 overflowing on 小 ones.

相关代码片段

相关速查表

练习 CSS Responsive 设计

5 个练习

练习 CSS Responsive 设计

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误