编程

ia-tailwind-css

试用

Tailwind CSS v4 模式速查:CSS 优先配置、@theme 设计令牌、组件变体、v3 迁移修复。

它能做什么

Tailwind CSS v4 项目模式参考。涵盖 CSS 优先配置(@theme、@theme inline、@custom-variant、@utility)、自动生成工具类的设计令牌,以及通过 `@theme inline` 把 `:root`/`.dark` 自定义属性映射成语义类,无需 `dark:` 前缀即可切换主题。包含编码规则(用 `gap` 代替 `space-*`、`min-h-dvh` 代替 `min-h-screen`、禁止动态拼接类名)、`cn()` 合并工具类、`tailwind-variants`/CVA 类型安全组件变体、`eslint-plugin-better-tailwindcss` 静态检查规则,以及样式失效常见错误对照表。另附 v3 到 v4 迁移步骤与 codemod 指引,以及 CSS Modules 通过 `@reference` 接入主题令牌的方式。

什么时候用它

  • 为新 v4 项目配置 @theme 设计令牌与自定义工具类
  • 将现有 v3 代码库升级到 v4
  • 使用 tailwind-variants 或 CVA 构建类型安全的组件变体
  • 排查 Tailwind 样式、暗色模式或迁移构建失败问题

技能文档

Tailwind CSS v4

Verify before implementing: For v4-specific syntax (@theme, @variant, CSS-first config), look up current docs via Context7 (query-docs) before writing code. Tailwind v4 changed significantly from v3 and training data may be stale.

CSS-First Configuration

v4 eliminates tailwind.config.ts. All configuration lives in CSS.

DirectivePurpose
@import "tailwindcss"Entry point (replaces @tailwind base/components/utilities)
@theme { }Define/extend design tokens -- auto-generates utility classes
@theme inline { }Map CSS variables to Tailwind utilities without generating new vars
@theme static { }Define tokens that don't generate utilities
@utility name { }Create custom utilities (replaces @layer components + @apply)
@custom-variant name (selector)Define custom variants
@import "tailwindcss";

@theme {
  --color-brand: oklch(0.72 0.11 178);
  --font-display: "Inter", sans-serif;
  --animate-fade-in: fade-in 0.2s ease-out;
  @keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
}

@custom-variant dark (&:where(.dark, .dark *));

Tokens defined with @theme become utilities automatically: --color-brand produces bg-brand, text-brand, border-brand. Define z-index as tokens (--z-modal: 50) and reference via z-(--z-modal) instead of arbitrary z-50.

CSS Modules: when using .module.css with Tailwind v4, add @reference "#tailwind"; at the top of the module file to enable theme token access inside the module.

Animations (tw-animate-css): use animate-in/animate-out base classes combined with effect classes (fade-in, slide-in-from-top). Decimal spacing gotcha: use bracket notation [0.625rem] instead of fractional values like 2.5.

v3 to v4 Migration

For projects upgrading from v3 to v4, see v3-to-v4-migration.md for the full breaking-change table and codemod guidance. For greenfield v4 work, current patterns are above.

Coding Rules

  • gap over space-x/space-y -- gap handles wrapping; space-* breaks on wrap
  • size-* over w-* h-* -- for equal dimensions
  • min-h-dvh over min-h-screen -- dvh accounts for mobile browser chrome
  • Opacity modifier (bg-black/50) -- *-opacity-* utilities are removed in v4
  • Design tokens over arbitrary values -- check @theme before using [#hex]
  • Never construct classes dynamically -- text-${color}-500 won't be detected; use complete class names
  • @utility over @apply with @layer -- @apply on @layer classes fails in v4
  • Parent padding over last-child margin -- use padding on containers instead of bottom margins on the last child

ESLint Integration

Use eslint-plugin-better-tailwindcss for automated class validation:

  • no-conflicting-classes -- catches text-red-500 text-blue-500
  • no-unknown-classes -- flags typos
  • enforce-canonical-classes -- normalizes shorthand
  • no-duplicate-classes -- removes redundant entries
  • no-deprecated-classes -- catches v3 classes removed in v4
  • useSortedClasses -- enforces canonical class order; configure attributes: ["classList"] and functions: ["clsx", "cva", "cn", "tv", "tw"] to cover JSX utility functions

Class Merging

Use cn() combining clsx + tailwind-merge for conditional/dynamic classes. Use plain strings for static className attributes.

import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); }
// Static: plain string


// Conditional: use cn()

Component Variants

Use tailwind-variants (tv()) for type-safe variant components. Alternative: class-variance-authority (cva()).

import { tv } from "tailwind-variants";
const button = tv({
  base: "rounded-lg px-4 py-2 font-medium transition-colors",
  variants: {
    color: { primary: "bg-blue-600 text-white", secondary: "bg-gray-200 text-gray-800" },
    size: { sm: "text-sm px-3 py-1", md: "text-base", lg: "text-lg px-6 py-3" },
  },
  defaultVariants: { color: "primary", size: "md" },
});

See tailwind-variants patterns for slots, composition, and responsive variants.

Common Errors

SymptomFix
bg-primary doesn't workAdd @theme inline { --color-primary: var(--primary); }
Colors all black/whiteDouble hsl() wrapping -- use var(--color) not hsl(var(--color))
@apply fails on custom classUse @utility instead of @layer components
Build fails after migrationDelete tailwind.config.ts
Animations brokenReplace tailwindcss-animate with tw-animate-css
.dark { @theme { } } failsv4 does not support nested @theme -- use :root/.dark CSS vars mapped via @theme inline

Dark Mode (v4 Pattern)

:root { --background: hsl(0 0% 100%); --foreground: hsl(222 84% 4.9%); }
.dark { --background: hsl(222 84% 4.9%); --foreground: hsl(210 40% 98%); }
@theme inline { --color-background: var(--background); --color-foreground: var(--foreground); }

Semantic classes (bg-background, text-foreground) auto-switch -- no dark: variants needed for themed colors.

Verify

  • Build passes with zero errors (npm run build or equivalent)
  • No v3 class names remain in changed files (check with @tailwindcss/upgrade --dry-run if available)
  • No conflicting classes on the same element

References

  • Component patterns -- tailwind-variants slots, CVA, compound components
  • Layout patterns -- grid areas, container queries, z-index management, fluid typography

常见问题

@theme 和 @theme inline 有什么区别?
@theme 用于定义或扩展设计令牌,并自动生成对应工具类(例如 --color-brand 会产出 bg-brand、text-brand)。@theme inline 把已有 CSS 变量映射为 Tailwind 工具类而不创建新变量,常用于把 :root/.dark 中的自定义属性接到 bg-background 等语义类。
可以动态拼接类名(如 text-${color}-500)吗?
不可以。文档明确指出动态拼接的类名不会被 Tailwind 编译器识别,对应工具类不会被打包输出,必须使用完整静态类名。
CSS Modules 怎么配合 Tailwind v4?
在 .module.css 文件顶部添加 @reference "#tailwind";,即可在模块内访问主题令牌。文档同时提醒 v3 迁移时常出现 hsl() 重复包裹的写法错误,需改为 var(--color) 形式。

相关技能

把自然语言描述转为结构化 JSON,并由 mcp-diagram-generator MCP 服务生成 Draw.io、Mermaid 或 Excalidraw 图表文件。

作者 nssa.io1.0k 次安装47 星标

在本地磁盘以分类纯 Markdown 文件保存需要长期留存的事实,与智能体内置记忆并存。

作者 Iván559 次安装18 星标

诊断生产力系统反复失效的根因,给出最小干预——容量测算、瓶颈定位、可靠的本地记录。

作者 Iván855 次安装69 星标

以 AI 机器人身份加入视频会议,提供语音、虚拟形象与屏幕共享四种模式。

作者 johnpatternai22 次安装8 星标

从 AdMapix API 拉取广告创意、应用、榜单和收入预估等数据,原样返回结构化 JSON。

作者 fly0pants4.3k 次安装296 星标

iliaal 的更多技能

浏览全部技能

Laravel 与 PHP 8.4 框架级开发指南:架构、Eloquent、迁移、队列、测试,基于真实踩坑编写。

作者 iliaal40 次安装

先核对需求规范再评代码质量,输出按严重度排序的发现,并可切换多 agent 深度评审。

作者 iliaal37 次安装

围绕可复现失败信号展开的 7 步根因调试流程,先有失败用例再下修复。

作者 iliaal36 次安装

在动手写代码之前,先拿到一份通过评审的设计文档。

作者 iliaal33 次安装

编写能验证真实行为的测试:每个用例聚焦单一行为,优先使用真实对象,覆盖静默失败路径。

作者 iliaal33 次安装

为软件开发产出结构化实施计划,提供持久化 .plan/ 状态、按规模分阶段与明确的决策规则。

作者 iliaal30 次安装