Tailwind CSS 是由 Adam Wathan 于 2017 年创建的 CSS 框架,采用 "实用工具优先"(Utility-First) 的设计理念。与 Bootstrap 等传统框架不同,Tailwind 提供低级的原子化工具类,让开发者自由组合构建自定义设计。
Tailwind CSS 的核心定位是 原子化 CSS 框架。它提供了:
<!-- 传统 CSS 方式 -->
<style>
.card {
padding: 1.5rem;
background: white;
border-radius: 0.5rem;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
</style>
<div class="card">内容</div>
<!-- Tailwind 方式(原子化) -->
<div class="p-6 bg-white rounded-lg shadow-md">内容</div>
<!-- 对比:一个类 vs 多个类 -->
/* Tailwind 的每个类只做一件事 */
p-6 -> padding: 1.5rem
bg-white -> background: white
rounded-lg -> border-radius: 0.5rem
shadow-md -> box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1)
<!-- 布局 -->
<div class="flex justify-between items-center">
<span>左侧</span>
<span>右侧</span>
</div>
<!-- 网格 -->
<div class="grid grid-cols-3 gap-4">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<!-- 间距 -->
<div class="p-4 m-2 space-y-4">
<div>上下间距 4</div>
<div>同样间距</div>
</div>
<!-- 文字 -->
<p class="text-lg font-bold text-center text-gray-800">
大号粗体居中灰色文字
</p>
<!-- 颜色 -->
<div class="bg-blue-500 text-white hover:bg-blue-700">
蓝色背景
</div>
<!-- 边框 -->
<div class="border-2 border-red-500 rounded-full">
红色边框圆角
</div>
<!-- 阴影 -->
<div class="shadow-lg hover:shadow-xl">阴影</div>
<!-- 响应式断点 -->
<!-- sm: 640px, md: 768px, lg: 1024px, xl: 1280px, 2xl: 1536px -->
<div class="text-sm md:text-base lg:text-lg">
响应式字体大小
</div>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
响应式网格
</div>
<div class="hidden md:block">
中屏以上显示
</div>
<div class="block md:hidden">
中屏以下显示
</div>
<!-- 响应式间距 -->
<div class="p-2 md:p-4 lg:p-8">响应式内边距</div>
<!-- 暗色模式 -->
<div class="bg-white dark:bg-gray-800 text-black dark:text-white">
自动适配暗色模式
<button class="bg-blue-500 dark:bg-blue-700">按钮</button>
</div>
<!-- 自定义暗色模式 -->
<div class="shadow dark:shadow-gray-700">暗色阴影</div>
<!-- 启用暗色模式(在配置文件中) -->
// tailwind.config.js
module.exports = {
darkMode: "class", // 或 "media"
// ...
}
// tailwind.config.js
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {
colors: {
brand: {
light: "#4f8cf7",
DEFAULT: "#2563eb",
dark: "#1d4ed8"
}
},
fontFamily: {
sans: ["Inter", "sans-serif"],
mono: ["JetBrains Mono", "monospace"]
},
spacing: {
"18": "4.5rem",
"88": "22rem"
},
animation: {
"spin-slow": "spin 3s linear infinite"
},
screens: {
"xs": "480px",
"3xl": "1792px"
}
}
},
plugins: []
}
// 使用 @apply 组合类
// styles.css
@tailwind base;
@tailwind components;
@tailwind utilities;
.btn-primary {
@apply px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-700;
}
.card {
@apply p-6 bg-white rounded-lg shadow-md;
}
// 在 HTML 中使用
<button class="btn-primary">按钮</button>
<div class="card">卡片内容</div>
// 或者使用组件库(如 DaisyUI)
// 安装 daisyui 作为插件
// React + Tailwind CSS
const Button = ({ children, variant = "primary" }) => {
const variants = {
primary: "bg-blue-500 hover:bg-blue-700",
secondary: "bg-gray-500 hover:bg-gray-700",
danger: "bg-red-500 hover:bg-red-700"
};
return (
<button
className={`px-4 py-2 text-white rounded-lg transition-colors ${variants[variant]}`}
>
{children}
</button>
);
};
// 使用
const App = () => (
<div className="p-8 max-w-4xl mx-auto">
<h1 className="text-3xl font-bold text-center mb-8">
欢迎使用 Tailwind CSS
</h1>
<div className="flex gap-4">
<Button variant="primary">主要</Button>
<Button variant="secondary">次要</Button>
<Button variant="danger">危险</Button>
</div>
</div>
);
<!-- Vue + Tailwind CSS -->
<template>
<div class="p-8 max-w-4xl mx-auto">
<h1 class="text-3xl font-bold text-center mb-8">
Vue + Tailwind
</h1>
<div class="flex flex-col gap-4">
<input
v-model="name"
class="border-2 border-gray-300 rounded-lg p-2 focus:border-blue-500 focus:outline-none"
placeholder="输入姓名"
/>
<button
@click="handleClick"
class="bg-blue-500 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition-colors"
>
点击我
</button>
<p class="text-lg text-gray-700">
你好, {{ name || "访客" }}!
</p>
</div>
<!-- 条件渲染 -->
<div v-if="show" class="mt-4 p-4 bg-green-100 border border-green-400 rounded-lg">
这是条件渲染的内容
</div>
</div>
</template>
<script setup>
import { ref } from "vue";
const name = ref("");
const show = ref(true);
const handleClick = () => {
show.value = !show.value;
};
</script>
| 对比项 | Tailwind | Bootstrap | CSS-in-JS |
|---|---|---|---|
| 设计理念 | 原子化 | 组件化 | 组件级 |
| 学习曲线 | 中等 | 平缓 | 中等 |
| 定制性 | 极高 | 中等 | 高 |
| 包体积 | 极小(JIT) | 大 | 中等 |
| 设计一致性 | 高 | 高 | 依赖团队 |
| 适用场景 | 定制设计 | 快速原型 | 组件库 |
Tailwind 安装、核心概念、常用工具类(布局/间距/文字/颜色)
响应式设计、暗色模式、自定义配置
@apply 组合、插件开发、JIT 模式
与 React/Vue/Svelte 集成、组件开发、生产优化
Tailwind CSS 是样式开发的"乐高"。
它用 原子化工具类、JIT 编译、完全可定制 让 CSS 开发变得高效而一致。Tailwind 是现代前端开发的最佳样式方案之一。
"Tailwind 让 CSS 开发从复杂变得简单。" 🎨