# 响应式之智——一页适配万物


## 概述

同一个页面在手机上竖着看，在桌面上横着看——布局必须跟着变。

Tailwind 采用 **mobile-first（移动优先）** 的策略：先写手机上的样式，再用断点覆盖更大屏幕的布局。

这意味着不加前缀的样式默认在所有屏幕上都生效。加上 `md:` 或 `lg:` 前缀后，只在达到相应宽度的屏幕上生效。

## 断点系统

### 默认断点

| 前缀 | 最小宽度 | 适用 |
|------|----------|------|
| `sm` | 640px | 大屏手机横屏、小平板 |
| `md` | 768px | 平板 |
| `lg` | 1024px | 小桌面、笔记本 |
| `xl` | 1280px | 桌面显示器 |
| `2xl` | 1536px | 大屏幕 |

![](images/08-breakpoints.svg)

### 使用方法

```html
<!-- 手机：上下排列；桌面：左右排列 -->
<div class="flex flex-col md:flex-row">
  <div class="w-full md:w-1/3">侧边栏</div>
  <div class="w-full md:w-2/3">主内容</div>
</div>

<!-- 手机：1 列；平板：2 列；桌面：3 列 -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
  <div>卡片 1</div>
  <div>卡片 2</div>
  <div>卡片 3</div>
</div>

<!-- 手机：小字号居中；桌面：大字号 -->
<h1 class="text-2xl text-center md:text-4xl md:text-left">
  适配不同屏幕的标题
</h1>
```

### 工作原理

`sm:` 等价于 `@media (width >= 40rem)`，`md:` 等价于 `@media (width >= 48rem)`，以此类推。

不加前缀的样式，对**所有**屏幕生效。加了前缀的样式，只在**该宽度及以上**生效。

```html
<!-- 默认是 flex-col，md 以上才是 flex-row -->
<div class="flex flex-col md:flex-row"></div>
```

### 最大宽度变体

v4 自动生成了对应的 `max-*` 变体，表示"小于某个断点"：

```html
<!-- 只在小于 md（768px）的屏幕上隐藏 -->
<div class="max-md:hidden">手机上隐藏</div>

<!-- 只在 md 到 lg 之间的屏幕上显示 -->
<div class="max-lg:max-md:block">
  平板专属内容
</div>
```

## 常见响应式模式

### 1. 响应式网格

最常用的模式——卡片数量随屏幕变化：

```html
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
  <!-- 手机 1 列 → 平板 2 列 → 小桌面 3 列 → 大桌面 4 列 -->
</div>
```

### 2. 导航栏折叠

手机上导航链接折叠成汉堡菜单，桌面上展开：

```html
<nav class="flex items-center justify-between px-6 h-16">
  <span class="text-xl font-bold">Logo</span>

  <!-- 桌面端显示的导航链接 -->
  <div class="hidden md:flex gap-8">
    <a href="#">首页</a>
    <a href="#">产品</a>
    <a href="#">关于</a>
  </div>

  <!-- 手机端汉堡按钮 -->
  <button class="md:hidden p-2">
    <svg><!-- 汉堡图标 --></svg>
  </button>
</nav>
```

`hidden md:flex` 的意思是：默认隐藏（手机不显示），md 以上用 flex 显示。

### 3. 侧边栏布局

手机上侧边栏隐藏或堆叠在上面，桌面端在左侧：

```html
<div class="flex flex-col md:flex-row">
  <aside class="w-full md:w-64 md:min-h-screen bg-gray-50 p-4">
    侧边栏
  </aside>
  <main class="flex-1 p-6">
    主内容
  </main>
</div>
```

### 4. 响应式字号

手机上用小一点的标题，桌面上用大的：

```html
<h1 class="text-2xl md:text-4xl lg:text-5xl font-bold">
  自适应标题
</h1>

<p class="text-sm md:text-base lg:text-lg">
  正文大小也根据屏幕变化
</p>
```

## Container 布局

Tailwind 的 `.container` 类会根据屏幕宽度自动设置 `max-width`：

```html
<div class="container mx-auto px-6">
  内容最大宽度不超过 2xl 断点（1536px），并居中
</div>
```

> [!tip] 技巧
> 使用 `container mx-auto px-6` 作为页面内容包装器。`mx-auto` 居中，`px-6` 在手机上提供边距。

## 容器查询（Container Queries）

v4 新特性。**容器查询**和**响应式断点**不同：断点基于视口宽度，容器查询基于**父容器**的宽度。

这个功能非常实用——同一个组件放在侧边栏和主内容区时，可以自适应。

标记容器：

```html
<div class="@container">
  <!-- 这个容器内的子元素可以用 @ 前缀查询容器宽度 -->
</div>
```

容器内的子元素：

```html
<div class="@container p-4">
  <div class="flex flex-col @md:flex-row">
    <!-- 容器宽度 >= 48rem 时横向排列，否则纵向 -->
  </div>
</div>
```

容器查询的断点以 `@` 开头：`@sm`、`@md`、`@lg`、`@xl`、`@2xl`。

常用场景：卡片组件。不管卡片放在哪里（侧边栏还是主区域），只要容器够宽就自动切换横排/竖排布局。

## 实战：让产品页全面响应

```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>星光科技 · 智能灯</title>
  <script src="https://unpkg.com/@tailwindcss/browser@4"></script>
</head>
<body>
  <nav class="fixed top-0 inset-x-0 bg-white/70 backdrop-blur-md border-b border-gray-100/50 z-50">
    <div class="max-w-5xl mx-auto flex items-center justify-between px-4 sm:px-6 h-16">
      <span class="text-lg sm:text-xl font-bold text-gray-900">星光科技</span>
      <!-- 桌面导航链接，手机上隐藏 -->
      <div class="hidden md:flex gap-6 lg:gap-8 text-gray-600">
        <a href="#" class="hover:text-gray-900 transition-colors">产品</a>
        <a href="#" class="hover:text-gray-900 transition-colors">特性</a>
        <a href="#" class="hover:text-gray-900 transition-colors">用户评价</a>
        <a href="#" class="hover:text-gray-900 transition-colors">联系我们</a>
      </div>
      <!-- 手机汉堡按钮 -->
      <button class="md:hidden p-2 text-gray-600">
        <svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
        </svg>
      </button>
    </div>
  </nav>

  <main class="max-w-5xl mx-auto px-4 sm:px-6 pt-28 sm:pt-32 pb-16">
    <!-- Hero -->
    <div class="bg-gradient-to-br from-blue-50 via-white to-purple-50 rounded-2xl sm:rounded-3xl p-8 sm:p-16 text-center">
      <h1 class="text-3xl sm:text-4xl lg:text-5xl font-extrabold text-gray-900 tracking-tight">
        智能台灯 <span class="text-transparent bg-clip-text bg-gradient-to-r from-blue-600 to-purple-600">Lumina</span>
      </h1>
      <p class="mt-4 sm:mt-6 text-base sm:text-lg lg:text-xl text-gray-500 leading-relaxed max-w-2xl mx-auto">
        光线随你而变。一盏懂你的台灯。
      </p>
      <button class="mt-6 sm:mt-8 px-6 sm:px-8 py-3 sm:py-4 bg-gradient-to-r from-blue-600 to-purple-600 text-white text-base sm:text-lg font-semibold rounded-xl shadow-lg shadow-blue-600/30
                     transform transition-all duration-200 ease-out
                     hover:-translate-y-1 hover:shadow-xl hover:shadow-blue-600/40
                     active:scale-95">
        立即购买
      </button>
    </div>

    <!-- 特性卡片 —— 响应式网格 -->
    <section class="mt-20 sm:mt-32">
      <h2 class="text-2xl sm:text-3xl font-bold text-gray-900 text-center">为什么选择 Lumina</h2>
      <div class="mt-8 sm:mt-12 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6 sm:gap-8">
        <div class="group p-5 sm:p-6 bg-white/80 backdrop-blur-sm rounded-2xl border border-gray-100 shadow-md
                        transition-all duration-200 hover:shadow-xl hover:-translate-y-1">
          <div class="w-10 h-10 sm:w-12 sm:h-12 bg-gradient-to-br from-blue-100 to-blue-50 rounded-xl flex items-center justify-center
                      transition-transform duration-200 group-hover:scale-110">
            <span class="text-xl sm:text-2xl">🧠</span>
          </div>
          <h3 class="mt-3 sm:mt-4 text-lg sm:text-xl font-semibold text-gray-900">AI 调光</h3>
          <p class="mt-2 text-sm sm:text-base text-gray-500 leading-relaxed">内置传感器实时感知环境光线，自动调整亮度和色温。</p>
        </div>

        <div class="group p-5 sm:p-6 bg-white/80 backdrop-blur-sm rounded-2xl border border-gray-100 shadow-md
                        transition-all duration-200 hover:shadow-xl hover:-translate-y-1">
          <div class="w-10 h-10 sm:w-12 sm:h-12 bg-gradient-to-br from-green-100 to-green-50 rounded-xl flex items-center justify-center
                      transition-transform duration-200 group-hover:scale-110">
            <span class="text-xl sm:text-2xl">👁️</span>
          </div>
          <h3 class="mt-3 sm:mt-4 text-lg sm:text-xl font-semibold text-gray-900">护眼模式</h3>
          <p class="mt-2 text-sm sm:text-base text-gray-500 leading-relaxed">无频闪、低蓝光，通过德国莱茵 TÜV 护眼认证。</p>
        </div>

        <div class="group p-5 sm:p-6 bg-white/80 backdrop-blur-sm rounded-2xl border border-gray-100 shadow-md
                        transition-all duration-200 hover:shadow-xl hover:-translate-y-1
                        sm:col-span-2 lg:col-span-1">
          <div class="w-10 h-10 sm:w-12 sm:h-12 bg-gradient-to-br from-purple-100 to-purple-50 rounded-xl flex items-center justify-center
                      transition-transform duration-200 group-hover:scale-110">
            <span class="text-xl sm:text-2xl">🎤</span>
          </div>
          <h3 class="mt-3 sm:mt-4 text-lg sm:text-xl font-semibold text-gray-900">语音控制</h3>
          <p class="mt-2 text-sm sm:text-base text-gray-500 leading-relaxed">支持 Siri、小爱同学、Google Assistant 语音指令。</p>
        </div>
      </div>
    </section>

    <!-- 用户评价 -->
    <section class="mt-20 sm:mt-32">
      <h2 class="text-2xl sm:text-3xl font-bold text-gray-900 text-center">用户评价</h2>
      <div class="mt-8 sm:mt-12 divide-y divide-gray-100">
        <div class="py-4 sm:py-6 flex gap-3 sm:gap-4">
          <img class="w-10 h-10 sm:w-12 sm:h-12 rounded-full grayscale hover:grayscale-0 transition-all duration-300" src="https://i.pravatar.cc/48?img=1" alt="用户" />
          <div>
            <p class="font-semibold text-gray-900">设计师小王</p>
            <p class="mt-1 text-sm sm:text-base text-gray-500">"用了两周，眼睛舒服多了。AI 调光真的很智能。"</p>
          </div>
        </div>
        <div class="py-4 sm:py-6 flex gap-3 sm:gap-4">
          <img class="w-10 h-10 sm:w-12 sm:h-12 rounded-full grayscale hover:grayscale-0 transition-all duration-300" src="https://i.pravatar.cc/48?img=2" alt="用户" />
          <div>
            <p class="font-semibold text-gray-900">程序员大刘</p>
            <p class="mt-1 text-sm sm:text-base text-gray-500">"晚上写代码的时候，灯光会自动变暖，不刺眼。"</p>
          </div>
        </div>
        <div class="py-4 sm:py-6 flex gap-3 sm:gap-4">
          <img class="w-10 h-10 sm:w-12 sm:h-12 rounded-full grayscale hover:grayscale-0 transition-all duration-300" src="https://i.pravatar.cc/48?img=3" alt="用户" />
          <div>
            <p class="font-semibold text-gray-900">学生小林</p>
            <p class="mt-1 text-sm sm:text-base text-gray-500">"期末复习全靠它了，灯光可以跟着时间变化。"</p>
          </div>
        </div>
      </div>
    </section>

    <footer class="mt-20 sm:mt-32 pt-8 border-t border-gray-100 flex flex-col sm:flex-row items-center justify-between gap-4 text-sm text-gray-400">
      <span>&copy; 2026 星光科技</span>
      <div class="flex gap-6">
        <a href="#" class="hover:text-gray-600 transition-colors">隐私政策</a>
        <a href="#" class="hover:text-gray-600 transition-colors">服务条款</a>
      </div>
    </footer>
  </main>
</body>
</html>
```

这一版的响应式处理清单：

| 元素 | 手机 | 平板 | 桌面 |
|------|------|------|------|
| 导航链接 | 隐藏 | 显示 | 显示 |
| 汉堡按钮 | 显示 | 隐藏 | 隐藏 |
| 卡片网格 | 1 列 | 2 列 | 3 列 |
| Hero 标题 | text-3xl | text-4xl | text-5xl |
| 按钮内边距 | px-6 py-3 | px-8 py-4 | px-8 py-4 |
| 圆角 | rounded-2xl | rounded-3xl | rounded-3xl |
| 页脚方向 | 纵向堆叠 | 横向排列 | 横向排列 |
| 截图内部间距 | p-8 | p-16 | p-16 |

> [!summary] 本章小结
> - Tailwind 是 mobile-first：不加前缀的样式对所有屏幕生效
> - `sm:` / `md:` / `lg:` / `xl:` / `2xl:` 是断点前缀
> - v4 也支持 `max-*` 变体表示"小于某断点"
> - 容器查询 `@container` + `@md:*` 让组件自适应父容器宽度
> - 响应式设计的关键：**先写好手机版，再逐步叠加更大屏幕的样式**

---

页面在各种屏幕上都很漂亮了。但只支持浅色背景——下一次我们加入**深色主题**。


---

> 作者: Aphros  
> URL: https://blog.papergate.top/posts/08.%E5%93%8D%E5%BA%94%E5%BC%8F%E4%B9%8B%E6%99%BA%E4%B8%80%E9%A1%B5%E9%80%82%E9%85%8D%E4%B8%87%E7%89%A9/  

