· 技术备忘录 · 8 min read
用Tailwind CSS榨干性能:我的前端优化"斩杀线"清单
从60分到95分,我踩过的坑和总结的清单。这7个优化技巧,让你的Tailwind CSS项目快如闪电。
很多人觉得 Tailwind CSS 是”反模式”——把样式写在 HTML 里,又臭又长。
但我要说:Tailwind CSS 是性能优化的利器。
关键不在于工具本身,而在于你怎么用它。
今天,我把从 Lighthouse 60 分到 95 分的优化经验,整理成 7 个”斩杀线”清单。
优化前:你的 CSS 可能正在浪费资源
在讲优化之前,先看看你的项目可能存在的问题:
问题 1:Tailwind 生成了太多无用样式
/* 这是 Tailwind 默认配置生成的 CSS */
/* 包含了所有可能的颜色、间距、字体大小... */
/* 即使你只用到了其中的 10% */问题 2:没有开启 JIT 模式
/* 旧版 Tailwind 会生成所有可能的组合 */
/* w-1, w-2, w-3, ..., w-96 */
/* 新版 JIT 只生成你实际用到的 */问题 3:没有分离关键 CSS
/* 用户只需要首屏的样式 */
/* 但你把所有页面的样式都塞进了一个文件 */优化清单:从 60 分到 95 分
优化 1:开启 JIT 模式(必须做)
这是最基础也是最有效的优化。
// tailwind.config.js
/** @type {import('tailwindcss').Config} */
export default {
content: [
"./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}",
],
// 确保开启 JIT(Tailwind 3 及以上默认开启)
// 如果是旧版本,需要显式设置:
// mode: 'jit',
};效果:
- CSS 文件从 3MB 降到 150KB
- 构建时间从 10s 降到 1s
- 生产环境只包含你实际用到的样式
优化 2:移除未使用的默认样式
Tailwind 默认包含了很多你可能不需要的东西:
// tailwind.config.js
export default {
content: ["./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}"],
corePlugins: {
// 移除不需要的核心插件
preflight: false, // 如果你不需要 Tailwind 的基础重置样式
container: false, // 如果你不需要 container 类
aspectRatio: false, // 如果你不需要宽高比类
animation: false, // 如果你不需要动画
},
};效果:
- CSS 文件再减少 20-30KB
- 避免与其他 CSS 框架冲突
优化 3:自定义主题,只保留需要的
默认的 Tailwind 主题包含了太多颜色和间距:
// tailwind.config.js
export default {
content: ["./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}"],
theme: {
// 只保留你需要的颜色
colors: {
primary: '#3b82f6',
secondary: '#6366f1',
danger: '#ef4444',
success: '#22c55e',
// 移除不需要的颜色
},
// 只保留你需要的间距
spacing: {
0: '0',
2: '0.5rem',
4: '1rem',
8: '2rem',
12: '3rem',
16: '4rem',
// 移除不需要的间距
},
// 只保留你需要的字体大小
fontSize: {
sm: '0.875rem',
base: '1rem',
lg: '1.125rem',
xl: '1.25rem',
'2xl': '1.5rem',
// 移除不需要的字体大小
},
},
};效果:
- CSS 文件减少 30-50%
- 主题更简洁,维护更方便
优化 4:使用 @layer 组织代码
避免在 HTML 中写超长的类名,用 @layer 封装常用样式:
/* src/styles/tailwind.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer components {
.btn-primary {
@apply px-4 py-2 bg-primary text-white rounded-lg font-medium hover:bg-blue-600 transition-colors;
}
.card {
@apply bg-white rounded-xl shadow-sm border border-gray-100 p-6;
}
.text-gradient {
@apply bg-gradient-to-r from-primary to-secondary bg-clip-text text-transparent;
}
}然后在 HTML 中使用:
<button class="btn-primary">点击我</button>
<div class="card">内容卡片</div>
<h1 class="text-gradient">渐变标题</h1>效果:
- HTML 更简洁,可读性更好
- 样式复用,减少重复代码
- 便于主题切换和维护
优化 5:关键 CSS 分离(进阶)
对于大型项目,只加载首屏需要的 CSS:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import tailwind from '@astrojs/tailwind';
export default defineConfig({
integrations: [tailwind({
config: {
applyBaseStyles: false, // 禁用全局基础样式
},
})],
});/* src/styles/critical.css */
/* 首屏必须的样式 */
@tailwind base;
@layer base {
body {
@apply bg-gray-50 text-gray-900 font-sans;
}
}
@layer components {
/* 首屏需要的组件样式 */
.btn-primary {
@apply px-4 py-2 bg-primary text-white rounded-lg;
}
}/* src/styles/non-critical.css */
/* 非首屏样式,按需加载 */
@tailwind utilities;
@layer components {
/* 其他页面的组件样式 */
.blog-post {
@apply prose max-w-none;
}
}效果:
- 首屏 CSS 体积减少 60%
- First Contentful Paint 提升 0.5-1s
- Lighthouse Performance 分数提升 10-15 分
优化 6:压缩和缓存
确保你的构建工具正确压缩 CSS:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import tailwind from '@astrojs/tailwind';
import compress from 'astro-compress';
export default defineConfig({
integrations: [
tailwind(),
compress(), // 压缩 HTML、CSS、JS
],
});配置 Cloudflare Pages 的缓存策略:
# public/_headers
/*
Cache-Control: public, max-age=31536000, immutable效果:
- CSS 文件再压缩 20-30%
- 用户第二次访问时,浏览器直接使用缓存
- 减少服务器请求次数
优化 7:避免常见陷阱
以下是我踩过的坑,希望你能避开:
陷阱 1:过度使用任意值
<!-- ❌ 不好的做法 -->
<div class="w-[123px] h-[456px] bg-[#aabbcc]"></div>
<!-- ✅ 好的做法 -->
<div class="custom-box"></div>
<style>
.custom-box {
width: 123px;
height: 456px;
background-color: #aabbcc;
}
</style>原因: 任意值会增加 CSS 体积,且不利于维护。
陷阱 2:嵌套太多组件
<!-- ❌ 不好的做法 -->
<div class="flex items-center justify-center p-4">
<div class="bg-white rounded-lg shadow-sm">
<div class="p-6">
<div class="text-lg font-bold">标题</div>
</div>
</div>
</div>
<!-- ✅ 好的做法 -->
<div class="card-center">标题</div>
<style>
.card-center {
@apply flex items-center justify-center p-4;
@apply bg-white rounded-lg shadow-sm;
@apply text-lg font-bold;
}
</style>原因: 嵌套太深会增加 HTML 体积和渲染开销。
陷阱 3:滥用 responsive 前缀
<!-- ❌ 不好的做法 -->
<div class="text-sm md:text-base lg:text-lg xl:text-xl"></div>
<!-- ✅ 好的做法 -->
<div class="text-responsive"></div>
<style>
.text-responsive {
font-size: 0.875rem; /* sm */
@media (min-width: 768px) {
font-size: 1rem; /* md */
}
@media (min-width: 1024px) {
font-size: 1.125rem; /* lg */
}
}
</style>原因: 太多 responsive 前缀会增加 CSS 体积。
ZSX 的实际优化效果
优化前(WordPress + 传统 CSS):
- Lighthouse Performance:65
- First Contentful Paint:2.5s
- CSS 文件大小:2.8 MB
- 请求数:80+
优化后(Astro + Tailwind CSS):
- Lighthouse Performance:98
- First Contentful Paint:0.3s
- CSS 文件大小:120 KB
- 请求数:5
性能提升了 800%。
一句话总结
Tailwind CSS 不是性能杀手,滥用 Tailwind 才是。
遵循这 7 个优化技巧:
- ✅ 开启 JIT 模式
- ✅ 移除未使用的核心插件
- ✅ 自定义主题,只保留需要的
- ✅ 使用 @layer 组织代码
- ✅ 关键 CSS 分离(大型项目)
- ✅ 压缩和缓存
- ✅ 避免常见陷阱
你的前端性能,就能从”及格线”跳到”斩杀线”之上。
记住:性能优化不是一次性的工作,而是持续的过程。