Tailwind CSS v4 深入:基于 Rust 的新引擎与零配置体验

2769 字
14 分钟
Tailwind CSS v4 深入:基于 Rust 的新引擎与零配置体验

前言:Tailwind 的进化之路#

Tailwind CSS 自 2017 年诞生以来,从一个争议性的 utility-first 框架成长为前端最流行的 CSS 方案之一。每一个大版本都带来了显著的改进:

  • v1:确立 utility-first 理念
  • v2:JIT(Just-In-Time)引擎,按需生成
  • v3:JIT 成为默认,任意值语法 [],原生嵌套
  • v4全面重写——用 Rust 重建引擎,CSS-first 配置,零配置体验

Tailwind CSS v4 不是一次增量更新,而是一次从底层架构到使用方式的全面革新。本文将深入剖析 v4 的核心变化、迁移策略和最佳实践。

从 JavaScript 到 Rust:新引擎 Oxide#

为什么用 Rust 重写?#

Tailwind v3 的引擎完全用 JavaScript(Node.js)编写。虽然 JIT 模式已经很快了,但随着项目规模增长,构建时间仍然是痛点。v4 的引擎代号 Oxide,使用 Rust 编写核心逻辑,通过 NAPI-RS 桥接到 Node.js。

性能提升是惊人的:

指标v3v4提升
全量构建(大型项目)~800ms~100ms8x
增量构建~200ms~5ms40x
内存占用~150MB~20MB7.5x

这不仅仅是”快了一点”——对于大型项目来说,这意味着 HMR(热模块替换)几乎是瞬时的。

Lightning CSS 集成#

v4 内置了 Lightning CSS(同样由 Rust 编写)作为 CSS 解析器和转换器,替代了之前的 PostCSS + Autoprefixer 组合。

Lightning CSS 提供:

  • CSS 解析和序列化:比 PostCSS 快 100 倍以上
  • 自动浏览器前缀:不再需要 Autoprefixer
  • CSS 嵌套降级:自动将原生 CSS 嵌套转换为兼容语法
  • CSS 模块:原生支持
  • 代码压缩:内置 minification,不再需要 cssnano
/* 你写的代码 */
.card {
display: flex;
user-select: none;
&:hover {
transform: scale(1.02);
}
& .title {
color: oklch(0.6 0.2 250);
}
}
/* Lightning CSS 自动输出(针对目标浏览器) */
.card {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
.card:hover {
-webkit-transform: scale(1.02);
transform: scale(1.02);
}
.card .title {
color: rgb(0, 119, 255);
}

不再需要 PostCSS#

v4 最大的架构变化之一:PostCSS 不再是必需的依赖

# v3 的 postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
# v4:不需要 PostCSS 配置文件
# Tailwind 自己处理一切

当然,如果你仍然需要 PostCSS 生态中的其他插件,v4 也提供了 PostCSS 插件模式作为兼容方案。

CSS-First 配置:告别 tailwind.config.js#

从 JS 配置到 CSS 配置#

v3 中,所有配置都在 tailwind.config.js 中:

tailwind.config.js
module.exports = {
content: ['./src/**/*.{html,js,vue}'],
theme: {
extend: {
colors: {
brand: {
50: '#eff6ff',
500: '#3b82f6',
900: '#1e3a5f',
},
},
fontFamily: {
sans: ['Inter', 'system-ui', 'sans-serif'],
},
spacing: {
'18': '4.5rem',
},
},
},
plugins: [
require('@tailwindcss/typography'),
],
};

v4 中,配置直接写在 CSS 文件里:

app.css
@import "tailwindcss";
@theme {
--color-brand-50: #eff6ff;
--color-brand-500: #3b82f6;
--color-brand-900: #1e3a5f;
--font-sans: "Inter", system-ui, sans-serif;
--spacing-18: 4.5rem;
}

就这样。没有 JS 配置文件。 Tailwind 直接从你的 CSS 中读取配置。

@theme 详解#

@theme 是 v4 引入的新 at-rule,用于定义设计令牌(design tokens)。它本质上是在定义 CSS 自定义属性,但 Tailwind 会识别这些变量并自动生成对应的 utility classes。

@import "tailwindcss";
@theme {
/* 颜色 —— 自动生成 bg-primary, text-primary, border-primary 等 */
--color-primary: #3b82f6;
--color-secondary: #8b5cf6;
--color-accent: #f59e0b;
/* 间距 —— 自动生成 p-xs, m-xs, gap-xs 等 */
--spacing-xs: 0.25rem;
--spacing-sm: 0.5rem;
--spacing-md: 1rem;
--spacing-lg: 2rem;
--spacing-xl: 4rem;
/* 圆角 —— 自动生成 rounded-card 等 */
--radius-card: 12px;
--radius-button: 8px;
--radius-pill: 9999px;
/* 阴影 —— 自动生成 shadow-soft 等 */
--shadow-soft: 0 2px 8px rgba(0, 0, 0, 0.08);
--shadow-medium: 0 4px 16px rgba(0, 0, 0, 0.12);
--shadow-strong: 0 8px 32px rgba(0, 0, 0, 0.2);
/* 字体大小 */
--text-display: 3.5rem;
--text-heading: 2rem;
--text-body: 1rem;
--text-caption: 0.875rem;
/* 行高 */
--leading-display: 1.1;
--leading-heading: 1.3;
--leading-body: 1.7;
/* 断点 */
--breakpoint-sm: 640px;
--breakpoint-md: 768px;
--breakpoint-lg: 1024px;
--breakpoint-xl: 1280px;
/* 动画 */
--ease-spring: cubic-bezier(0.2, 0, 0, 1);
--ease-bounce: cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

定义完后,你可以直接使用这些生成的 utility classes:

<div class="bg-primary text-white p-lg rounded-card shadow-soft">
<h1 class="text-display leading-display">标题</h1>
<p class="text-body leading-body text-secondary">内容</p>
<button class="bg-accent rounded-button px-md py-sm">
按钮
</button>
</div>

覆盖默认主题#

@import "tailwindcss";
@theme {
/* 完全替换默认颜色(使用 --color-* 前缀) */
--color-*: initial; /* 清除所有默认颜色 */
/* 定义你自己的颜色系统 */
--color-base: #1a1a2e;
--color-surface: #16213e;
--color-overlay: #0f3460;
--color-primary: #e94560;
--color-text: #eaeaea;
--color-muted: #a0a0b0;
}

使用 --<namespace>-*: initial 可以清除该命名空间下的所有默认值,实现完全自定义。

暗色模式#

v4 的暗色模式配置也变为 CSS-first:

@import "tailwindcss";
@theme {
--color-bg: #ffffff;
--color-text: #1a1a2e;
--color-card: #f8fafc;
}
/* 暗色模式变量 */
@media (prefers-color-scheme: dark) {
@theme {
--color-bg: #0f172a;
--color-text: #e2e8f0;
--color-card: #1e293b;
}
}

或者使用 class 策略:

@import "tailwindcss";
@variant dark (&:where(.dark, .dark *));

这让 dark: 变体生效于 .dark 类下:

<html class="dark">
<body class="bg-bg text-text">
<div class="bg-card dark:bg-card">
<!-- 内容 -->
</div>
</body>
</html>

零配置自动内容检测#

告别 content 配置#

v3 中你必须显式指定内容扫描路径:

// v3
module.exports = {
content: [
'./src/**/*.{html,js,ts,vue,svelte}',
'./index.html',
],
};

v4 自动检测项目中的模板文件。它会:

  1. 读取项目的 package.json 和构建工具配置
  2. 自动识别框架(Vue、Svelte、Astro 等)
  3. 扫描相关源文件中的 class 使用

你不需要配置任何 content 路径。

如果需要手动指定(例如包含外部目录),可以在 CSS 中使用 @source

@import "tailwindcss";
/* 额外扫描路径 */
@source "../shared-components/**/*.vue";
@source "../content/**/*.md";

如果需要排除某些路径:

@source not "./src/legacy/**";

新的导入方式#

简化导入#

v3 需要三行导入:

/* v3 */
@tailwind base;
@tailwind components;
@tailwind utilities;

v4 只需一行:

/* v4 */
@import "tailwindcss";

如果你需要在各层之间插入自定义样式,可以使用分层导入:

@import "tailwindcss/preflight" layer(base);
@import "tailwindcss/utilities" layer(utilities);
/* 在 base 和 utilities 之间插入自定义样式 */
@layer base {
body {
font-family: var(--font-sans);
color: var(--color-text);
background: var(--color-bg);
}
h1, h2, h3 {
font-weight: 700;
line-height: var(--leading-heading);
}
}

新功能与语法变化#

新的 3D Transform Utilities#

v4 新增了 3D 变换相关的 utility classes:

<!-- 3D 变换 -->
<div class="rotate-x-45 rotate-y-30 perspective-800">
3D 旋转的元素
</div>
<!-- 背面可见性 -->
<div class="backface-hidden">
翻转卡片的正面
</div>
<!-- 变换风格 -->
<div class="transform-3d">
保持 3D 变换上下文
</div>

改进的渐变语法#

<!-- 线性渐变 -->
<div class="bg-linear-to-r from-blue-500 to-purple-500">
线性渐变
</div>
<!-- 圆锥渐变(新增) -->
<div class="bg-conic from-red-500 via-yellow-500 to-green-500">
圆锥渐变
</div>
<!-- 径向渐变 -->
<div class="bg-radial from-white to-gray-200">
径向渐变
</div>

容器查询 Utilities#

v4 原生支持 Container Queries 相关的 utility classes:

<!-- 声明容器 -->
<div class="@container">
<!-- 容器查询断点 -->
<div class="@sm:grid-cols-2 @lg:grid-cols-3 @xl:grid-cols-4">
<div class="@sm:flex-row flex-col">
自适应容器宽度的布局
</div>
</div>
</div>
<!-- 命名容器 -->
<div class="@container/sidebar">
<div class="@sm/sidebar:hidden">
在 sidebar 容器较窄时隐藏
</div>
</div>

改进的任意值语法#

<!-- v3 语法仍然支持 -->
<div class="bg-[#1da1f2]">...</div>
<!-- v4 新增:CSS 变量简写 -->
<div class="bg-(--my-color)">...</div>
<!-- 等同于 -->
<div class="bg-[var(--my-color)]">...</div>

@utility 自定义 Utility#

v4 引入了 @utility 来定义自定义 utility classes,替代了 v3 中的 @layer utilities + 手动类名:

/* 定义自定义 utility */
@utility content-grid {
display: grid;
grid-template-columns:
[full-start] minmax(1rem, 1fr)
[content-start] minmax(0, 65ch)
[content-end] minmax(1rem, 1fr)
[full-end];
& > * {
grid-column: content;
}
}
@utility text-balance {
text-wrap: balance;
}
@utility scrollbar-hidden {
scrollbar-width: none;
&::-webkit-scrollbar {
display: none;
}
}
@utility glass {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(16px);
border: 1px solid rgba(255, 255, 255, 0.2);
}

使用:

<main class="content-grid">
<h1 class="text-balance">这是一个很长的标题,会自动平衡换行</h1>
<div class="scrollbar-hidden overflow-y-auto h-96">
隐藏滚动条的可滚动区域
</div>
<div class="glass rounded-xl p-6">
毛玻璃效果的卡片
</div>
</main>

@variant 自定义变体#

/* 自定义变体 */
@variant hocus (&:hover, &:focus-visible);
@variant group-active (group:active &);
@variant scrolled (.scrolled &);
/* 基于数据属性的变体 */
@variant data-loading (&[data-loading="true"]);
@variant data-active (&[data-state="active"]);
<button class="hocus:bg-blue-600 hocus:scale-105">
hover 或 focus 时变化
</button>
<div class="data-loading:opacity-50 data-loading:pointer-events-none"
data-loading="true">
加载中状态
</div>

从 v3 迁移到 v4#

自动迁移工具#

Tailwind 提供了官方迁移工具:

Terminal window
npx @tailwindcss/upgrade

它会自动:

  1. 更新 package.json 中的依赖
  2. tailwind.config.js 转换为 CSS @theme 配置
  3. 更新 CSS 导入语法
  4. 移除不再需要的 PostCSS 配置
  5. 更新已弃用的 class 名称

手动迁移清单#

如果你需要手动迁移,以下是关键变化:

1. 安装新版本#

Terminal window
# 移除旧依赖
npm uninstall tailwindcss postcss autoprefixer
# 安装 v4
npm install tailwindcss@4

2. 更新 CSS 入口文件#

/* 之前 */
@tailwind base;
@tailwind components;
@tailwind utilities;
/* 之后 */
@import "tailwindcss";

3. 迁移配置#

tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
brand: '#3b82f6',
},
borderRadius: {
'4xl': '2rem',
},
},
},
};
/* 之后: 在 CSS 中 */
@import "tailwindcss";
@theme {
--color-brand: #3b82f6;
--radius-4xl: 2rem;
}

4. 更新构建配置#

Vite:

vite.config.js
import tailwindcss from '@tailwindcss/vite';
export default {
plugins: [
tailwindcss(),
],
};

其他构建工具(PostCSS 兼容模式):

postcss.config.js
export default {
plugins: {
'@tailwindcss/postcss': {},
},
};

5. 已弃用/重命名的类#

# 一些 v3 中的类在 v4 中重命名
bg-opacity-50 → bg-black/50 (使用颜色不透明度语法)
text-opacity-75 → text-black/75
decoration-slice → box-decoration-slice
decoration-clone → box-decoration-clone
bg-gradient-to-r → bg-linear-to-r

与构建工具集成#

Vite(推荐)#

v4 对 Vite 有一流支持:

vite.config.js
import { defineConfig } from 'vite';
import tailwindcss from '@tailwindcss/vite';
export default defineConfig({
plugins: [
tailwindcss(),
],
});

这种方式最快,因为 Tailwind 直接集成到 Vite 的构建管线中,跳过了 PostCSS 层。

CLI 独立使用#

v4 提供了独立的 CLI 工具,不依赖任何构建工具:

Terminal window
# 安装
npm install tailwindcss @tailwindcss/cli
# 构建
npx @tailwindcss/cli -i src/input.css -o dist/output.css
# 监听模式
npx @tailwindcss/cli -i src/input.css -o dist/output.css --watch
# 压缩
npx @tailwindcss/cli -i src/input.css -o dist/output.css --minify

Astro#

astro.config.mjs
import { defineConfig } from 'astro/config';
import tailwindcss from '@tailwindcss/vite';
export default defineConfig({
vite: {
plugins: [tailwindcss()],
},
});

性能对比深度分析#

构建性能#

以一个包含 500+ 组件的大型 Vue 项目为例:

v3 全量构建:
解析配置: 15ms
扫描内容: 120ms
生成 CSS: 650ms
PostCSS 处理: 80ms
Autoprefixer: 45ms
总计: ~910ms
v4 全量构建:
Oxide 引擎处理: 85ms
Lightning CSS: 12ms
总计: ~97ms

运行时性能#

v4 生成的 CSS 更高效:

/* v3 可能生成冗余的 CSS 变量层 */
.bg-blue-500 {
--tw-bg-opacity: 1;
background-color: rgb(59 130 246 / var(--tw-bg-opacity));
}
/* v4 更直接 */
.bg-blue-500 {
background-color: #3b82f6;
}
/* 当使用不透明度时才引入变量 */
.bg-blue-500\/50 {
background-color: rgb(59 130 246 / 0.5);
}

产出文件大小#

场景v3 (gzip)v4 (gzip)
小型项目~8KB~6KB
中型项目~15KB~11KB
大型项目~25KB~18KB

v4 产出更小主要得益于 Lightning CSS 的优化压缩和更少的运行时辅助变量。

最佳实践#

1. 拥抱 CSS-first 配置#

/* ✅ 在 CSS 中定义设计令牌 */
@theme {
--color-brand: oklch(0.65 0.24 265);
--font-display: "Cal Sans", sans-serif;
}
/* ❌ 不要试图保留 JS 配置文件 */

2. 利用原生 CSS 特性#

v4 鼓励使用现代 CSS 特性:

/* 使用 CSS 嵌套(Tailwind 原生支持) */
.card {
@apply rounded-xl bg-white shadow-soft;
&:hover {
@apply shadow-medium;
}
& .title {
@apply text-heading font-bold;
}
}
/* 使用 oklch 色彩空间 */
@theme {
--color-brand: oklch(0.65 0.24 265);
--color-brand-light: oklch(0.85 0.12 265);
--color-brand-dark: oklch(0.45 0.24 265);
}

3. 组件提取策略#

/* 使用 @utility 创建可复用的模式 */
@utility btn {
display: inline-flex;
align-items: center;
justify-content: center;
padding: var(--spacing-sm) var(--spacing-md);
border-radius: var(--radius-button);
font-weight: 600;
transition: all 0.15s ease;
cursor: pointer;
}
@utility btn-primary {
@apply btn;
background: var(--color-primary);
color: white;
&:hover {
filter: brightness(1.1);
}
}

4. 性能优化#

/* 限制扫描范围 */
@source "./src/components/**/*.vue";
@source "./src/pages/**/*.astro";
/* 排除不需要扫描的目录 */
@source not "./src/**/*.test.ts";
@source not "./src/**/*.spec.ts";

总结#

Tailwind CSS v4 是一次大胆的重写,它带来了:

  • 10-40x 的性能提升:Rust 引擎 + Lightning CSS 让构建飞快
  • 零配置体验:自动内容检测,无需手动指定扫描路径
  • CSS-first 配置@theme 替代 JS 配置文件,更直觉
  • 更少的依赖:不再需要 PostCSS、Autoprefixer、cssnano
  • 更小的产出:Lightning CSS 的优化让 CSS 文件更精简
  • 现代 CSS 拥抱:原生嵌套、oklch、Container Queries 等

如果你正在启动新项目,v4 是不二之选。如果是已有项目,官方迁移工具可以帮你平滑过渡。

Tailwind CSS v4 不仅仅是一个 CSS 框架的更新——它代表了前端工具链从 JavaScript 向 Rust 迁移的大趋势,也展示了 CSS-first 开发体验的未来方向。

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

Tailwind CSS v4 深入:基于 Rust 的新引擎与零配置体验
https://boke.hackerdream.xyz/posts/tailwind-v4-deep-dive/
作者
晴天
发布于
2026-02-18
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
晴天
Hello, I'm 晴天.
公告
欢迎来到我的博客!这是一则示例公告。
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
站点统计
文章
125
分类
17
标签
287
总字数
257,955
运行时长
0
最后活动
0 天前

目录