海川的日志

快速掌握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: 1remclass="p-4"
display: flexclass="flex"
background-color: rgb(59, 130, 246)class="bg-blue-500"
border-radius: 0.5remclass="rounded-lg"

规律:一个属性值 = 一个简短的 class,在 HTML 的 class 属性中组合使用。


二、CSS 属性映射表(快速查询)

1. 尺寸和间距

Width(宽度)

CSSTailwind
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: 0min-w-0
max-width: 28rem (448px)max-w-md

单位系统:每级代表4px,共有0-96级,即0px-384px1rem = 16px = 4级

Height(高度)

CSSTailwind
height: 100%h-full
height: 100vhh-screen
height: 16rem (256px)h-64
min-height: 100vhmin-h-screen
max-height: 20rem (320px)max-h-80

Padding(内边距)

CSSTailwind
padding: 1remp-4
padding: 0.5rem 1rempx-4 py-2
padding-left: 2rempl-8
padding-top: 1.5rempt-6

快速记忆p=padding, m=margin, px=horizontal, py=vertical

Margin(外边距)

CSSTailwind
margin: 0m-0
margin: 1remm-4
margin: 0 automx-auto
margin-bottom: 2remmb-8
margin-left: -1rem-ml-4

Gap(间距)

CSSTailwind
gap: 1remgap-4
gap: 1.5remgap-6
column-gap: 1remgap-x-4
row-gap: 2remgap-y-8

2. 布局

Display(显示方式)

CSSTailwind
display: blockblock
display: inlineinline
display: inline-blockinline-block
display: flexflex
display: gridgrid
display: nonehidden

Flexbox(一维布局)

CSSTailwind
flex-direction: rowflex-row (默认)
flex-direction: columnflex-col
justify-content: centerjustify-center
justify-content: space-betweenjustify-between
align-items: centeritems-center
align-items: flex-enditems-end
flex: 1 (自动扩展)flex-1
flex-wrap: wrapflex-wrap

常用组合flex items-center justify-between = 水平均匀分布且竖直居中

Grid(二维布局)

CSSTailwind
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: 1remgap-4

3. 颜色和背景

背景色

CSSTailwind
background-color: #3b82f6 (蓝色)bg-blue-500
background-color: #ef4444 (红色)bg-red-500
background-color: whitebg-white
background-color: transparentbg-transparent

颜色深度:50(最浅)→ 100 → 200 → … → 900(最深)

文字色

CSSTailwind
color: whitetext-white
color: #1f2937 (深灰)text-gray-800
color: #6366f1 (靛蓝)text-indigo-500

Border(边框)

CSSTailwind
border: 1px solid blackborder border-black
border: 2px solid #e5e7ebborder-2 border-gray-200
border-left: 3px solid blueborder-l-4 border-blue-500
border-radius: 0.5remrounded-lg
border-radius: 50%rounded-full

4. 文字样式

字体大小和行高

CSSTailwind
font-size: 12pxtext-xs
font-size: 14pxtext-sm
font-size: 16pxtext-base
font-size: 20pxtext-xl
font-size: 32pxtext-4xl

字体权重

CSSTailwind
font-weight: 400 (正常)font-normal
font-weight: 600 (加粗)font-semibold
font-weight: 700 (很粗)font-bold

文字对齐

CSSTailwind
text-align: lefttext-left
text-align: centertext-center
text-align: righttext-right
text-align: justifytext-justify

其他文字效果

CSSTailwind
text-decoration: underlineunderline
text-decoration: noneno-underline
text-transform: uppercaseuppercase
text-transform: lowercaselowercase
overflow: hidden; text-overflow: ellipsis; white-space: nowraptruncate
限制3行,超出省略号line-clamp-3

5. 阴影和效果

CSSTailwind
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.5opacity-50
opacity: 0.75opacity-75

6. 位置和定位

CSSTailwind
position: staticstatic
position: relativerelative
position: absoluteabsolute
position: fixedfixed
position: stickysticky
top: 0top-0
right: 1remright-4
z-index: 10z-10

三、响应式设计(移动优先)

Tailwind 默认是移动端,然后用前缀适配更大屏幕:

屏幕宽度前缀用法例
< 640px(无)w-full
>= 640pxsm:sm:w-1/2
>= 768pxmd:md:w-1/3
>= 1024pxlg:lg:w-1/4
>= 1280pxxl: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 对比

语法对比

需求BootstrapTailwind
创建按钮<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">

学习成本

方面BootstrapTailwind
初学者友好⭐⭐⭐⭐⭐
要记多少类30-50个学会映射规则即可
定制难度中等简单
文件体积50-130KB15-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-primarytext-primarypx-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
内边距 1remp-4
外边距 automx-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
间距 1remgap-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

每种颜色深度:50100200 → … → 900950(越小越浅)

例如蓝色的所有深度:bg-blue-50 ~ bg-blue-950