tailwind-v4-shadcn

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Tailwind v4 + shadcn/ui Production Stack

Tailwind v4 + shadcn/ui 生产级技术栈

Production-tested: WordPress Auditor (https://wordpress-auditor.webfonts.workers.dev) Last Updated: 2025-11-09 Status: Production Ready ✅

经生产验证: WordPress Auditor (https://wordpress-auditor.webfonts.workers.dev) 最后更新: 2025-11-09 状态: 可用于生产 ✅

⚠️ BEFORE YOU START (READ THIS!)

⚠️ 开始前必读(务必阅读!)

CRITICAL FOR AI AGENTS: If you're Claude Code helping a user set up Tailwind v4:
  1. Explicitly state you're using this skill at the start of the conversation
  2. Reference patterns from the skill rather than general knowledge
  3. Prevent known issues listed in
    reference/common-gotchas.md
  4. Don't guess - if unsure, check the skill documentation
USER ACTION REQUIRED: Tell Claude to check this skill first!
Say: "I'm setting up Tailwind v4 + shadcn/ui - check the tailwind-v4-shadcn skill first"
对AI Agent的重要提示: 如果你是帮用户搭建Tailwind v4的Claude Code:
  1. 对话开头明确说明你正在使用本技能
  2. 引用本技能中的模式而非通用知识
  3. 避免
    reference/common-gotchas.md
    中列出的已知问题
  4. 不要猜测 - 如有疑问,请查阅本技能文档
用户需执行的操作: 告知Claude优先检查本技能!
请说:"我正在搭建Tailwind v4 + shadcn/ui - 优先检查tailwind-v4-shadcn技能"

Why This Matters (Real-World Results)

为什么这很重要(实际效果对比)

Without skill activation:
  • ❌ Setup time: ~5 minutes
  • ❌ Errors encountered: 2-3 (tw-animate-css, duplicate @layer base)
  • ❌ Manual fixes needed: 2+ commits
  • ❌ Token usage: ~65k
  • ❌ User confidence: Required debugging
With skill activation:
  • ✅ Setup time: ~1 minute
  • ✅ Errors encountered: 0
  • ✅ Manual fixes needed: 0
  • ✅ Token usage: ~20k (70% reduction)
  • ✅ User confidence: Instant success
未启用技能时:
  • ❌ 搭建耗时: ~5分钟
  • ❌ 遇到错误: 2-3个(tw-animate-css、重复的@layer base)
  • ❌ 所需手动修复: 2次以上提交
  • ❌ Token消耗: ~65k
  • ❌ 用户信心: 需要调试
启用技能后:
  • ✅ 搭建耗时: ~1分钟
  • ✅ 遇到错误: 0
  • ✅ 所需手动修复: 0
  • ✅ Token消耗: ~20k(减少70%)
  • ✅ 用户信心: 一次性成功

Known Issues This Skill Prevents

本技能可避免的已知问题

  1. tw-animate-css import error (deprecated in v4)
  2. Duplicate @layer base blocks (shadcn init adds its own)
  3. Wrong template selection (vanilla TS vs React)
  4. Missing post-init cleanup (incompatible CSS rules)
  5. Wrong plugin syntax (using @import or require() instead of @plugin directive)
All of these are handled automatically when the skill is active.

  1. tw-animate-css导入错误(v4中已废弃旧版本)
  2. 重复的@layer base块(shadcn init会自动添加)
  3. 模板选择错误(原生TS和React混淆)
  4. 初始化后缺少清理步骤(不兼容的CSS规则)
  5. 插件语法错误(使用@import或require()而非@plugin指令)
技能启用后所有这些问题都会自动处理。

Quick Start (5 Minutes - Follow This Exact Order)

快速开始(5分钟 - 严格按顺序执行)

1. Install Dependencies

1. 安装依赖

bash
pnpm add tailwindcss @tailwindcss/vite
pnpm add -D @types/node
pnpm dlx shadcn@latest init
bash
pnpm add tailwindcss @tailwindcss/vite
pnpm add -D @types/node
pnpm dlx shadcn@latest init

2. Configure Vite

2. 配置Vite

typescript
// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import path from 'path'

export default defineConfig({
  plugins: [react(), tailwindcss()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  }
})
typescript
// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import path from 'path'

export default defineConfig({
  plugins: [react(), tailwindcss()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  }
})

3. Update components.json

3. 更新components.json

json
{
  "tailwind": {
    "config": "",              // ← CRITICAL: Empty for v4
    "css": "src/index.css",
    "baseColor": "slate",      // Base color palette
    "cssVariables": true,
    "prefix": ""               // No prefix for utility classes
  }
}
json
{
  "tailwind": {
    "config": "",              // ← 关键:v4版本此处留空
    "css": "src/index.css",
    "baseColor": "slate",      // 基础调色板
    "cssVariables": true,
    "prefix": ""               // 工具类无前缀
  }
}

4. Delete tailwind.config.ts

4. 删除tailwind.config.ts

bash
rm tailwind.config.ts  # v4 doesn't use this file

bash
rm tailwind.config.ts  # v4不需要这个文件

The Four-Step Architecture (CRITICAL)

四步架构(核心要求)

This pattern is mandatory - skipping steps will break your theme.
该模式是强制要求 - 跳过步骤会导致主题失效。

Step 1: Define CSS Variables at Root Level

步骤1:在根级别定义CSS变量

css
/* src/index.css */
@import "tailwindcss";

:root {
  --background: hsl(0 0% 100%);      /* ← hsl() wrapper required */
  --foreground: hsl(222.2 84% 4.9%);
  --primary: hsl(221.2 83.2% 53.3%);
  /* ... all light mode colors */
}

.dark {
  --background: hsl(222.2 84% 4.9%);
  --foreground: hsl(210 40% 98%);
  --primary: hsl(217.2 91.2% 59.8%);
  /* ... all dark mode colors */
}
Critical Rules:
  • ✅ Define at root level (NOT inside
    @layer base
    )
  • ✅ Use
    hsl()
    wrapper on all color values
  • ✅ Use
    .dark
    for dark mode (NOT
    .dark { @theme { } }
    )
css
/* src/index.css */
@import "tailwindcss";

:root {
  --background: hsl(0 0% 100%);      /* ← 必须用hsl()包裹 */
  --foreground: hsl(222.2 84% 4.9%);
  --primary: hsl(221.2 83.2% 53.3%);
  /* ... 所有亮色模式颜色 */
}

.dark {
  --background: hsl(222.2 84% 4.9%);
  --foreground: hsl(210 40% 98%);
  --primary: hsl(217.2 91.2% 59.8%);
  /* ... 所有暗色模式颜色 */
}
核心规则:
  • ✅ 在根级别定义(不要放在
    @layer base
    内部)
  • ✅ 所有颜色值都用
    hsl()
    包裹
  • ✅ 暗色模式用
    .dark
    (不要用
    .dark { @theme { } }

Step 2: Map Variables to Tailwind Utilities

步骤2:将变量映射到Tailwind工具类

css
@theme inline {
  --color-background: var(--background);
  --color-foreground: var(--foreground);
  --color-primary: var(--primary);
  /* ... map ALL CSS variables */
}
Why This Is Required:
  • Generates utility classes (
    bg-background
    ,
    text-primary
    )
  • Without this,
    bg-primary
    etc. won't exist
css
@theme inline {
  --color-background: var(--background);
  --color-foreground: var(--foreground);
  --color-primary: var(--primary);
  /* ... 映射所有CSS变量 */
}
为什么需要这一步:
  • 生成工具类(
    bg-background
    text-primary
  • 没有这一步,
    bg-primary
    等类不会存在

Step 3: Apply Base Styles

步骤3:应用基础样式

css
@layer base {
  body {
    background-color: var(--background);  /* NO hsl() here */
    color: var(--foreground);
  }
}
Critical Rules:
  • ✅ Reference variables directly:
    var(--background)
  • ❌ Never double-wrap:
    hsl(var(--background))
css
@layer base {
  body {
    background-color: var(--background);  /* 此处不要加hsl() */
    color: var(--foreground);
  }
}
核心规则:
  • ✅ 直接引用变量:
    var(--background)
  • ❌ 不要双层包裹:
    hsl(var(--background))

Step 4: Result - Automatic Dark Mode

步骤4:效果 - 自动暗黑模式

tsx
<div className="bg-background text-foreground">
  {/* No dark: variants needed - theme switches automatically */}
</div>

tsx
<div className="bg-background text-foreground">
  {/* 不需要dark:前缀 - 主题会自动切换 */}
</div>

Dark Mode Setup

暗黑模式设置

1. Create ThemeProvider

1. 创建ThemeProvider

See
reference/dark-mode.md
for full implementation or use template:
typescript
// Copy from: templates/theme-provider.tsx
完整实现请查看
reference/dark-mode.md
或使用模板:
typescript
// 复制自: templates/theme-provider.tsx

2. Wrap Your App

2. 包裹你的应用

typescript
// src/main.tsx
import { ThemeProvider } from '@/components/theme-provider'

ReactDOM.createRoot(document.getElementById('root')!).render(
  <React.StrictMode>
    <ThemeProvider defaultTheme="dark" storageKey="vite-ui-theme">
      <App />
    </ThemeProvider>
  </React.StrictMode>,
)
typescript
// src/main.tsx
import { ThemeProvider } from '@/components/theme-provider'

ReactDOM.createRoot(document.getElementById('root')!).render(
  <React.StrictMode>
    <ThemeProvider defaultTheme="dark" storageKey="vite-ui-theme">
      <App />
    </ThemeProvider>
  </React.StrictMode>,
)

3. Add Theme Toggle

3. 添加主题切换按钮

bash
pnpm dlx shadcn@latest add dropdown-menu
See
reference/dark-mode.md
for ModeToggle component code.

bash
pnpm dlx shadcn@latest add dropdown-menu
ModeToggle组件代码请查看
reference/dark-mode.md

Critical Rules (MUST FOLLOW)

核心规则(必须遵守)

✅ Always Do:

✅ 必须执行:

  1. Wrap color values with
    hsl()
    in
    :root
    and
    .dark
    css
    --background: hsl(0 0% 100%);  /* ✅ Correct */
  2. Use
    @theme inline
    to map all CSS variables
    css
    @theme inline {
      --color-background: var(--background);
    }
  3. Set
    "tailwind.config": ""
    in components.json
    json
    { "tailwind": { "config": "" } }
  4. Delete
    tailwind.config.ts
    if it exists
  5. Use
    @tailwindcss/vite
    plugin (NOT PostCSS)
  6. Use
    cn()
    for conditional classes
    typescript
    import { cn } from "@/lib/utils"
    <div className={cn("base", isActive && "active")} />
  1. :root
    .dark
    中用
    hsl()
    包裹颜色值
    css
    --background: hsl(0 0% 100%);  /* ✅ 正确 */
  2. 使用
    @theme inline
    映射所有CSS变量
    css
    @theme inline {
      --color-background: var(--background);
    }
  3. 在components.json中设置
    "tailwind.config": ""
    json
    { "tailwind": { "config": "" } }
  4. 如果存在
    tailwind.config.ts
    请删除
  5. 使用
    @tailwindcss/vite
    插件(不要用PostCSS)
  6. 使用
    cn()
    处理条件类名
    typescript
    import { cn } from "@/lib/utils"
    <div className={cn("base", isActive && "active")} />

❌ Never Do:

❌ 禁止操作:

  1. Put
    :root
    or
    .dark
    inside
    @layer base
    css
    /* WRONG */
    @layer base {
      :root { --background: hsl(...); }
    }
  2. Use
    .dark { @theme { } }
    pattern
    css
    /* WRONG - v4 doesn't support nested @theme */
    .dark {
      @theme {
        --color-primary: hsl(...);
      }
    }
  3. Double-wrap colors
    css
    /* WRONG */
    body {
      background-color: hsl(var(--background));
    }
  4. Use
    tailwind.config.ts
    for theme colors
    typescript
    /* WRONG - v4 ignores this */
    export default {
      theme: {
        extend: {
          colors: { primary: 'hsl(var(--primary))' }
        }
      }
    }
  5. Use
    @apply
    directive (deprecated in v4)
  6. Use
    dark:
    variants for semantic colors
    tsx
    /* WRONG */
    <div className="bg-primary dark:bg-primary-dark" />
    
    /* CORRECT */
    <div className="bg-primary" />

  1. :root
    .dark
    放在
    @layer base
    内部
    css
    /* 错误 */
    @layer base {
      :root { --background: hsl(...); }
    }
  2. 使用
    .dark { @theme { } }
    模式
    css
    /* 错误 - v4不支持嵌套@theme */
    .dark {
      @theme {
        --color-primary: hsl(...);
      }
    }
  3. 双层包裹颜色
    css
    /* 错误 */
    body {
      background-color: hsl(var(--background));
    }
  4. 使用
    tailwind.config.ts
    定义主题颜色
    typescript
    /* 错误 - v4会忽略该配置 */
    export default {
      theme: {
        extend: {
          colors: { primary: 'hsl(var(--primary))' }
        }
      }
    }
  5. 使用
    @apply
    指令(v4中已废弃)
  6. 语义化颜色使用
    dark:
    前缀
    tsx
    /* 错误 */
    <div className="bg-primary dark:bg-primary-dark" />
    
    /* 正确 */
    <div className="bg-primary" />

Semantic Color Tokens

语义化颜色Token

Always use semantic names for colors:
css
:root {
  --destructive: hsl(0 84.2% 60.2%);        /* Red - errors, critical */
  --success: hsl(142.1 76.2% 36.3%);        /* Green - success states */
  --warning: hsl(38 92% 50%);               /* Yellow - warnings */
  --info: hsl(221.2 83.2% 53.3%);           /* Blue - info, primary */
}
Usage:
tsx
<div className="bg-destructive text-destructive-foreground">Critical</div>
<div className="bg-success text-success-foreground">Success</div>
<div className="bg-warning text-warning-foreground">Warning</div>
<div className="bg-info text-info-foreground">Info</div>

颜色始终使用语义化名称:
css
:root {
  --destructive: hsl(0 84.2% 60.2%);        /* 红色 - 错误、临界提示 */
  --success: hsl(142.1 76.2% 36.3%);        /* 绿色 - 成功状态 */
  --warning: hsl(38 92% 50%);               /* 黄色 - 警告 */
  --info: hsl(221.2 83.2% 53.3%);           /* 蓝色 - 提示、主色 */
}
用法:
tsx
<div className="bg-destructive text-destructive-foreground">临界提示</div>
<div className="bg-success text-success-foreground">成功</div>
<div className="bg-warning text-warning-foreground">警告</div>
<div className="bg-info text-info-foreground">提示</div>

Common Issues & Quick Fixes

常见问题与快速修复

SymptomCauseFix
bg-primary
doesn't work
Missing
@theme inline
mapping
Add
@theme inline
block
Colors all black/whiteDouble
hsl()
wrapping
Use
var(--color)
not
hsl(var(--color))
Dark mode not switchingMissing ThemeProviderWrap app in
<ThemeProvider>
Build fails
tailwind.config.ts
exists
Delete the file
Text invisibleWrong contrast colorsCheck color definitions in
:root
/
.dark
See
reference/common-gotchas.md
for complete troubleshooting guide.

症状原因修复方案
bg-primary
不生效
缺少
@theme inline
映射
添加
@theme inline
颜色全为黑/白色双层
hsl()
包裹
使用
var(--color)
而非
hsl(var(--color))
暗黑模式不切换缺少ThemeProvider
<ThemeProvider>
包裹应用
构建失败存在
tailwind.config.ts
删除该文件
文字不可见颜色对比度错误检查
:root
/
.dark
中的颜色定义
完整故障排查指南请查看
reference/common-gotchas.md

File Templates

文件模板

All templates are available in the
templates/
directory:
  • index.css - Complete CSS setup with all color variables
  • components.json - shadcn/ui v4 configuration
  • vite.config.ts - Vite + Tailwind plugin setup
  • tsconfig.app.json - TypeScript with path aliases
  • theme-provider.tsx - Dark mode provider with localStorage
  • utils.ts -
    cn()
    utility for class merging
Copy these files to your project and customize as needed.

所有模板都可以在
templates/
目录下获取:
  • index.css - 包含所有颜色变量的完整CSS配置
  • components.json - shadcn/ui v4配置
  • vite.config.ts - Vite + Tailwind插件配置
  • tsconfig.app.json - 配置了路径别名的TypeScript配置
  • theme-provider.tsx - 带localStorage的暗黑模式Provider
  • utils.ts - 用于类名合并的
    cn()
    工具函数
将这些文件复制到你的项目中按需自定义即可。

Complete Setup Checklist

完整搭建检查清单

  • Vite + React + TypeScript project created
  • @tailwindcss/vite
    installed (NOT postcss)
  • vite.config.ts
    uses
    tailwindcss()
    plugin
  • tsconfig.json
    has path aliases configured
  • components.json
    exists with
    "config": ""
  • NO
    tailwind.config.ts
    file exists
  • src/index.css
    follows v4 pattern:
    • :root
      and
      .dark
      at root level (not in @layer)
    • Colors wrapped with
      hsl()
    • @theme inline
      maps all variables
    • @layer base
      uses unwrapped variables
  • Theme provider installed and wrapping app
  • Dark mode toggle component created
  • Test theme switching works in browser

  • 已创建Vite + React + TypeScript项目
  • 已安装
    @tailwindcss/vite
    (不是postcss)
  • vite.config.ts
    已使用
    tailwindcss()
    插件
  • tsconfig.json
    已配置路径别名
  • 存在
    components.json
    "config": ""
  • 不存在
    tailwind.config.ts
    文件
  • src/index.css
    遵循v4模式:
    • :root
      .dark
      在根级别(不在@layer内部)
    • 颜色用
      hsl()
      包裹
    • @theme inline
      已映射所有变量
    • @layer base
      使用未包裹的变量
  • 已安装ThemeProvider并包裹应用
  • 已创建暗黑模式切换组件
  • 浏览器中测试主题切换正常

Advanced Topics

进阶主题

Custom Colors

自定义颜色

Add new semantic colors:
css
:root {
  --brand: hsl(280 65% 60%);
  --brand-foreground: hsl(0 0% 100%);
}

.dark {
  --brand: hsl(280 75% 70%);
  --brand-foreground: hsl(280 20% 10%);
}

@theme inline {
  --color-brand: var(--brand);
  --color-brand-foreground: var(--brand-foreground);
}
Usage:
<div className="bg-brand text-brand-foreground">Branded</div>
添加新的语义化颜色:
css
:root {
  --brand: hsl(280 65% 60%);
  --brand-foreground: hsl(0 0% 100%);
}

.dark {
  --brand: hsl(280 75% 70%);
  --brand-foreground: hsl(280 20% 10%);
}

@theme inline {
  --color-brand: var(--brand);
  --color-brand-foreground: var(--brand-foreground);
}
用法:
<div className="bg-brand text-brand-foreground">品牌色内容</div>

Migration from v3

从v3迁移

See
reference/migration-guide.md
for complete v3 → v4 migration steps.
完整的v3 → v4迁移步骤请查看
reference/migration-guide.md

Component Best Practices

组件最佳实践

  1. Always use semantic tokens
    tsx
    <Button variant="destructive">Delete</Button>  /* ✅ */
    <Button className="bg-red-600">Delete</Button>  /* ❌ */
  2. Use
    cn()
    for conditional styling
    tsx
    import { cn } from "@/lib/utils"
    
    <div className={cn(
      "base-class",
      isActive && "active-class",
      hasError && "error-class"
    )} />
  3. Compose shadcn/ui components
    tsx
    <Dialog>
      <DialogTrigger asChild>
        <Button>Open</Button>
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Title</DialogTitle>
        </DialogHeader>
      </DialogContent>
    </Dialog>

  1. 始终使用语义化token
    tsx
    <Button variant="destructive">删除</Button>  /* ✅ */
    <Button className="bg-red-600">删除</Button>  /* ❌ */
  2. 使用
    cn()
    处理条件样式
    tsx
    import { cn } from "@/lib/utils"
    
    <div className={cn(
      "base-class",
      isActive && "active-class",
      hasError && "error-class"
    )} />
  3. 组合使用shadcn/ui组件
    tsx
    <Dialog>
      <DialogTrigger asChild>
        <Button>打开</Button>
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>标题</DialogTitle>
        </DialogHeader>
      </DialogContent>
    </Dialog>

Dependencies

依赖

✅ Install These

✅ 请安装以下依赖

json
{
  "dependencies": {
    "tailwindcss": "^4.1.17",
    "@tailwindcss/vite": "^4.1.17",
    "clsx": "^2.1.1",
    "tailwind-merge": "^3.3.1",
    "@radix-ui/react-*": "latest",
    "lucide-react": "^0.553.0",
    "react": "^19.2.0",
    "react-dom": "^19.2.0"
  },
  "devDependencies": {
    "@types/node": "^24.10.0",
    "@vitejs/plugin-react": "^5.1.0",
    "vite": "^7.2.2",
    "typescript": "~5.9.0",
    "tw-animate-css": "^1.4.0"
  }
}
json
{
  "dependencies": {
    "tailwindcss": "^4.1.17",
    "@tailwindcss/vite": "^4.1.17",
    "clsx": "^2.1.1",
    "tailwind-merge": "^3.3.1",
    "@radix-ui/react-*": "latest",
    "lucide-react": "^0.553.0",
    "react": "^19.2.0",
    "react-dom": "^19.2.0"
  },
  "devDependencies": {
    "@types/node": "^24.10.0",
    "@vitejs/plugin-react": "^5.1.0",
    "vite": "^7.2.2",
    "typescript": "~5.9.0",
    "tw-animate-css": "^1.4.0"
  }
}

Animation Packages (Updated Nov 2025)

动画包(2025年11月更新)

shadcn/ui has deprecated
tailwindcss-animate
in favor of
tw-animate-css
for Tailwind v4 compatibility.
✅ DO Install (v4-compatible):
bash
pnpm add -D tw-animate-css
Then add to
src/index.css
:
css
@import "tailwindcss";
@import "tw-animate-css";
❌ DO NOT Install:
bash
npm install tailwindcss-animate  # Deprecated - v3 only
Why:
tw-animate-css
is the official v4-compatible replacement for animations, required by shadcn/ui components.

shadcn/ui已废弃
tailwindcss-animate
,改用
tw-animate-css
以兼容Tailwind v4。
✅ 请安装(兼容v4):
bash
pnpm add -D tw-animate-css
然后添加到
src/index.css
:
css
@import "tailwindcss";
@import "tw-animate-css";
❌ 不要安装:
bash
npm install tailwindcss-animate  # 已废弃 - 仅支持v3
原因:
tw-animate-css
是官方兼容v4的动画替换包,是shadcn/ui组件的必需依赖。

Tailwind v4 Plugins

Tailwind v4插件

Tailwind v4 supports official plugins using the
@plugin
directive in CSS.
Tailwind v4支持在CSS中使用
@plugin
指令引入官方插件。

Official Plugins (Tailwind Labs)

官方插件(Tailwind Labs出品)

Typography Plugin - Style Markdown/CMS Content

排版插件 - 为Markdown/CMS内容添加样式

When to use: Displaying blog posts, documentation, or any HTML from Markdown/CMS.
Installation:
bash
pnpm add -D @tailwindcss/typography
Configuration (v4 syntax):
css
/* src/index.css */
@import "tailwindcss";
@plugin "@tailwindcss/typography";
Usage:
html
<article class="prose lg:prose-xl dark:prose-invert">
  {{ markdown_content }}
</article>
Available classes:
  • prose
    - Base typography styles
  • prose-sm
    ,
    prose-base
    ,
    prose-lg
    ,
    prose-xl
    ,
    prose-2xl
    - Size variants
  • dark:prose-invert
    - Dark mode styles

适用场景: 展示博客文章、文档,或任何来自Markdown/CMS的HTML内容。
安装:
bash
pnpm add -D @tailwindcss/typography
配置(v4语法):
css
/* src/index.css */
@import "tailwindcss";
@plugin "@tailwindcss/typography";
用法:
html
<article class="prose lg:prose-xl dark:prose-invert">
  {{ markdown_content }}
</article>
可用类名:
  • prose
    - 基础排版样式
  • prose-sm
    prose-base
    prose-lg
    prose-xl
    prose-2xl
    - 尺寸变体
  • dark:prose-invert
    - 暗黑模式样式

Forms Plugin - Reset Form Element Styles

表单插件 - 重置表单元素样式

When to use: Building custom forms without shadcn/ui components, or need consistent cross-browser form styling.
Installation:
bash
pnpm add -D @tailwindcss/forms
Configuration (v4 syntax):
css
/* src/index.css */
@import "tailwindcss";
@plugin "@tailwindcss/forms";
What it does:
  • Resets browser default form styles
  • Makes form elements styleable with Tailwind utilities
  • Fixes cross-browser inconsistencies for inputs, selects, checkboxes, radios
Note: Less critical for shadcn/ui users (they have pre-styled form components), but still useful for basic forms.

适用场景: 不使用shadcn/ui组件构建自定义表单,或是需要跨浏览器一致的表单样式。
安装:
bash
pnpm add -D @tailwindcss/forms
配置(v4语法):
css
/* src/index.css */
@import "tailwindcss";
@plugin "@tailwindcss/forms";
作用:
  • 重置浏览器默认表单样式
  • 让表单元素可以用Tailwind工具类自定义样式
  • 修复input、select、checkbox、radio的跨浏览器不一致问题
注意: shadcn/ui用户必要性较低(已有预定义样式的表单组件),但对基础表单仍然有用。

Common Plugin Errors

常见插件错误

These errors happen when using v3 syntax in v4 projects:
❌ WRONG (v3 config file syntax):
js
// tailwind.config.js
module.exports = {
  plugins: [require('@tailwindcss/typography')]
}
❌ WRONG (@import instead of @plugin):
css
@import "@tailwindcss/typography";  /* Doesn't work */
✅ CORRECT (v4 @plugin directive):
css
/* src/index.css */
@import "tailwindcss";
@plugin "@tailwindcss/typography";
@plugin "@tailwindcss/forms";

这些错误是在v4项目中使用v3语法导致的:
❌ 错误(v3配置文件语法):
js
// tailwind.config.js
module.exports = {
  plugins: [require('@tailwindcss/typography')]
}
❌ 错误(用@import而非@plugin):
css
@import "@tailwindcss/typography";  /* 不生效 */
✅ 正确(v4 @plugin指令):
css
/* src/index.css */
@import "tailwindcss";
@plugin "@tailwindcss/typography";
@plugin "@tailwindcss/forms";

Built-in Features (No Plugin Needed)

内置功能(无需插件)

Container queries are built into Tailwind v4 core - no plugin needed:
tsx
<div className="@container">
  <div className="@md:text-lg">
    Responds to container width, not viewport
  </div>
</div>
❌ Don't install:
@tailwindcss/container-queries
(deprecated, now core feature)

容器查询已内置到Tailwind v4核心 - 无需插件:
tsx
<div className="@container">
  <div className="@md:text-lg">
    响应容器宽度而非视口宽度
  </div>
</div>
❌ 不要安装:
@tailwindcss/container-queries
(已废弃,现在是核心功能)

Reference Documentation

参考文档

For deeper understanding, see:
  • architecture.md - Deep dive into the 4-step pattern
  • dark-mode.md - Complete dark mode implementation
  • common-gotchas.md - All the ways it can break (and fixes)
  • migration-guide.md - Migrating hardcoded colors to CSS variables

如需深入理解,请查看:
  • architecture.md - 四步模式的深度解析
  • dark-mode.md - 完整暗黑模式实现
  • common-gotchas.md - 所有可能出错的场景及修复方案
  • migration-guide.md - 将硬编码颜色迁移到CSS变量

Official Documentation

官方文档


Production Example

生产环境示例

This skill is based on the WordPress Auditor project:
All patterns in this skill have been validated in production.

Questions? Issues?
  1. Check
    reference/common-gotchas.md
    first
  2. Verify all steps in the 4-step architecture
  3. Ensure
    components.json
    has
    "config": ""
  4. Delete
    tailwind.config.ts
    if it exists
  5. Check official docs: https://ui.shadcn.com/docs/tailwind-v4
本技能基于WordPress Auditor项目构建:
  • 线上地址: https://wordpress-auditor.webfonts.workers.dev
  • 技术栈: Vite + React 19 + Tailwind v4 + shadcn/ui + Cloudflare Workers
  • 暗黑模式: 完整支持系统/亮色/暗色切换
  • 版本: Tailwind v4.1.14 + shadcn/ui最新版(2025年10月)
本技能中的所有模式都已在生产环境验证。

问题?反馈?
  1. 优先查看
    reference/common-gotchas.md
  2. 验证四步架构中的所有步骤
  3. 确保
    components.json
    "config": ""
  4. 如果存在
    tailwind.config.ts
    请删除
  5. 查看官方文档: https://ui.shadcn.com/docs/tailwind-v4