tailwind-css by paulrberg/agent-skills
npx skills add https://github.com/paulrberg/agent-skills --skill tailwind-cssTailwind CSS v4 的专家指导,涵盖 CSS 优先配置、现代工具类模式以及使用 tailwind-variants 进行类型安全的组件样式设计。
Tailwind CSS v4 移除了 tailwind.config.ts,转而采用纯 CSS 配置。所有配置都通过特殊指令存在于 CSS 文件中。
核心指令:
@import "tailwindcss" - 加载 Tailwind 的入口点@theme { } - 定义或扩展设计令牌@theme static { } - 定义不应生成工具类的令牌@utility - 创建自定义工具类@custom-variant - 定义自定义变体最小示例:
@import "tailwindcss";
@theme {
--color-brand: oklch(0.72 0.11 178);
--font-display: "Inter", sans-serif;
--spacing-edge: 1.5rem;
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
所有使用 @theme 定义的主题令牌都会自动作为工具类可用。例如,--color-brand 可以作为 bg-brand、text-brand、border-brand 等使用。
使用 eslint-plugin-better-tailwindcss 进行 Tailwind CSS v4 类验证和样式强制执行。
正确性规则(错误):
no-conflicting-classes - 检测相互覆盖的类no-unknown-classes - 标记未在 Tailwind 中注册的类风格规则(警告):
enforce-canonical-classes - 使用标准的 v4 类名enforce-shorthand-classes - 使用缩写类版本no-deprecated-classes - 移除过时的类名no-duplicate-classes - 消除冗余声明no-unnecessary-whitespace - 清理多余的空格示例:
// ❌ 错误:分开的边距
<div className="px-6 py-6">
// ✅ 正确:简写
<div className="p-6">
// ❌ 错误:分开的宽度/高度
<div className="w-6 h-6">
// ✅ 正确:尺寸工具类
<div className="size-6">
修改 Tailwind 类后,运行项目的 ESLint 检查,以验证整个代码库中的所有更改。
使用gap进行 flex/grid 间距,而非 space-x/space-y:
gap 工具类能正确处理换行,而 space-* 工具类在 flex/grid 项目换行到多行时会失效。
// ✅ 正确:gap 处理换行
<div className="flex gap-4">
// ❌ 错误:项目换行时失效
<div className="flex space-x-4">
对于相等尺寸,优先使用size-*而非分开的 w-*/h-*:
// ✅ 正确:简洁
<div className="size-16">
// ❌ 错误:冗余
<div className="w-16 h-16">
始终使用min-h-dvh而非 min-h-screen:
动态视口高度(dvh)考虑了移动浏览器 UI 控件,而 vh 单位忽略了它,导致在移动端 Safari 上出现布局问题。
// ✅ 正确:在移动端 Safari 上有效
<main className="min-h-dvh">
// ❌ 错误:在移动端 Safari 上有问题
<main className="min-h-screen">
优先使用上/左边距而非下/右边距:
一致的方向性提高了布局的可预测性。
// ✅ 正确:上边距
<div className="mt-4">
// ❌ 避免:下边距(除非需要)
<div className="mb-4">
在父容器上使用内边距,而非在最后一个子元素上使用下边距:
内边距提供一致的间距,无需 :last-child 选择器。
// ✅ 正确:父容器上的内边距
<section className="pb-8">
<div>项目 1</div>
<div>项目 2</div>
<div>项目 3</div>
</section>
// ❌ 避免:子元素上的边距
<section>
<div className="mb-4">项目 1</div>
<div className="mb-4">项目 2</div>
<div>项目 3</div>
</section>
对于最大宽度,优先使用容器比例而非像素值:
// ✅ 正确:语义化的容器尺寸
<div className="max-w-2xl">
// ❌ 避免:任意的像素值
<div className="max-w-[672px]">
避免使用leading-*类;使用行高修饰符:
Tailwind v4 支持使用 text-{size}/{leading} 语法进行内联行高修饰。
// ✅ 正确:组合的尺寸和行高
<p className="text-base/7">
// ❌ 错误:分开的工具类
<p className="text-base leading-7">
字体大小参考:
| 类名 | 大小 |
|---|---|
text-xs | 12px |
text-sm | 14px |
text-base | 16px |
text-lg | 18px |
text-xl | 20px |
使用不透明度修饰符语法,而非单独的不透明度工具类:
所有 *-opacity-* 工具类在 Tailwind v4 中已被移除。请改用修饰符语法。
// ✅ 正确:不透明度修饰符
<div className="bg-red-500/60">
// ❌ 错误:在 v4 中已移除
<div className="bg-red-500 bg-opacity-60">
优先使用设计令牌而非任意的十六进制值:
在使用任意颜色值之前,请先检查项目的 @theme 配置。
// ✅ 正确:主题令牌
<div className="bg-brand">
// ❌ 避免:任意的十六进制值(先检查主题)
<div className="bg-[#4f46e5]">
Tailwind v4 重命名了边框半径工具类:
| v3 | v4(等效) | 大小 |
|---|---|---|
rounded-sm | rounded-xs | 2px |
rounded | rounded-sm | 4px |
rounded-md | rounded | 6px |
rounded-lg | rounded-md | 8px |
编写新代码时请使用 v4 的名称。
Tailwind v4 重命名了渐变工具类并添加了新的渐变类型。
使用bg-linear-*,而非 bg-gradient-*:
// ✅ 正确:v4 语法
<div className="bg-linear-to-r from-blue-500 to-purple-500">
// ❌ 错误:在 v4 中已移除
<div className="bg-gradient-to-r from-blue-500 to-purple-500">
新的渐变类型:
bg-radial - 径向渐变bg-conic - 锥形渐变示例:
<div className="bg-radial from-blue-500 to-purple-500">
<div className="bg-conic from-red-500 via-yellow-500 to-green-500">
始终优先使用 Tailwind 的预定义比例:
在使用任意值之前,请先检查项目的 @theme 配置以获取可用的令牌。
// ✅ 正确:预定义比例
<div className="ml-4">
// ❌ 避免:任意的像素值
<div className="ml-[16px]">
通用规则: 优先使用尺寸比例而非像素值。三行相似的代码优于过早的抽象。
常见的模式是结合 clsx 和 tailwind-merge 的 cn 工具函数。
使用cn 用于:
const CARD_BASE = cn("fixed classes")cn("base", condition && "conditional")cn(baseClasses, className)cn("p-4", "p-6") → "p-6"不要使用cn 用于:
className 属性中的静态字符串:className="fixed classes"示例:
// ✅ 正确:className 中的静态字符串
<button className="rounded-lg px-4 py-2 font-medium bg-blue-600">
// ✅ 正确:使用 cn 的静态常量
const CARD_BASE = cn("rounded-lg border border-gray-300 p-4");
<div className={CARD_BASE} />
// ✅ 正确:使用 cn 的条件类
<button className={cn(
"rounded-lg px-4 py-2 font-medium",
isActive ? "bg-blue-600" : "bg-gray-700",
disabled && "opacity-50"
)} />
// ❌ 错误:在静态 className 属性中不必要地使用 cn
<button className={cn("rounded-lg px-4 py-2 font-medium")} />
对于 Image 组件,使用 Tailwind 尺寸类而非像素值。
// ✅ 正确:Tailwind 单位
<Image src={src} alt={alt} className="size-16" />
<Image src={src} alt={alt} className="w-24 h-auto" />
// ❌ 错误:像素值
<Image src={src} alt={alt} width={64} height={64} />
将 z-index 值定义为 @theme 中的 CSS 自定义属性,然后使用 z-(--z-*) 语法引用它们。
切勿使用任意的 z-index 数字:
// ✅ 正确:主题 z-index 值
<div className="z-(--z-modal)">
<div className="z-(--z-sticky)">
// ❌ 错误:任意的 z-index 数字
<div className="z-[100]">
<div className="z-[9999]">
在 CSS 中定义 z-index 令牌:
@theme {
--z-base: 0;
--z-sticky: 10;
--z-modal: 100;
--z-tooltip: 1000;
}
使用简单的 dark: 变体来处理深色模式样式。
模式:
先编写浅色模式样式,然后添加深色模式覆盖。
// ✅ 正确:先浅色模式,后深色覆盖
<div className="bg-white text-gray-900 dark:bg-gray-900 dark:text-white">
// ❌ 避免:先深色模式(可读性较差)
<div className="dark:bg-gray-900 dark:text-white bg-white text-gray-900">
仅当无法用 Tailwind 类轻松编写复杂 CSS 时,才将 CSS 模块作为最后的手段。
所有 .module.css 文件必须在顶部包含 @reference "#tailwind";,以在模块内启用 Tailwind 工具类和主题令牌。
示例:
/* component.module.css */
@reference "#tailwind";
.component {
/* 无法用 Tailwind 工具类表达的复杂 CSS */
/* 仍然可以使用 Tailwind 工具类和主题令牌 */
}
references/tailwind-variants.md 了解模式@theme 配置以获取可用的令牌tailwind-variants 中的 tv() 进行类型安全的变体设计示例:
import { tv } from "tailwind-variants";
const button = tv({
base: "rounded-lg px-4 py-2 font-medium",
variants: {
color: {
primary: "bg-blue-600 text-white",
secondary: "bg-gray-600 text-white",
},
size: {
sm: "text-sm",
md: "text-base",
lg: "text-lg",
},
},
});
references/tailwind-v4-rules.md 了解破坏性变更bg-linear-*,而非 bg-gradient-*)bg-my-color,而非 bg-[--var-my-color])@theme 配置中@theme 配置以查看可用的颜色/20、/50 等)示例:
// ✅ 正确:带不透明度的主题令牌
<div className="bg-brand/20 text-brand">
// ❌ 避免:任意的十六进制值
<div className="bg-[#4f46e5]/20 text-[#4f46e5]">
references/tw-animate-css.md 了解可用的动画animate-in 或 animate-out)与效果类组合使用[0.625rem] 语法,而非 2.5示例:
// 进入:淡入 + 从底部滑入
<div className="fade-in slide-in-from-bottom-4 duration-300 animate-in">
// 退出:淡出 + 向底部滑出
<div className="fade-out slide-out-to-bottom-4 duration-200 animate-out">
| 方面 | 模式 |
|---|---|
| 配置 | 纯 CSS:@theme、@utility、@custom-variant |
| 渐变 | bg-linear-*、bg-radial、bg-conic |
| 不透明度 | 修饰符语法:bg-black/50 |
| 行高 | 修饰符语法:text-base/7 |
| 字体特性 | font-features-zero、font-features-ss01 等 |
| CSS 变量 | bg-my-color(从 @theme 自动创建) |
| CSS 模块 | 顶部 @reference "#tailwind"; |
| 类合并 | cn() 用于条件类;静态类用纯字符串 |
| 视口 | min-h-dvh(非 min-h-screen) |
| 组件变体 | references/tailwind-variants.md |
| 动画 | references/tw-animate-css.md |
| V4 规则 | references/tailwind-v4-rules.md |
references/tailwind-v4-rules.md — 破坏性变更、已移除/重命名的工具类、布局规则、排版、渐变、CSS 变量、新的 v4 功能、常见陷阱references/tailwind-variants.md — 组件变体、插槽 API、组合、TypeScript 集成、响应式变体references/tw-animate-css.md — 进入/退出动画、滑动/淡入淡出/缩放工具类、间距问题每周安装量
90
代码仓库
GitHub 星标数
41
首次出现
2026年2月14日
安全审计
已安装于
claude-code83
kimi-cli81
gemini-cli81
github-copilot81
amp81
codex81
Expert guidance for Tailwind CSS v4, CSS-first configuration, modern utility patterns, and type-safe component styling with tailwind-variants.
Tailwind CSS v4 eliminates tailwind.config.ts in favor of CSS-only configuration. All configuration lives in CSS files using special directives.
Core Directives:
@import "tailwindcss" - Entry point that loads Tailwind@theme { } - Define or extend design tokens@theme static { } - Define tokens that should not generate utilities@utility - Create custom utilities@custom-variant - Define custom variantsMinimal Example:
@import "tailwindcss";
@theme {
--color-brand: oklch(0.72 0.11 178);
--font-display: "Inter", sans-serif;
--spacing-edge: 1.5rem;
}
All theme tokens defined with @theme automatically become available as utility classes. For example, --color-brand can be used as bg-brand, text-brand, border-brand, etc.
Use eslint-plugin-better-tailwindcss for Tailwind CSS v4 class validation and style enforcement.
Correctness Rules (errors):
no-conflicting-classes - Detect classes that override each otherno-unknown-classes - Flag classes not registered with TailwindStylistic Rules (warnings):
enforce-canonical-classes - Use standard v4 class namesenforce-shorthand-classes - Use abbreviated class versionsno-deprecated-classes - Remove outdated class namesno-duplicate-classes - Eliminate redundant declarationsno-unnecessary-whitespace - Clean up extra spacingExamples:
// ❌ Bad: separate padding
<div className="px-6 py-6">
// ✅ Good: shorthand
<div className="p-6">
// ❌ Bad: separate width/height
<div className="w-6 h-6">
// ✅ Good: size utility
<div className="size-6">
Run the project's ESLint check after modifying Tailwind classes to validate all changes across the codebase.
Usegap for flex/grid spacing, not space-x/space-y:
The gap utilities handle wrapping correctly, while space-* utilities break when flex/grid items wrap to multiple lines.
// ✅ Good: gap handles wrapping
<div className="flex gap-4">
// ❌ Bad: breaks when items wrap
<div className="flex space-x-4">
Prefersize-* over separate w-*/h-* for equal dimensions:
// ✅ Good: concise
<div className="size-16">
// ❌ Bad: redundant
<div className="w-16 h-16">
Always usemin-h-dvh instead of min-h-screen:
Dynamic viewport height (dvh) accounts for mobile browser chrome, while vh units ignore it and cause layout issues on mobile Safari.
// ✅ Good: works on mobile Safari
<main className="min-h-dvh">
// ❌ Bad: buggy on mobile Safari
<main className="min-h-screen">
Prefer top/left margins over bottom/right:
Consistent directionality improves layout predictability.
// ✅ Good: top margin
<div className="mt-4">
// ❌ Avoid: bottom margin (unless needed)
<div className="mb-4">
Use padding on parent containers instead of bottom margins on last child:
Padding provides consistent spacing without needing :last-child selectors.
// ✅ Good: padding on parent
<section className="pb-8">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</section>
// ❌ Avoid: margin on children
<section>
<div className="mb-4">Item 1</div>
<div className="mb-4">Item 2</div>
<div>Item 3</div>
</section>
For max-widths, prefer container scale over pixel values:
// ✅ Good: semantic container size
<div className="max-w-2xl">
// ❌ Avoid: arbitrary pixel value
<div className="max-w-[672px]">
Avoidleading-* classes; use line height modifiers:
Tailwind v4 supports inline line height modifiers with the text-{size}/{leading} syntax.
// ✅ Good: combined size and line height
<p className="text-base/7">
// ❌ Bad: separate utilities
<p className="text-base leading-7">
Font Size Reference:
| Class | Size |
|---|---|
text-xs | 12px |
text-sm | 14px |
text-base | 16px |
text-lg | 18px |
text-xl | 20px |
Use opacity modifier syntax, not separate opacity utilities:
All *-opacity-* utilities were removed in Tailwind v4. Use the modifier syntax instead.
// ✅ Good: opacity modifier
<div className="bg-red-500/60">
// ❌ Bad: removed in v4
<div className="bg-red-500 bg-opacity-60">
Prefer design tokens over arbitrary hex values:
Check the project's @theme configuration before using arbitrary color values.
// ✅ Good: theme token
<div className="bg-brand">
// ❌ Avoid: arbitrary hex (check theme first)
<div className="bg-[#4f46e5]">
Tailwind v4 renamed border radius utilities:
| v3 | v4 (equivalent) | Size |
|---|---|---|
rounded-sm | rounded-xs | 2px |
rounded | rounded-sm | 4px |
rounded-md | rounded | 6px |
rounded-lg |
Use the v4 names when writing new code.
Tailwind v4 renamed gradient utilities and added new gradient types.
Usebg-linear-*, not bg-gradient-*:
// ✅ Good: v4 syntax
<div className="bg-linear-to-r from-blue-500 to-purple-500">
// ❌ Bad: removed in v4
<div className="bg-gradient-to-r from-blue-500 to-purple-500">
New gradient types:
bg-radial - Radial gradientsbg-conic - Conic gradientsExample:
<div className="bg-radial from-blue-500 to-purple-500">
<div className="bg-conic from-red-500 via-yellow-500 to-green-500">
Always prefer Tailwind's predefined scale:
Check the project's @theme configuration for available tokens before using arbitrary values.
// ✅ Good: predefined scale
<div className="ml-4">
// ❌ Avoid: arbitrary pixel value
<div className="ml-[16px]">
General rule: Prefer sizing scale over pixel values. Three similar lines of code is better than a premature abstraction.
The common pattern is a cn utility combining clsx + tailwind-merge.
Usecn for:
const CARD_BASE = cn("fixed classes")cn("base", condition && "conditional")cn(baseClasses, className)cn("p-4", "p-6") → "p-6"Do NOT usecn for:
className attributes: className="fixed classes"Examples:
// ✅ Good: static string in className
<button className="rounded-lg px-4 py-2 font-medium bg-blue-600">
// ✅ Good: static constant with cn
const CARD_BASE = cn("rounded-lg border border-gray-300 p-4");
<div className={CARD_BASE} />
// ✅ Good: conditional with cn
<button className={cn(
"rounded-lg px-4 py-2 font-medium",
isActive ? "bg-blue-600" : "bg-gray-700",
disabled && "opacity-50"
)} />
// ❌ Bad: unnecessary cn for static className attribute
<button className={cn("rounded-lg px-4 py-2 font-medium")} />
Use Tailwind size classes instead of pixel values for Image components.
// ✅ Good: Tailwind units
<Image src={src} alt={alt} className="size-16" />
<Image src={src} alt={alt} className="w-24 h-auto" />
// ❌ Bad: pixel values
<Image src={src} alt={alt} width={64} height={64} />
Define z-index values as CSS custom properties in @theme, then reference them with the z-(--z-*) syntax.
Never use arbitrary z-index numbers:
// ✅ Good: theme z-index value
<div className="z-(--z-modal)">
<div className="z-(--z-sticky)">
// ❌ Bad: arbitrary z-index numbers
<div className="z-[100]">
<div className="z-[9999]">
Define z-index tokens in CSS:
@theme {
--z-base: 0;
--z-sticky: 10;
--z-modal: 100;
--z-tooltip: 1000;
}
Use the plain dark: variant for dark mode styles.
Pattern:
Write light mode styles first, then add dark mode overrides.
// ✅ Good: light mode first, then dark override
<div className="bg-white text-gray-900 dark:bg-gray-900 dark:text-white">
// ❌ Avoid: dark mode first (less readable)
<div className="dark:bg-gray-900 dark:text-white bg-white text-gray-900">
Use CSS Modules only as a last resort for complex CSS that cannot be easily written with Tailwind classes.
All .module.css files must include @reference "#tailwind"; at the top to enable Tailwind utilities and theme tokens inside the module.
Example:
/* component.module.css */
@reference "#tailwind";
.component {
/* Complex CSS that can't be expressed with Tailwind utilities */
/* Can still use Tailwind utilities and theme tokens */
}
references/tailwind-variants.md for patterns@theme configuration for available tokenstv() from tailwind-variants for type-safe variantsExample:
import { tv } from "tailwind-variants";
const button = tv({
base: "rounded-lg px-4 py-2 font-medium",
variants: {
color: {
primary: "bg-blue-600 text-white",
secondary: "bg-gray-600 text-white",
},
size: {
sm: "text-sm",
md: "text-base",
lg: "text-lg",
},
},
});
references/tailwind-v4-rules.md for breaking changesbg-linear-*, not bg-gradient-*)bg-my-color, not bg-[--var-my-color])@theme configuration@theme configuration first to see available colors/20, /50, etc.)Example:
// ✅ Good: theme token with opacity
<div className="bg-brand/20 text-brand">
// ❌ Avoid: arbitrary hex
<div className="bg-[#4f46e5]/20 text-[#4f46e5]">
references/tw-animate-css.md for available animationsanimate-in or animate-out) with effect classes[0.625rem] syntax, not 2.5Example:
// Enter: fade + slide up
<div className="fade-in slide-in-from-bottom-4 duration-300 animate-in">
// Exit: fade + slide down
<div className="fade-out slide-out-to-bottom-4 duration-200 animate-out">
| Aspect | Pattern |
|---|---|
| Configuration | CSS-only: @theme, @utility, @custom-variant |
| Gradients | bg-linear-*, bg-radial, bg-conic |
| Opacity | Modifier syntax: bg-black/50 |
| Line Height | Modifier syntax: text-base/7 |
references/tailwind-v4-rules.md — Breaking changes, removed/renamed utilities, layout rules, typography, gradients, CSS variables, new v4 features, common pitfallsreferences/tailwind-variants.md — Component variants, slots API, composition, TypeScript integration, responsive variantsreferences/tw-animate-css.md — Enter/exit animations, slide/fade/zoom utilities, spacing gotchasWeekly Installs
90
Repository
GitHub Stars
41
First Seen
Feb 14, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code83
kimi-cli81
gemini-cli81
github-copilot81
amp81
codex81
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
118,000 周安装
rounded-md |
| 8px |
| Font Features | font-features-zero, font-features-ss01, etc. |
| CSS Variables | bg-my-color (auto-created from @theme) |
| CSS Modules | @reference "#tailwind"; at top |
| Class Merging | cn() for conditionals; plain string for static |
| Viewport | min-h-dvh (not min-h-screen) |
| Component Variants | references/tailwind-variants.md |
| Animations | references/tw-animate-css.md |
| V4 Rules | references/tailwind-v4-rules.md |