Tailwind CSS 简介与安装

Tailwind CSS 是一个实用优先的 CSS 框架,通过类名快速构建现代化界面。


1. 一句话概括主题

Tailwind CSS 是一个实用优先的 CSS 框架,它提供了大量预定义的 CSS 工具类,让你可以直接在 HTML 中使用类名来构建界面,而不需要写自定义 CSS。


2. 它是什么

传统 CSS 开发方式

在传统开发中,你需要这样写:

<!-- HTML -->
<div class="card">
  <h2 class="card-title">标题</h2>
  <p class="card-content">内容</p>
</div>
/* CSS */
.card {
  background-color: white;
  border-radius: 0.5rem;
  padding: 1.5rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
 
.card-title {
  font-size: 1.5rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
}
 
.card-content {
  color: #6b7280;
  line-height: 1.5;
}

Tailwind CSS 方式

使用 Tailwind CSS,你可以直接在 HTML 中使用工具类:

<div class="bg-white rounded-lg p-6 shadow-sm">
  <h2 class="text-2xl font-semibold mb-2">标题</h2>
  <p class="text-gray-500 leading-relaxed">内容</p>
</div>

简单理解:Tailwind CSS = 大量预定义的 CSS 工具类 + 实用优先的设计理念,让你用类名代替写 CSS。


3. 能解决什么问题 + 为什么重要

解决的问题

  1. 减少上下文切换:不需要在 HTML 和 CSS 文件之间切换
  2. 提高开发速度:不需要为每个样式写 CSS,直接使用工具类
  3. 保持一致性:使用预定义的设计系统,确保样式一致
  4. 响应式设计简单:内置响应式工具类,轻松实现响应式
  5. 按需打包:只打包使用的样式,文件体积小

为什么重要

  • 现代开发趋势:越来越多的项目采用实用优先的 CSS 框架
  • 提高效率:减少写 CSS 的时间,专注于业务逻辑
  • 易于维护:样式直接在 HTML 中,更容易理解和修改
  • 团队协作:统一的工具类命名,减少沟通成本

4. 核心概念

4.1 实用优先(Utility-First)

Tailwind CSS 采用实用优先的理念,提供大量单一职责的工具类:

<!-- 每个类名只做一件事 -->
<div class="flex items-center justify-between p-4 bg-white rounded-lg">
  <span class="text-lg font-bold text-gray-900">标题</span>
  <button class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">
    按钮
  </button>
</div>

4.2 响应式设计

Tailwind CSS 内置了响应式断点系统:

<!-- 移动端小字体,桌面端大字体 -->
<h1 class="text-2xl md:text-4xl lg:text-6xl">响应式标题</h1>
 
<!-- 移动端单列,桌面端多列 -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
  <!-- 内容 -->
</div>

4.3 状态变体

支持 hover、focus、active 等状态:

<button class="bg-blue-500 hover:bg-blue-600 focus:ring-2 focus:ring-blue-500 active:scale-95">
  交互按钮
</button>

5. 安装方式

5.1 使用 npm/yarn/pnpm 安装

步骤 1:安装 Tailwind CSS

# 使用 npm
npm install -D tailwindcss
 
# 使用 yarn
yarn add -D tailwindcss
 
# 使用 pnpm
pnpm add -D tailwindcss

步骤 2:初始化配置文件

npx tailwindcss init

这会创建一个 tailwind.config.js 文件:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [],
  theme: {
    extend: {},
  },
  plugins: [],
}

步骤 3:配置内容路径

tailwind.config.js 中配置需要扫描的文件:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    './src/**/*.{html,js,jsx,ts,tsx}',
    './public/index.html',
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

步骤 4:添加 Tailwind 指令

在你的主 CSS 文件中(通常是 src/index.csssrc/styles.css)添加:

@tailwind base;
@tailwind components;
@tailwind utilities;

步骤 5:在项目中引入 CSS 文件

在入口文件中引入 CSS:

// src/main.js 或 src/index.js
import './index.css'

5.2 使用 CDN(快速测试)

仅用于快速测试,生产环境不推荐:

<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
  <div class="bg-blue-500 text-white p-4">
    使用 CDN 的 Tailwind CSS
  </div>
</body>
</html>

5.3 与框架集成

React + Vite

# 创建项目
npm create vite@latest my-app -- --template react
 
# 安装 Tailwind CSS
cd my-app
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

Next.js

Next.js 13+ 内置 Tailwind CSS 支持:

# 创建项目时选择 Tailwind CSS
npx create-next-app@latest my-app --tailwind
 
# 或手动安装
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

Vue

# 创建项目
npm create vite@latest my-app -- --template vue
 
# 安装 Tailwind CSS
cd my-app
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

6. 验证安装

创建一个测试页面验证安装是否成功:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Tailwind CSS 测试</title>
  <link href="./dist/output.css" rel="stylesheet">
</head>
<body>
  <div class="min-h-screen bg-gradient-to-br from-blue-500 to-purple-600 flex items-center justify-center">
    <div class="bg-white rounded-lg shadow-xl p-8 max-w-md">
      <h1 class="text-3xl font-bold text-gray-900 mb-4">
        Tailwind CSS 安装成功!
      </h1>
      <p class="text-gray-600 mb-6">
        如果你能看到这个样式化的页面,说明 Tailwind CSS 已经正确安装。
      </p>
      <button class="w-full bg-blue-500 text-white py-2 px-4 rounded-lg hover:bg-blue-600 transition-colors">
        开始使用
      </button>
    </div>
  </div>
</body>
</html>

如果页面显示正常,说明安装成功!


7. 常见问题

Q: 样式不生效怎么办?

A: 检查以下几点:

  1. 确保 tailwind.config.js 中的 content 路径正确
  2. 确保在主 CSS 文件中添加了 @tailwind 指令
  3. 确保构建工具正确处理了 CSS 文件
  4. 清除缓存并重新构建

Q: 文件体积太大怎么办?

A: Tailwind CSS 使用 JIT(Just-In-Time)模式,只打包使用的样式。确保:

  1. content 配置正确,Tailwind 能扫描到所有文件
  2. 使用生产构建(NODE_ENV=production
  3. 启用 PurgeCSS(Tailwind 3.0+ 已内置)

Q: 如何自定义主题?

A:tailwind.config.jstheme 中扩展:

module.exports = {
  theme: {
    extend: {
      colors: {
        'brand-blue': '#1e40af',
      },
    },
  },
}

8. 下一步

安装完成后,你可以:

  1. 阅读 快速开始 了解基础用法
  2. 学习 核心概念 掌握工具类系统
  3. 查看 官方文档 了解更多

相关链接

TailwindCSS 安装 配置 实用优先