快速掌握Tailwind CSS
作者:海川,发表于:2026年1月26日 02:54:40
从设计哲学到实战应用,深度理解 Tailwind CSS 的核心理念、优缺点、与其他框架的对比
前言
如果你已经熟悉 CSS,Tailwind CSS 就是一套”类名快速参考”——它把每个常用的 CSS 属性都映射到了一个简短的类名。你不需要学习新的 CSS 知识,只需要学会”CSS规则 → Tailwind类名”的转换。
本文核心:详尽列举 CSS 到 Tailwind 的映射关系,让你快速上手,并保留与 Bootstrap 的对比。
一、核心概念(30秒速览)
| CSS 写法 | Tailwind 写法 |
|---|
width: 100% | class="w-full" |
padding: 1rem | class="p-4" |
display: flex | class="flex" |
background-color: rgb(59, 130, 246) | class="bg-blue-500" |
border-radius: 0.5rem | class="rounded-lg" |
规律:一个属性值 = 一个简短的 class,在 HTML 的 class 属性中组合使用。
二、CSS 属性映射表(快速查询)
1. 尺寸和间距
Width(宽度)
| CSS | Tailwind |
|---|
width: 100% | w-full |
width: 50% | w-1/2 |
width: 33.333% | w-1/3 |
width: 16rem (256px) | w-64 |
width: 20rem (320px) | w-80 |
min-width: 0 | min-w-0 |
max-width: 28rem (448px) | max-w-md |
单位系统:每级代表4px,共有0-96级,即0px-384px。1rem = 16px = 4级
Height(高度)
| CSS | Tailwind |
|---|
height: 100% | h-full |
height: 100vh | h-screen |
height: 16rem (256px) | h-64 |
min-height: 100vh | min-h-screen |
max-height: 20rem (320px) | max-h-80 |
Padding(内边距)
| CSS | Tailwind |
|---|
padding: 1rem | p-4 |
padding: 0.5rem 1rem | px-4 py-2 |
padding-left: 2rem | pl-8 |
padding-top: 1.5rem | pt-6 |
快速记忆:p=padding, m=margin, px=horizontal, py=vertical
Margin(外边距)
| CSS | Tailwind |
|---|
margin: 0 | m-0 |
margin: 1rem | m-4 |
margin: 0 auto | mx-auto |
margin-bottom: 2rem | mb-8 |
margin-left: -1rem | -ml-4 |
Gap(间距)
| CSS | Tailwind |
|---|
gap: 1rem | gap-4 |
gap: 1.5rem | gap-6 |
column-gap: 1rem | gap-x-4 |
row-gap: 2rem | gap-y-8 |
2. 布局
Display(显示方式)
| CSS | Tailwind |
|---|
display: block | block |
display: inline | inline |
display: inline-block | inline-block |
display: flex | flex |
display: grid | grid |
display: none | hidden |
Flexbox(一维布局)
| CSS | Tailwind |
|---|
flex-direction: row | flex-row (默认) |
flex-direction: column | flex-col |
justify-content: center | justify-center |
justify-content: space-between | justify-between |
align-items: center | items-center |
align-items: flex-end | items-end |
flex: 1 (自动扩展) | flex-1 |
flex-wrap: wrap | flex-wrap |
常用组合:flex items-center justify-between = 水平均匀分布且竖直居中
Grid(二维布局)
| CSS | Tailwind |
|---|
grid-template-columns: repeat(3, 1fr) | grid-cols-3 |
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)) | grid-cols-[repeat(auto-fit,minmax(250px,1fr))] |
gap: 1rem | gap-4 |
3. 颜色和背景
背景色
| CSS | Tailwind |
|---|
background-color: #3b82f6 (蓝色) | bg-blue-500 |
background-color: #ef4444 (红色) | bg-red-500 |
background-color: white | bg-white |
background-color: transparent | bg-transparent |
颜色深度:50(最浅)→ 100 → 200 → … → 900(最深)
文字色
| CSS | Tailwind |
|---|
color: white | text-white |
color: #1f2937 (深灰) | text-gray-800 |
color: #6366f1 (靛蓝) | text-indigo-500 |
Border(边框)
| CSS | Tailwind |
|---|
border: 1px solid black | border border-black |
border: 2px solid #e5e7eb | border-2 border-gray-200 |
border-left: 3px solid blue | border-l-4 border-blue-500 |
border-radius: 0.5rem | rounded-lg |
border-radius: 50% | rounded-full |
4. 文字样式
字体大小和行高
| CSS | Tailwind |
|---|
font-size: 12px | text-xs |
font-size: 14px | text-sm |
font-size: 16px | text-base |
font-size: 20px | text-xl |
font-size: 32px | text-4xl |
字体权重
| CSS | Tailwind |
|---|
font-weight: 400 (正常) | font-normal |
font-weight: 600 (加粗) | font-semibold |
font-weight: 700 (很粗) | font-bold |
文字对齐
| CSS | Tailwind |
|---|
text-align: left | text-left |
text-align: center | text-center |
text-align: right | text-right |
text-align: justify | text-justify |
其他文字效果
| CSS | Tailwind |
|---|
text-decoration: underline | underline |
text-decoration: none | no-underline |
text-transform: uppercase | uppercase |
text-transform: lowercase | lowercase |
overflow: hidden; text-overflow: ellipsis; white-space: nowrap | truncate |
| 限制3行,超出省略号 | line-clamp-3 |
5. 阴影和效果
| CSS | Tailwind |
|---|
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1) | shadow-sm |
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1) | shadow-lg |
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1) | shadow-2xl |
opacity: 0.5 | opacity-50 |
opacity: 0.75 | opacity-75 |
6. 位置和定位
| CSS | Tailwind |
|---|
position: static | static |
position: relative | relative |
position: absolute | absolute |
position: fixed | fixed |
position: sticky | sticky |
top: 0 | top-0 |
right: 1rem | right-4 |
z-index: 10 | z-10 |
三、响应式设计(移动优先)
Tailwind 默认是移动端,然后用前缀适配更大屏幕:
| 屏幕宽度 | 前缀 | 用法例 |
|---|
| < 640px | (无) | w-full |
| >= 640px | sm: | sm:w-1/2 |
| >= 768px | md: | md:w-1/3 |
| >= 1024px | lg: | lg:w-1/4 |
| >= 1280px | xl: | xl:w-1/5 |
<!-- 手机全宽,平板50%,电脑33% -->
<div class="w-full sm:w-1/2 md:w-1/3">响应式宽度</div>
<!-- 手机竖排,电脑横排 -->
<div class="flex flex-col md:flex-row gap-4">
<div>Item 1</div>
<div>Item 2</div>
</div>
四、伪类和状态变体
在 Tailwind 类名前加前缀,代表不同状态:
| 状态 | 前缀 | 用法例 |
|---|
| 鼠标悬停 | hover: | hover:bg-blue-600 |
| 获得焦点 | focus: | focus:outline-2 |
| 活跃状态 | active: | active:bg-blue-800 |
| 禁用状态 | disabled: | disabled:opacity-50 |
| 深色模式 | dark: | dark:bg-gray-900 |
<button
class="
bg-blue-600 text-white
hover:bg-blue-700
focus:outline-none focus:ring-2 focus:ring-blue-400
disabled:opacity-50 disabled:cursor-not-allowed
"
>
点击我
</button>
<div class="bg-white dark:bg-gray-900">深色模式友好</div>
五、实战常见场景
场景1:居中一个元素
<!-- CSS 写法 -->
<style>
.centered {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
</style>
<!-- Tailwind 写法 -->
<div class="flex justify-center items-center h-screen">
<div>居中内容</div>
</div>
场景2:响应式网格(3列 → 2列 → 1列)
<!-- CSS 写法 -->
<style>
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
@media (max-width: 1024px) {
.grid {
grid-template-columns: repeat(2, 1fr);
}
}
@media (max-width: 640px) {
.grid {
grid-template-columns: 1fr;
}
}
</style>
<!-- Tailwind 写法 -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
场景3:卡片组件
<!-- CSS 写法 -->
<style>
.card {
background: white;
border: 1px solid #e5e7eb;
border-radius: 0.5rem;
padding: 1.5rem;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
transition: box-shadow 0.2s;
}
.card:hover {
box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
}
</style>
<!-- Tailwind 写法 -->
<div
class="bg-white border border-gray-200 rounded-lg p-6 shadow-md hover:shadow-lg transition-shadow"
>
<h3 class="text-lg font-bold mb-2">标题</h3>
<p class="text-gray-600">内容描述</p>
</div>
场景4:按钮状态
<!-- CSS 写法 -->
<style>
.btn {
padding: 0.5rem 1rem;
background: #3b82f6;
color: white;
border: none;
border-radius: 0.375rem;
cursor: pointer;
transition: background 0.2s;
}
.btn:hover {
background: #2563eb;
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
</style>
<!-- Tailwind 写法 -->
<button
class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 disabled:opacity-50 disabled:cursor-not-allowed"
>
按钮
</button>
六、Tailwind vs Bootstrap 对比
语法对比
| 需求 | Bootstrap | Tailwind |
|---|
| 创建按钮 | <button class="btn btn-primary btn-lg"> | <button class="px-4 py-2 bg-blue-600 text-white"> |
| 网格布局 | <div class="container"><div class="row"><div class="col-md-4"> | <div class="grid grid-cols-3 gap-4"><div> |
| 文本居中 | <div class="text-center"> | <div class="text-center"> (同) |
| 隐藏元素 | <div class="d-none d-md-block"> | <div class="hidden md:block"> |
| 添加阴影 | <div class="shadow"> | <div class="shadow-md"> |
学习成本
| 方面 | Bootstrap | Tailwind |
|---|
| 初学者友好 | ⭐⭐⭐ | ⭐⭐ |
| 要记多少类 | 30-50个 | 学会映射规则即可 |
| 定制难度 | 中等 | 简单 |
| 文件体积 | 50-130KB | 15-50KB |
| 学习方式 | 记组件名 | 理解 CSS 属性映射 |
选择建议
用 Bootstrap 当你:
- 快速搭建通用 UI(表单、表格、按钮等)
- 需要预制组件快速上手
- 团队 CSS 基础较弱
用 Tailwind 当你:
- 追求自定义设计,不想”有 Bootstrap 的样子”
- 已经熟悉 CSS,需要快速转换
- 要建立设计系统或品牌风格
七、最佳实践
1. 提取重复样式(@apply)
如果多个地方用同样的样式组合,提取为类:
@layer components {
.btn-primary {
@apply px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 transition;
}
.card {
@apply bg-white rounded-lg shadow-md p-6 border border-gray-100;
}
}
然后 HTML 中使用:
<button class="btn-primary">提交</button>
<div class="card">卡片内容</div>
2. 使用设计系统配置
// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
primary: "#0066cc", // 项目主色
secondary: "#ff6600", // 项目副色
},
spacing: {
gutter: "1.5rem", // 全局间距
},
},
},
};
然后整个项目使用 bg-primary、text-primary、px-gutter 等,修改风格时只需改配置。
3. 响应式优先使用 mobile-first
<!-- ❌ 不推荐:从大屏开始写 -->
<div class="lg:w-1/2 md:w-2/3 sm:w-full">
<!-- ✅ 推荐:从小屏开始写 -->
<div class="w-full sm:w-2/3 md:w-1/2"></div>
</div>
4. class 过长时换行
<!-- ❌ 不可读 -->
<div
class="flex flex-col md:flex-row gap-4 p-6 bg-white rounded-lg shadow-md border border-gray-200 hover:shadow-lg transition-shadow"
>
<!-- ✅ 可读 -->
<div
class="
flex flex-col md:flex-row
gap-4 p-6
bg-white rounded-lg shadow-md border border-gray-200
hover:shadow-lg transition-shadow
"
></div>
</div>
八、常见问题
Q1:动态 class 名不工作
// ❌ 错误
const color = "blue";
<div className={`bg-${color}-600`}>内容</div>;
// 原因:构建时看不到 bg-blue-600
// ✅ 正确
const colorMap = { blue: "bg-blue-600", red: "bg-red-600" };
<div className={colorMap[color]}>内容</div>;
Q2:如何使用自定义颜色?
// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
"brand-blue": "#1e3a8a",
},
},
},
};
// HTML 中使用
<div class="bg-brand-blue">自定义颜色</div>;
Q3:单位是多少?
基础单位 = 4px
0 = 0px
1 = 4px
2 = 8px
4 = 16px
6 = 24px
8 = 32px
...
96 = 384px
九、总结速查表
最常用的 20 个类
| 功能 | 类名 |
|---|
| 弹性布局 | flex |
| 水平居中 | justify-center |
| 竖直居中 | items-center |
| 全宽 | w-full |
| 全高 | h-screen |
| 内边距 1rem | p-4 |
| 外边距 auto | mx-auto |
| 蓝色背景 | bg-blue-500 |
| 圆角 | rounded-lg |
| 阴影 | shadow-md |
| 文本白色 | text-white |
| 粗体 | font-bold |
| 响应式列 | md:w-1/2 |
| Hover 效果 | hover:bg-blue-600 |
| 隐藏元素 | hidden |
| 响应式显示 | md:block |
| 网格 3 列 | grid grid-cols-3 |
| 间距 1rem | gap-4 |
| 边框 | border border-gray-300 |
| 深色模式 | dark:bg-gray-900 |
十、推荐资源
最后的话
如果你已经会 CSS,Tailwind 就是快速查表的过程。核心就是记住映射规则:
CSS属性名缩写 + 数值 = 类名
- 用响应式前缀
sm:/md:/lg: 适配不同屏幕
- 用伪类前缀
hover:/focus: 添加交互效果
一周内熟悉常见类名,一个月内成为高效用户。
附录:完整颜色系统快速查阅
Tailwind 预设颜色(共 15 种):
slate, gray, zinc, neutral, stone, red, orange, amber, yellow, lime, green, emerald, teal, cyan, sky, blue, indigo, violet, purple, fuchsia, pink, rose
每种颜色深度:50 → 100 → 200 → … → 900 → 950(越小越浅)
例如蓝色的所有深度:bg-blue-50 ~ bg-blue-950