Tailwind CSS 本质上是一个 PostCSS 插件,其底层工作原理可以拆解为以下几个关键步骤,结合了 PostCSS 的处理能力和 Tailwind 特有的生成逻辑:
@tailwind base;
, @tailwind components;
, @tailwind utilities;
)。/* 输入 */
@tailwind utilities;
/* 输出(示例) */
.p-4 { padding: 1rem; }
.text-red-500 { color: #ef4444; }
/* ...数千个实用类 */
扫描项目文件
↓
提取 Tailwind CSS 类名
↓
匹配配置 (tailwind.config.js)
↓
生成原子化 CSS
↓
输出到最终的 CSS 文件中
(1)扫描项目文件
Tailwind 根据配置的 content
项,扫描 HTML、JSX、TSX、Vue 文件,提取出所用到的类名。
// tailwind.config.js
module.exports = {
content: ["./src/**/*.{js,jsx,ts,tsx,html,vue}"],
}
例如你在 JSX 中写:
Hello
Tailwind 就会提取出类名:
["text-xl", "font-bold", "text-blue-500"]
(2)匹配配置
Tailwind 内部维护一套主题配置(theme
):
theme: {
colors: { blue: {500: '#3B82F6'} },
fontSize: { xl: '1.25rem' },
fontWeight: { bold: 700 },
}
扫描后找到匹配的设计 Token(颜色、字体大小等)。
(3)生成原子化 CSS
每个类名会对应生成一个原子 CSS 类:
.text-xl { font-size: 1.25rem; }
.font-bold { font-weight: 700; }
.text-blue-500 { color: #3B82F6; }
(4)输出 CSS 文件
最终输出到 index.css
或 main.css
:
.text-xl { font-size: 1.25rem; }
.font-bold { font-weight: 700; }
.text-blue-500 { color: #3B82F6; }
tailwind.config.js
并与默认配置合并。theme
部分(如颜色、间距、字体等),生成对应的实用类。theme.spacing.4
→ 生成 .p-4 { padding: 1rem; }
。class="bg-blue-500"
)。mt-[13px]
),JIT 引擎实时计算样式并注入 CSS。使用:
<div class="w-[243px] bg-[#e6e6e6] text-[17px]">Hellodiv>
动态生成:
.w-\[243px\] { width: 243px; }
.bg-\[\#e6e6e6\] { background-color: #e6e6e6; }
.text-\[17px\] { font-size: 17px; }
plugins: []
配置加载第三方或自定义插件,动态添加新的实用类。@apply
指令:在 CSS 中复用样式:.btn {
@apply px-4 py-2 bg-blue-500; /* 解析为组合样式 */
}
.px-4
, .py-2
, .bg-blue-500
的规则,将其声明复制到 .btn
中。Tailwind 本身还可以通过插件机制扩展:
const plugin = require('tailwindcss/plugin')
module.exports = {
plugins: [
plugin(({ addUtilities, addComponents, theme }) => {
addUtilities({
'.text-shadow': {
'text-shadow': '2px 2px #000'
}
})
addComponents({
'.btn-primary': {
padding: '.5rem 1rem',
backgroundColor: theme('colors.blue.500'),
color: '#fff'
}
})
})
]
}
插件通过:
addUtilities
添加工具类(例如 .text-shadow
)。addComponents
添加组件类(例如 .btn-primary
)。@tailwind
指令为生成的 CSS 规则。@apply
, @layer
等自定义指令。// Tailwind 作为 PostCSS 插件的伪代码
export default postcss.plugin('tailwind', (options) => {
return (root, result) => {
// 1. 读取配置
const config = resolveConfig(options.config);
// 2. 扫描指令(如 @tailwind utilities)
root.walkAtRules('tailwind', (rule) => {
const layer = rule.params;
// 3. 根据层(base/components/utilities)生成 CSS
const generatedRules = generateRulesForLayer(layer, config);
// 4. 替换指令为生成的 CSS
rule.replaceWith(generatedRules);
});
// 5. 处理 @apply 等自定义指令
processApplyDirectives(root, config);
};
});
技术点 | 说明 |
---|---|
PostCSS 插件机制 | Tailwind 本质就是 PostCSS 插件,利用 AST 生成 CSS |
实用类原子化 | 将样式拆解为最小颗粒度的类 |
AST 解析和构建 | 解析源码 → 提取类名 → 匹配 Token → 生成 CSS |
JIT 模式 | 按需实时编译任意值类名 实现极致的性能与体积优化 |
插件系统 | 使用 JS API 添加额外工具类、组件类 |
通过这种设计,Tailwind CSS 在保持灵活性的同时,解决了传统 CSS 框架的冗余问题,成为现代 Web 开发的高效工具。