tailwind-css
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTailwind CSS v4 & tailwind-variants
Tailwind CSS v4 与 tailwind-variants
Your Role
你的角色
You are an expert in Tailwind CSS v4. You understand CSS-first configuration, modern utility patterns, and type-safe
component styling with tailwind-variants.
你是Tailwind CSS v4方面的专家,了解CSS优先配置、现代工具类模式,以及使用tailwind-variants进行类型安全的组件样式设计。
Reference Documentation
参考文档
- Tailwind v4 Rules & Best Practices:
./references/TAILWIND_V4_RULES.md- Breaking changes, removed/renamed utilities, layout rules, typography, gradients, CSS variables, new v4 features, common pitfalls
- tailwind-variants Patterns:
./references/TAILWIND_VARIANTS.md- Component variants, slots API, composition, TypeScript integration, responsive variants
- Tailwind v4 规则与最佳实践:
./references/TAILWIND_V4_RULES.md- 破坏性变更、已移除/重命名的工具类、布局规则、排版、渐变、CSS变量、v4新特性、常见陷阱
- tailwind-variants 模式:
./references/TAILWIND_VARIANTS.md- 组件变体、插槽API、组合、TypeScript集成、响应式变体
Core Principles
核心原则
- Always use Tailwind CSS v4.1+
- Do not use deprecated or removed utilities - Use the replacement
- Never use - Use CSS variables, the
@applyfunction, or framework components instead--spacing() - Check for redundant classes - Remove any classes that aren't necessary
- 始终使用Tailwind CSS v4.1+版本
- 不要使用已废弃或已移除的工具类 - 使用替代方案
- 绝不使用- 改用CSS变量、
@apply函数或框架组件--spacing() - 检查冗余类 - 移除所有不必要的类
Configuration Method
配置方法
Tailwind CSS v4 uses CSS-only configuration:
- No file needed
tailwind.config.ts - Define theme tokens with or
@theme { }@theme static { } - Create custom utilities with
@utility { } - Define custom variants with
@custom-variant { }
css
@import "tailwindcss";
@theme {
--color-primary: oklch(0.72 0.11 178);
--spacing-edge: 1.5rem;
}Tailwind CSS v4采用纯CSS配置:
- 无需文件
tailwind.config.ts - 使用或
@theme { }定义主题令牌@theme static { } - 使用创建自定义工具类
@utility { } - 使用定义自定义变体
@custom-variant { }
css
@import "tailwindcss";
@theme {
--color-primary: oklch(0.72 0.11 178);
--spacing-edge: 1.5rem;
}Critical Breaking Changes (v3 → v4)
关键破坏性变更(v3 → v4)
Renamed Utilities
重命名的工具类
| v3 (deprecated) | v4 (use instead) |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| v3(已废弃) | v4(推荐使用) |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
Removed Utilities
已移除的工具类
| Deprecated | Replacement |
|---|---|
| Use opacity modifier: |
| Use opacity modifier: |
| Use opacity modifier: |
| |
| |
| |
| 已废弃工具类 | 替代方案 |
|---|---|
| 使用不透明度修饰符: |
| 使用不透明度修饰符: |
| 使用不透明度修饰符: |
| |
| |
| |
Gradient Syntax
渐变语法
Use v4 gradient syntax:
html
<!-- Linear gradients -->
<div class="bg-linear-to-br from-violet-500 to-fuchsia-500"></div>
<!-- Radial gradients -->
<div class="bg-radial-[at_50%_75%] from-sky-200 to-indigo-900"></div>
<!-- Conic gradients -->
<div class="bg-conic-180 from-indigo-600 via-indigo-50 to-indigo-600"></div>
<!-- Deprecated syntax -->
<div class="bg-gradient-to-br ..."></div>
<!-- Wrong! -->使用v4渐变语法:
html
<!-- 线性渐变 -->
<div class="bg-linear-to-br from-violet-500 to-fuchsia-500"></div>
<!-- 径向渐变 -->
<div class="bg-radial-[at_50%_75%] from-sky-200 to-indigo-900"></div>
<!-- 锥形渐变 -->
<div class="bg-conic-180 from-indigo-600 via-indigo-50 to-indigo-600"></div>
<!-- 已废弃语法 -->
<div class="bg-gradient-to-br ..."></div>
<!-- 错误! -->Responsive Design (Mobile-First)
响应式设计(移动端优先)
Tailwind is mobile-first:
- Default classes (no prefix) apply to all screen sizes
- Breakpoint prefixes (,
md:) apply at that breakpoint and abovelg: - Never use for mobile - it's redundant
sm:
html
<!-- Correct: Mobile first, then scale up -->
<div class="text-sm md:text-base lg:text-lg">
<div class="flex-col md:flex-row">
<div class="px-4 md:px-6 lg:px-8">
<!-- Hide on mobile, show on desktop -->
<div class="hidden lg:block"></div>
</div>
</div>
</div>Tailwind采用移动端优先策略:
- 默认类(无前缀)适用于所有屏幕尺寸
- 断点前缀(,
md:)在该断点及以上尺寸生效lg: - 绝不要为移动端使用前缀 - 这是多余的
sm:
html
<!-- 正确:移动端优先,然后向上适配 -->
<div class="text-sm md:text-base lg:text-lg">
<div class="flex-col md:flex-row">
<div class="px-4 md:px-6 lg:px-8">
<!-- 移动端隐藏,桌面端显示 -->
<div class="hidden lg:block"></div>
</div>
</div>
</div>Layout Best Practices
布局最佳实践
Always use gap for flex/grid spacing
始终为flex/grid布局使用gap设置间距
html
<!-- Don't use space utilities in flex/grid -->
<div class="flex space-x-4">...</div>
<!-- Wrong! -->
<!-- Use gap -->
<div class="flex gap-4">...</div>
<!-- Correct -->html
<!-- 不要在flex/grid中使用space工具类 -->
<div class="flex space-x-4">...</div>
<!-- 错误! -->
<!-- 使用gap -->
<div class="flex gap-4">...</div>
<!-- 正确 -->Use min-h-dvh
instead of min-h-screen
min-h-dvhmin-h-screen使用min-h-dvh
替代min-h-screen
min-h-dvhmin-h-screenmin-h-screenmin-h-screenUse size-*
for equal width/height
size-*使用size-*
设置等宽高
size-*html
<div class="w-10 h-10">...</div>
<!-- Verbose -->
<div class="size-10">...</div>
<!-- Preferred -->html
<div class="w-10 h-10">...</div>
<!-- 冗余写法 -->
<div class="size-10">...</div>
<!-- 推荐写法 -->Typography
排版
Use line-height modifiers, not classes:
leading-*html
<!-- Don't use separate leading classes -->
<p class="text-base leading-7">...</p>
<!-- Wrong! -->
<!-- Use line-height modifiers -->
<p class="text-base/7">...</p>
<!-- Correct -->使用行高修饰符,而非类:
leading-*html
<!-- 不要使用单独的leading类 -->
<p class="text-base leading-7">...</p>
<!-- 错误! -->
<!-- 使用行高修饰符 -->
<p class="text-base/7">...</p>
<!-- 正确 -->CSS Variables
CSS变量
Tailwind v4 exposes theme values as CSS variables:
css
.custom-element {
background: var(--color-red-500);
padding: var(--spacing-4);
border-radius: var(--radius-lg);
}Use function for calculations:
--spacing()css
.custom-class {
margin-top: calc(100vh - --spacing(16));
}Tailwind v4将主题值暴露为CSS变量:
css
.custom-element {
background: var(--color-red-500);
padding: var(--spacing-4);
border-radius: var(--radius-lg);
}使用函数进行计算:
--spacing()css
.custom-class {
margin-top: calc(100vh - --spacing(16));
}tailwind-variants Quick Start
tailwind-variants 快速入门
Type-safe component variants with automatic class conflict resolution:
typescript
import { tv, type VariantProps } from "tailwind-variants";
const button = tv({
base: "font-medium rounded-lg transition-colors",
variants: {
color: {
primary: "bg-blue-500 text-white hover:bg-blue-600",
secondary: "bg-gray-500 text-white hover:bg-gray-600"
},
size: {
sm: "text-sm px-3 py-1.5",
md: "text-base px-4 py-2",
lg: "text-lg px-6 py-3"
}
},
defaultVariants: {
color: "primary",
size: "md"
}
});
type ButtonVariants = VariantProps<typeof button>;
// Usage
button(); // Uses defaults
button({ color: "secondary", size: "lg" });See for:
./references/TAILWIND_VARIANTS.md- Slots API for multi-part components
- Compound variants
- Component composition with
extend - TypeScript integration patterns
具备自动类冲突解决的类型安全组件变体:
typescript
import { tv, type VariantProps } from "tailwind-variants";
const button = tv({
base: "font-medium rounded-lg transition-colors",
variants: {
color: {
primary: "bg-blue-500 text-white hover:bg-blue-600",
secondary: "bg-gray-500 text-white hover:bg-gray-600"
},
size: {
sm: "text-sm px-3 py-1.5",
md: "text-base px-4 py-2",
lg: "text-lg px-6 py-3"
}
},
defaultVariants: {
color: "primary",
size: "md"
}
});
type ButtonVariants = VariantProps<typeof button>;
// 使用示例
button(); // 使用默认值
button({ color: "secondary", size: "lg" });更多内容请查看 :
./references/TAILWIND_VARIANTS.md- 适用于多部分组件的Slots API
- 复合变体
- 使用进行组件组合
extend - TypeScript集成模式
Quick Reference
快速参考
| Topic | Pattern |
|---|---|
| Gradients | |
| Opacity | |
| Line height | |
| Spacing | Use |
| Equal size | |
| Full height | |
| Component variants | |
| 主题 | 写法模式 |
|---|---|
| 渐变 | |
| 不透明度 | |
| 行高 | |
| 间距 | 在flex/grid中使用 |
| 等宽高 | |
| 全屏高度 | |
| 组件变体 | 来自tailwind-variants的 |
Common Tasks
常见任务
Creating a component with variants:
- Read for patterns
./references/TAILWIND_VARIANTS.md - Use for type-safe variants
tv() - Extract types with
VariantProps<typeof component>
Debugging styles:
- Check for breaking changes
./references/TAILWIND_V4_RULES.md - Verify gradient syntax (, not
bg-linear-*)bg-gradient-* - Check opacity syntax (, not
bg-red-500/50)bg-opacity-*
创建带变体的组件:
- 查看 了解相关模式
./references/TAILWIND_VARIANTS.md - 使用创建类型安全的变体
tv() - 使用提取类型
VariantProps<typeof component>
调试样式:
- 查看 了解破坏性变更
./references/TAILWIND_V4_RULES.md - 验证渐变语法(使用,而非
bg-linear-*)bg-gradient-* - 检查不透明度语法(使用,而非
bg-red-500/50)bg-opacity-*