关于 tailwindcss-debugging
用于排查 Tailwind CSS 样式和构建问题的技能,面向开发者。它涵盖内容检测、CSS 冲突、v4 迁移、插件配置及生成 CSS 的检查。
主要功能
- 01诊断样式未生效的问题
- 02检查内容检测与类名
- 03排查 Tailwind CSS v4 迁移问题
- 04检查 CSS 冲突与导入顺序
- 05检查生成的 CSS 和构建输出
适用场景
- 01查找实用类缺失的原因
- 02解决 PostCSS 或插件配置错误
- 03排查样式被其他规则覆盖
- 04检查 CSS 输出大小或构建问题
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ josiahsiegel/ claude-plugin-marketplace --skill tailwindcss-debugging安装后在 Claude Code 中输入/tailwindcss-debugging确认已加载。
附带文件references/troubleshooting-guide.md
详细介绍
Tailwind CSS 调试与故障排查
常见问题与解决方案
1. 样式未生效
检查内容检测
v4 会自动检测内容,但如果样式缺失:
/* Explicitly specify sources */
@import "tailwindcss";
@source "./src/**/*.{html,js,jsx,ts,tsx,vue,svelte}";
验证类名
<div class={`text-${color}-500`}>
<div class={color === 'blue' ? 'text-blue-500' : 'text-red-500'}>
检查构建流程
# Restart dev server
npm run dev
# Clear cache and rebuild
rm -rf node_modules/.vite
npm run build
2. v4 迁移问题
PostCSS 插件已更改
// OLD (v3)
export default {
plugins: {
tailwindcss: {},
autoprefixer: {}
}
}
// NEW (v4)
export default {
plugins: {
'@tailwindcss/postcss': {}
}
}
配置已移至 CSS
/* v4 - Configure in CSS, not JS */
@import "tailwindcss";
@theme {
--color-primary: oklch(0.6 0.2 250);
}
暗色模式变体
/* v4 - Add if using selector strategy */
@custom-variant dark (&:where(.dark, .dark *));
3. 类被覆盖
检查优先级
/* Browser DevTools: Inspect element → Styles panel */
/* Look for crossed-out styles */
解决方案
<div class="!mt-0">
<div class="[&]:mt-0">
检查导入顺序
/* Your custom CSS should come after Tailwind */
@import "tailwindcss";
@import "./custom.css"; /* After Tailwind */
4. Typography 插件问题
样式未生效
/* Ensure plugin is loaded */
@plugin "@tailwindcss/typography";
工具类被 Prose 覆盖
<article class="prose prose-h1:text-4xl prose-a:text-blue-600">
<article class="prose">
<div class="not-prose">
<CustomComponent />
</div>
</article>
5. Forms 插件问题
样式未应用于普通输入框
<input type="text" />
<input />
使用类策略
@plugin "@tailwindcss/forms" {
strategy: class;
}
<input type="text" class="form-input" />
调试工具
VS Code 扩展
# Install Tailwind CSS IntelliSense
code --install-extension bradlc.vscode-tailwindcss
功能:
- 类名自动补全
- 悬停预览 CSS
- 错误检查
- 颜色装饰器
调试屏幕插件
npm install -D @tailwindcss/debug-screens
@plugin "@tailwindcss/debug-screens";
<body class="debug-screens">
浏览器开发者工具
- 检查元素 → 查看计算后的样式
- 样式面板 → 查看应用了哪些规则
- 筛选 → 搜索 Tailwind 类
- 计算值标签页 → 查看最终计算值
检查生成的 CSS
# Output CSS to file for inspection
npx tailwindcss -o output.css --content './src/**/*.{html,js}'
# With verbose logging
DEBUG=tailwindcss:* npm run build
v4 特定调试
检查插件加载情况
# Look for plugin-related errors
npm run build 2>&1 | grep -i plugin
验证 CSS 变量输出
/* In browser DevTools, check :root for variables */
:root {
--color-blue-500: oklch(...);
--spacing-4: 1rem;
}
内容检测问题
/* Add explicit sources if auto-detection fails */
@source "./src/**/*.tsx";
@source "./components/**/*.tsx";
/* Exclude paths */
@source not "./src/generated/**";
常见错误消息
“Cannot find module '@tailwindcss/postcss'”
npm install -D @tailwindcss/postcss
“Unknown at-rule @theme”
使用了 v3 工具链和 v4 语法。请更新构建设置:
npm install -D tailwindcss@latest @tailwindcss/postcss@latest
“Class 'X' doesn't exist”
动态类生成问题:
// BAD
const classes = `bg-${dynamic}-500`
// GOOD
const colorMap = {
primary: 'bg-blue-500',
danger: 'bg-red-500'
}
const classes = colorMap[dynamic]
“开发过程中样式未更新”
# Restart dev server
npm run dev
# Clear Vite cache
rm -rf node_modules/.vite
# Clear Next.js cache
rm -rf .next
性能调试
CSS 输出过大
# Check CSS file size
ls -lh dist/assets/*.css
# If too large, check for:
# 1. Dynamic class generation
# 2. Unnecessary safelisting
# 3. Unused plugins
构建缓慢
# Time the build
time npm run build
# v4 should be very fast
# Full build: <1s
# Incremental: microseconds
调试清单
初始设置
- 导入正确:
@import "tailwindcss"; - PostCSS 插件:
@tailwindcss/postcss(而非tailwindcss) - Vite 插件:
@tailwindcss/vite(如果使用 Vite) - 在入口文件中导入 CSS 文件
- 更改后重启开发服务器
样式未生效
- 类名完整(未动态生成)
- 文件位于内容路径中
- 已清除浏览器缓存
- 不存在 CSS 优先级冲突
- 在开发者工具中检查是否有样式被覆盖
迁移后
- 已移除或转换 tailwind.config.js
- 使用 @theme 指令进行自定义
- 已更新 PostCSS 配置
- 如果使用选择器策略,已添加暗色模式变体
- 已将插件更新为兼容 v4 的版本
生产环境问题
- NODE_ENV=production
- 构建输出包含样式
- CSS 文件链接正确
- 不存在动态类生成问题
获取帮助
创建最小复现示例
# Create fresh project
npm create vite@latest repro -- --template react-ts
cd repro
npm install -D tailwindcss @tailwindcss/vite
# Add minimal code that shows the issue
# Share on GitHub Issues or Discord
资源
评论
支持 Markdown