Tailwind CSS 完整学习指南
# 🎨 Tailwind CSS 完整学习指南
# 📋 目录
# 什么是 Tailwind CSS
Tailwind CSS 是一个**实用优先(Utility-First)**的 CSS 框架,它提供了大量预定义的 CSS 类,让你能够快速构建现代化的用户界面。
# 核心特点
- 实用优先: 通过组合小的、单一用途的类来构建复杂的设计
- 高度可定制: 通过配置文件自定义设计系统
- 响应式: 内置响应式设计支持
- JIT 模式: 按需生成 CSS,减少文件大小
- 组件友好: 与 React、Vue 等框架完美集成
# 传统 CSS vs Tailwind CSS
/* 传统 CSS */
.button {
background-color: #3b82f6;
color: white;
padding: 0.5rem 1rem;
border-radius: 0.375rem;
font-weight: 500;
transition: background-color 0.2s;
}
.button:hover {
background-color: #2563eb;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
<!-- Tailwind CSS -->
<button class="bg-blue-500 text-white px-4 py-2 rounded-md font-medium hover:bg-blue-600 transition-colors">
Click me
</button>
1
2
3
4
2
3
4
# 核心概念
# 1. 实用优先设计
Tailwind 不提供预构建的组件,而是提供构建块(utility classes):
<!-- 构建一个卡片组件 -->
<div class="bg-white rounded-lg shadow-md p-6 max-w-sm">
<h3 class="text-lg font-semibold text-gray-900 mb-2">Card Title</h3>
<p class="text-gray-600">Card content goes here...</p>
<button class="mt-4 bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">
Learn More
</button>
</div>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 2. 设计系统
Tailwind 基于设计系统构建,提供一致的设计语言:
<!-- 使用设计系统的间距 -->
<div class="space-y-4">
<div class="p-4">Item 1</div>
<div class="p-4">Item 2</div>
<div class="p-4">Item 3</div>
</div>
<!-- 使用设计系统的颜色 -->
<div class="bg-blue-50 border border-blue-200 text-blue-800">
Info message
</div>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 安装与配置
# 1. 通过 CDN 使用(快速开始)
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<h1 class="text-3xl font-bold text-blue-600">Hello Tailwind!</h1>
</body>
</html>
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 2. 通过 npm 安装
# 安装 Tailwind CSS
npm install -D tailwindcss
# 生成配置文件
npx tailwindcss init
# 安装 PostCSS 和 autoprefixer
npm install -D postcss autoprefixer
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 3. 配置文件
// tailwind.config.js
module.exports = {
content: [
"./src/**/*.{html,js,jsx,ts,tsx}",
"./public/index.html"
],
theme: {
extend: {
colors: {
'brand-blue': '#1e40af',
'brand-green': '#059669'
},
fontFamily: {
'sans': ['Inter', 'system-ui', 'sans-serif']
}
},
},
plugins: [],
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 基础语法
# 类名结构
Tailwind 的类名遵循特定模式:
<!-- 基础语法: property-value -->
<div class="bg-blue-500 text-white p-4 rounded-lg">
<!-- 响应式: breakpoint:property-value -->
<div class="md:bg-red-500 lg:bg-green-500">
<!-- 状态: state:property-value -->
<button class="hover:bg-blue-600 focus:ring-2 focus:ring-blue-500">
<!-- 变体: variant:property-value -->
<div class="dark:bg-gray-800 dark:text-white">
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 数值系统
<!-- 间距: 0, 1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32, 40, 48, 56, 64 -->
<div class="p-4 m-8">Padding 4, Margin 8</div>
<!-- 颜色: 50, 100, 200, 300, 400, 500, 600, 700, 800, 900 -->
<div class="bg-blue-500 text-blue-100">Blue color variants</div>
<!-- 尺寸: 0, 1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32, 40, 48, 56, 64, 72, 80, 96 -->
<div class="w-64 h-32">Width 64, Height 32</div>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 布局系统
# Flexbox 布局
<!-- 基础 Flexbox -->
<div class="flex">
<div class="flex-1">Flex item 1</div>
<div class="flex-1">Flex item 2</div>
</div>
<!-- Flex 方向 -->
<div class="flex flex-col">垂直布局</div>
<div class="flex flex-row">水平布局</div>
<!-- 对齐方式 -->
<div class="flex items-center justify-center">
<div>居中对齐</div>
</div>
<!-- 常用 Flexbox 类 -->
<div class="flex flex-col items-center justify-between space-y-4">
<div class="flex-shrink-0">不缩小</div>
<div class="flex-grow">自动增长</div>
<div class="self-end">自身对齐</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Grid 布局
<!-- 基础 Grid -->
<div class="grid grid-cols-3 gap-4">
<div>Grid item 1</div>
<div>Grid item 2</div>
<div>Grid item 3</div>
</div>
<!-- 响应式 Grid -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<div>Responsive grid item</div>
</div>
<!-- Grid 区域 -->
<div class="grid grid-cols-4 grid-rows-3 gap-4 h-64">
<div class="col-span-2 row-span-2 bg-blue-500">Large area</div>
<div class="col-span-2 bg-green-500">Medium area</div>
<div class="col-span-4 bg-red-500">Full width</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 定位
<!-- 相对定位 -->
<div class="relative">
<div class="absolute top-0 right-0">绝对定位</div>
</div>
<!-- 固定定位 -->
<div class="fixed top-4 right-4">固定定位</div>
<!-- 粘性定位 -->
<div class="sticky top-0">粘性定位</div>
<!-- 居中技巧 -->
<div class="relative h-64">
<div class="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2">
完美居中
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 样式系统
# 颜色系统
<!-- 背景颜色 -->
<div class="bg-blue-500">蓝色背景</div>
<div class="bg-gradient-to-r from-blue-500 to-purple-600">渐变背景</div>
<!-- 文字颜色 -->
<p class="text-gray-800">深灰色文字</p>
<p class="text-blue-600">蓝色文字</p>
<!-- 边框颜色 -->
<div class="border border-red-500">红色边框</div>
<div class="border-l-4 border-l-blue-500">左边框</div>
<!-- 颜色透明度 -->
<div class="bg-blue-500/50">50% 透明度</div>
<div class="bg-blue-500 bg-opacity-75">75% 透明度</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 字体系统
<!-- 字体大小 -->
<h1 class="text-4xl">大标题</h1>
<h2 class="text-2xl">中标题</h2>
<p class="text-base">正文</p>
<span class="text-sm">小字</span>
<!-- 字体粗细 -->
<p class="font-thin">细体</p>
<p class="font-normal">正常</p>
<p class="font-medium">中等</p>
<p class="font-semibold">半粗</p>
<p class="font-bold">粗体</p>
<!-- 行高 -->
<p class="leading-tight">紧密行高</p>
<p class="leading-normal">正常行高</p>
<p class="leading-relaxed">宽松行高</p>
<!-- 字间距 -->
<p class="tracking-tight">紧密字间距</p>
<p class="tracking-normal">正常字间距</p>
<p class="tracking-wide">宽松字间距</p>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 间距系统
<!-- 内边距 -->
<div class="p-4">四周内边距</div>
<div class="px-4 py-2">水平垂直内边距</div>
<div class="pt-4 pr-2 pb-4 pl-2">各边内边距</div>
<!-- 外边距 -->
<div class="m-4">四周边距</div>
<div class="mx-auto">水平居中</div>
<div class="mt-8 mb-4">上下边距</div>
<!-- 间距控制 -->
<div class="space-y-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<div class="space-x-4 flex">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 响应式设计
# 断点系统
<!-- 默认 (手机) -->
<div class="text-sm">小屏幕文字</div>
<!-- sm: 640px+ -->
<div class="sm:text-base">小屏幕以上</div>
<!-- md: 768px+ -->
<div class="md:text-lg">中等屏幕以上</div>
<!-- lg: 1024px+ -->
<div class="lg:text-xl">大屏幕以上</div>
<!-- xl: 1280px+ -->
<div class="xl:text-2xl">超大屏幕以上</div>
<!-- 2xl: 1536px+ -->
<div class="2xl:text-3xl">超超大屏幕以上</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 响应式布局
<!-- 响应式网格 -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
<div class="bg-blue-500 p-4">Grid Item</div>
</div>
<!-- 响应式 Flexbox -->
<div class="flex flex-col md:flex-row items-center justify-between">
<div>Logo</div>
<nav class="flex space-x-4 mt-4 md:mt-0">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</nav>
</div>
<!-- 响应式显示/隐藏 -->
<div class="hidden md:block">中等屏幕以上显示</div>
<div class="block md:hidden">小屏幕显示</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 状态变体
# 交互状态
<!-- 悬停状态 -->
<button class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded">
Hover me
</button>
<!-- 焦点状态 -->
<input class="border border-gray-300 focus:border-blue-500 focus:ring-2 focus:ring-blue-200 rounded px-3 py-2">
<!-- 激活状态 -->
<button class="bg-green-500 active:bg-green-600 text-white px-4 py-2 rounded">
Click me
</button>
<!-- 禁用状态 -->
<button class="bg-gray-300 text-gray-500 cursor-not-allowed px-4 py-2 rounded" disabled>
Disabled
</button>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 深色模式
<!-- 深色模式支持 -->
<div class="bg-white dark:bg-gray-800 text-gray-900 dark:text-white">
<h1 class="text-2xl font-bold">深色模式标题</h1>
<p class="text-gray-600 dark:text-gray-300">深色模式内容</p>
</div>
<!-- 深色模式切换 -->
<button class="bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-200 px-4 py-2 rounded">
Toggle Theme
</button>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 实用工具
# 阴影和边框
<!-- 阴影 -->
<div class="shadow-sm">小阴影</div>
<div class="shadow">默认阴影</div>
<div class="shadow-lg">大阴影</div>
<div class="shadow-xl">超大阴影</div>
<div class="shadow-2xl">巨大阴影</div>
<!-- 边框圆角 -->
<div class="rounded">小圆角</div>
<div class="rounded-md">中等圆角</div>
<div class="rounded-lg">大圆角</div>
<div class="rounded-full">完全圆形</div>
<!-- 边框 -->
<div class="border">默认边框</div>
<div class="border-2 border-blue-500">蓝色粗边框</div>
<div class="border-l-4 border-l-red-500">左边框</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 变换和动画
<!-- 变换 -->
<div class="transform rotate-45">旋转 45 度</div>
<div class="transform scale-110">放大 110%</div>
<div class="transform -translate-x-4">向左移动</div>
<!-- 过渡动画 -->
<button class="bg-blue-500 hover:bg-blue-600 transition-colors duration-200">
颜色过渡
</button>
<div class="transform hover:scale-105 transition-transform duration-300">
缩放过渡
</div>
<!-- 自定义动画 -->
<div class="animate-pulse">脉冲动画</div>
<div class="animate-bounce">弹跳动画</div>
<div class="animate-spin">旋转动画</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 最佳实践
# 1. 组件化思维
<!-- 创建可复用的组件类 -->
<button class="btn-primary">Primary Button</button>
<button class="btn-secondary">Secondary Button</button>
<style>
@layer components {
.btn-primary {
@apply bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600 transition-colors;
}
.btn-secondary {
@apply bg-gray-500 text-white px-4 py-2 rounded hover:bg-gray-600 transition-colors;
}
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 2. 使用 @apply 指令
/* 在 CSS 中使用 Tailwind 类 */
@layer components {
.card {
@apply bg-white rounded-lg shadow-md p-6;
}
.card-header {
@apply text-lg font-semibold text-gray-900 mb-4;
}
.card-body {
@apply text-gray-600;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 实际案例
# 1. 导航栏
<nav class="bg-white shadow-lg">
<div class="max-w-7xl mx-auto px-4">
<div class="flex justify-between items-center py-4">
<!-- Logo -->
<div class="flex items-center">
<img class="h-8 w-8" src="logo.svg" alt="Logo">
<span class="ml-2 text-xl font-bold text-gray-800">Brand</span>
</div>
<!-- 桌面导航 -->
<div class="hidden md:flex space-x-8">
<a href="#" class="text-gray-600 hover:text-blue-600 transition-colors">Home</a>
<a href="#" class="text-gray-600 hover:text-blue-600 transition-colors">About</a>
<a href="#" class="text-gray-600 hover:text-blue-600 transition-colors">Services</a>
<a href="#" class="text-gray-600 hover:text-blue-600 transition-colors">Contact</a>
</div>
<!-- 移动端菜单按钮 -->
<button class="md:hidden">
<svg class="h-6 w-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>
</div>
</nav>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 2. 卡片组件
<div class="max-w-sm bg-white rounded-lg shadow-md overflow-hidden">
<img class="w-full h-48 object-cover" src="image.jpg" alt="Card image">
<div class="p-6">
<h3 class="text-xl font-semibold text-gray-900 mb-2">Card Title</h3>
<p class="text-gray-600 mb-4">Card description goes here...</p>
<div class="flex items-center justify-between">
<span class="text-2xl font-bold text-blue-600">$99</span>
<button class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600 transition-colors">
Buy Now
</button>
</div>
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 实用技巧
# 1. 居中技巧
<!-- 完美居中 -->
<div class="flex items-center justify-center min-h-screen">
<div>水平垂直居中</div>
</div>
<!-- 绝对定位居中 -->
<div class="relative h-64">
<div class="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2">
绝对定位居中
</div>
</div>
<!-- Grid 居中 -->
<div class="grid place-items-center h-64">
<div>Grid 居中</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 2. 分隔线
<!-- 垂直分隔线 -->
<div class="divide-y divide-gray-300">
<div class="py-2">Item 1</div>
<div class="py-2">Item 2</div>
<div class="py-2">Item 3</div>
</div>
<!-- 水平分隔线 -->
<div class="flex divide-x divide-gray-300">
<div class="px-4">Item 1</div>
<div class="px-4">Item 2</div>
<div class="px-4">Item 3</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 3. 屏幕阅读器支持
<!-- 仅屏幕阅读器可见 -->
<div class="sr-only">Screen reader only content</div>
<!-- 焦点时显示 -->
<div class="sr-only focus:not-sr-only">Focus to show</div>
1
2
3
4
5
2
3
4
5
# 常用命令
# 安装 Tailwind CSS
npm install -D tailwindcss postcss autoprefixer
# 初始化配置文件
npx tailwindcss init
# 构建 CSS
npx tailwindcss -i ./src/input.css -o ./dist/output.css
# 监听模式
npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch
# 生产构建(压缩)
npx tailwindcss -i ./src/input.css -o ./dist/output.css --minify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 学习资源
- 官方文档 (opens new window)
- Tailwind Play (opens new window) - 在线编辑器
- Tailwind UI (opens new window) - 官方组件库
- Headless UI (opens new window) - 无样式组件库
- Heroicons (opens new window) - 图标库
# 总结
Tailwind CSS 是一个强大的实用优先 CSS 框架,它通过提供大量预定义的实用类,让开发者能够快速构建现代化的用户界面。掌握 Tailwind CSS 的关键在于:
- 理解实用优先的设计理念
- 熟悉类名命名规则和语法
- 掌握响应式设计和状态变体
- 学会组件化思维和最佳实践
- 利用工具和插件提升开发效率
通过不断实践和探索,Tailwind CSS 将成为你前端开发工具箱中的重要工具!🚀
上次更新: 2025/09/30, 16:44:31