tailwindcss-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTailwind CSS v4.x Best Practices
Tailwind CSS v4.x 最佳实践
Covers Tailwind CSS v4.0 through v4.2.2 (latest stable as of March 2026). Always check the official docs at https://tailwindcss.com/docs for the latest.
Tailwind CSS is a utility-first CSS framework. Instead of writing custom CSS, you compose designs using utility classes directly in your markup. Tailwind v4 introduced CSS-first configuration with variables, for custom utilities, and for custom variants — replacing the old approach entirely.
@theme@utility@custom-varianttailwind.config.js涵盖Tailwind CSS v4.0至v4.2.2版本(截至2026年3月的最新稳定版)。请始终访问官方文档https://tailwindcss.com/docs获取最新内容。
Tailwind CSS是一款优先实用类的CSS框架。无需编写自定义CSS,你可以直接在标记语言中组合实用类来完成设计。Tailwind v4引入了基于CSS的配置方式,使用变量、定义自定义实用类、定义自定义变体——完全取代了旧版的配置方式。
@theme@utility@custom-varianttailwind.config.jsCritical v4 Migration Gotchas
v4迁移关键注意事项
These are the most common mistakes when working with Tailwind v4. If you're migrating from v3 or using v4 for the first time, read the upgrade guide — but here are the top trip-ups:
| v3 (old) | v4 (correct) | Why it changed |
|---|---|---|
| | Important modifier moved from prefix to suffix |
| | Opacity modifiers removed; use slash syntax on the color |
| | Shadow scale shifted down one step |
| | Shadow scale shifted down one step |
| | Border radius scale shifted down one step |
| | Border radius scale shifted down one step |
| | Default ring width changed from 3px to 1px |
| | Renamed for clarity |
| | Shorter alias is now the only form |
| | Shorter alias is now the only form |
| | Renamed for consistency |
| | Blur scale shifted down one step |
| | Single CSS import replaces three directives |
| | CSS-first configuration replaces JS config |
| | Dark mode config moves to CSS |
| | CSS variable arbitrary values use parentheses |
| | Theme function uses CSS variable names |
| | Custom utilities use dedicated directive |
| | Deprecated in v4.2 |
这些是使用Tailwind v4时最常见的错误。如果你正从v3迁移或首次使用v4,请阅读升级指南——以下是最容易踩坑的点:
| v3(旧版) | v4(正确写法) | 变更原因 |
|---|---|---|
| | 重要修饰符从前缀移至后缀 |
| | 移除透明度修饰符;使用颜色后的斜杠语法 |
| | 阴影层级整体下移一级 |
| | 阴影层级整体下移一级 |
| | 边框圆角层级整体下移一级 |
| | 边框圆角层级整体下移一级 |
| | 默认环形宽度从3px改为1px |
| | 重命名以提高清晰度 |
| | 仅保留更短的别名写法 |
| | 仅保留更短的别名写法 |
| | 重命名以保持一致性 |
| | 模糊效果层级整体下移一级 |
| | 单个CSS导入语句替代三个指令 |
| CSS中的 | 基于CSS的配置替代JS配置 |
| | 暗色模式配置移至CSS |
| | CSS变量任意值使用括号 |
| | Theme函数使用CSS变量名 |
| | 自定义实用类使用专用指令 |
| | v4.2中已弃用 |
Core References
核心参考
| Topic | Description | Reference |
|---|---|---|
| Installation | Vite, PostCSS, Webpack, CLI, and CDN setup | core-installation |
| Utility Classes | Understanding Tailwind's utility-first approach | core-utility-classes |
| Theme Variables | Design tokens, | core-theme |
| Responsive Design | Mobile-first breakpoints, responsive variants, container queries | core-responsive |
| Variants | Conditional styling with state, pseudo-class, media query, and pointer variants | core-variants |
| Preflight | Tailwind's base styles and how to extend or disable them | core-preflight |
| Source Detection | How Tailwind detects classes, | core-source-detection |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 安装 | Vite、PostCSS、Webpack、CLI和CDN设置 | core-installation |
| 实用类 | 理解Tailwind的优先实用类方法 | core-utility-classes |
| 主题变量 | 设计标记、 | core-theme |
| 响应式设计 | 移动优先断点、响应式变体、容器查询 | core-responsive |
| 变体 | 使用状态、伪类、媒体查询和指针变体实现条件样式 | core-variants |
| 基础样式 | Tailwind的基础样式及扩展或禁用方法 | core-preflight |
| 源检测 | Tailwind如何检测类、 | core-source-detection |
Layout
布局
Display & Flexbox & Grid
显示与Flexbox与Grid
| Topic | Description | Reference |
|---|---|---|
| Display | flex, grid, block, inline, hidden, sr-only, flow-root, contents | layout-display |
| Flexbox | flex-direction, justify, items, gap, grow, shrink, wrap, order | layout-flexbox |
| Grid | grid-cols, grid-rows, gap, place-items, col-span, row-span, subgrid | layout-grid |
| Aspect Ratio | Controlling element aspect ratio for responsive media | layout-aspect-ratio |
| Columns | Multi-column layout for magazine-style or masonry layouts | layout-columns |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 显示 | flex、grid、block、inline、hidden、sr-only、flow-root、contents | layout-display |
| Flexbox | flex-direction、justify、items、gap、grow、shrink、wrap、order | layout-flexbox |
| Grid | grid-cols、grid-rows、gap、place-items、col-span、row-span、subgrid | layout-grid |
| 宽高比 | 控制元素宽高比以适配响应式媒体 | layout-aspect-ratio |
| 多列布局 | 杂志风格或瀑布流布局的多列设置 | layout-columns |
Positioning
定位
| Topic | Description | Reference |
|---|---|---|
| Position | Controlling element positioning with static, relative, absolute, fixed, and sticky | layout-position |
| Inset | Placement of positioned elements with inset, logical inset ( | layout-inset |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 定位 | 使用static、relative、absolute、fixed和sticky控制元素定位 | layout-position |
| 内边距偏移 | 定位元素的偏移设置,包括逻辑偏移( | layout-inset |
Sizing
尺寸
| Topic | Description | Reference |
|---|---|---|
| Width | Setting element width with spacing scale, fractions, container sizes, viewport units | layout-width |
| Height | Setting element height with spacing scale, fractions, viewport units | layout-height |
| Min & Max Sizing | min-width, max-width, min-height, max-height constraints | layout-min-max-sizing |
| Logical Sizing | Writing-mode-aware sizing: | layout-logical-properties |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 宽度 | 使用间距比例、分数、容器尺寸、视窗单位设置元素宽度 | layout-width |
| 高度 | 使用间距比例、分数、视窗单位设置元素高度 | layout-height |
| 最小与最大尺寸 | min-width、max-width、min-height、max-height约束 | layout-min-max-sizing |
| 逻辑尺寸 | 支持书写模式的尺寸: | layout-logical-properties |
Spacing
间距
| Topic | Description | Reference |
|---|---|---|
| Margin | Margins with spacing scale, negative values, logical properties ( | layout-margin |
| Padding | Padding with spacing scale, logical properties ( | layout-padding |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 外边距 | 带间距比例、负值、逻辑属性( | layout-margin |
| 内边距 | 带间距比例、逻辑属性( | layout-padding |
Overflow
溢出
| Topic | Description | Reference |
|---|---|---|
| Overflow | Controlling how elements handle content that overflows | layout-overflow |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 溢出 | 控制元素处理溢出内容的方式 | layout-overflow |
Images & Replaced Elements
图片与替换元素
| Topic | Description | Reference |
|---|---|---|
| Object Fit & Position | Controlling how images and video are resized and positioned | layout-object-fit-position |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 对象适配与定位 | 控制图片和视频的缩放与定位方式 | layout-object-fit-position |
Tables
表格
| Topic | Description | Reference |
|---|---|---|
| Table Layout | border-collapse, table-auto, table-fixed | layout-tables |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 表格布局 | border-collapse、table-auto、table-fixed | layout-tables |
Transforms
变换
| Topic | Description | Reference |
|---|---|---|
| Transform Base | Base transform utilities, hardware acceleration, custom transform values | transform-base |
| Translate | Translating elements on x, y, z axes with spacing scale and percentages | transform-translate |
| Rotate | Rotating elements in 2D and 3D space | transform-rotate |
| Scale | Scaling elements uniformly or on specific axes | transform-scale |
| Skew | Skewing elements on x and y axes | transform-skew |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 变换基础 | 基础变换实用类、硬件加速、自定义变换值 | transform-base |
| 平移 | 使用间距比例和百分比在x、y、z轴平移元素 | transform-translate |
| 旋转 | 在2D和3D空间旋转元素 | transform-rotate |
| 缩放 | 均匀或沿特定轴缩放元素 | transform-scale |
| 倾斜 | 在x和y轴倾斜元素 | transform-skew |
Typography
排版
| Topic | Description | Reference |
|---|---|---|
| Font & Text | Font size, weight, color, line-height, letter-spacing, decoration, truncate, | typography-font-text |
| Text Align | Controlling text alignment with left, center, right, justify | typography-text-align |
| List Style | list-style-type, list-style-position for bullets and markers | typography-list-style |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 字体与文本 | 字体大小、字重、颜色、行高、字母间距、装饰、截断、 | typography-font-text |
| 文本对齐 | 使用左对齐、居中、右对齐、两端对齐控制文本对齐方式 | typography-text-align |
| 列表样式 | 项目符号和标记的list-style-type、list-style-position | typography-list-style |
Visual
视觉效果
| Topic | Description | Reference |
|---|---|---|
| Background | Background color, gradient, image, size, position | visual-background |
| Border | Border width, color, radius, divide, ring, block border utilities ( | visual-border |
| Effects | Box shadow, opacity, mix-blend, backdrop-blur, filter, colored drop shadows | visual-effects |
| SVG | fill, stroke, stroke-width for SVG and icon styling | visual-svg |
| Text Shadow | Text shadow sizes, colors, and opacity modifiers (v4.1) | effects-text-shadow |
| Mask | Composable mask utilities with gradient and radial masks (v4.1) | effects-mask |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 背景 | 背景颜色、渐变、图片、尺寸、位置 | visual-background |
| 边框 | 边框宽度、颜色、圆角、分隔线、环形、块级边框实用类( | visual-border |
| 特效 | 盒阴影、透明度、混合模式、背景模糊、滤镜、彩色投影 | visual-effects |
| SVG | SVG和图标样式的fill、stroke、stroke-width | visual-svg |
| 文本阴影 | 文本阴影尺寸、颜色和透明度修饰符(v4.1) | effects-text-shadow |
| 遮罩 | 可组合的遮罩实用类,包括渐变和径向遮罩(v4.1) | effects-mask |
Effects & Interactivity
特效与交互
| Topic | Description | Reference |
|---|---|---|
| Transition & Animation | CSS transitions, animation keyframes, reduced motion | effects-transition-animation |
| Visibility & Interactivity | Visibility, cursor, pointer-events, user-select, z-index | effects-visibility-interactivity |
| Form Controls | accent-color, appearance, caret-color, resize | effects-form-controls |
| Scroll Snap | scroll-snap-type, scroll-snap-align for carousels | effects-scroll-snap |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 过渡与动画 | CSS过渡、动画关键帧、减少动画 | effects-transition-animation |
| 可见性与交互 | 可见性、光标、指针事件、用户选择、z-index | effects-visibility-interactivity |
| 表单控件 | accent-color、外观、光标颜色、调整大小 | effects-form-controls |
| 滚动捕捉 | 轮播图的scroll-snap-type、scroll-snap-align | effects-scroll-snap |
Features
功能特性
Dark Mode
暗色模式
| Topic | Description | Reference |
|---|---|---|
| Dark Mode | Dark mode with | features-dark-mode |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 暗色模式 | 带 | features-dark-mode |
Migration
迁移
| Topic | Description | Reference |
|---|---|---|
| Upgrade Guide | Migrating from v3 to v4, all renamed/removed utilities, scale shifts, config migration | features-upgrade |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 升级指南 | 从v3迁移到v4、所有重命名/移除的实用类、层级变更、配置迁移 | features-upgrade |
Customization
自定义
| Topic | Description | Reference |
|---|---|---|
| Custom Styles | Adding custom styles, utilities with | features-custom-styles |
| Functions & Directives | Tailwind's CSS directives ( | features-functions-directives |
| Content Detection | How Tailwind detects classes, | features-content-detection |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 自定义样式 | 添加自定义样式、使用 | features-custom-styles |
| 函数与指令 | Tailwind的CSS指令( | features-functions-directives |
| 内容检测 | Tailwind如何检测类、 | features-content-detection |
Best Practices
最佳实践
| Topic | Description | Reference |
|---|---|---|
| Utility Patterns | Managing duplication, conflicts, important modifier, when to use components | best-practices-utility-patterns |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 实用类模式 | 管理重复代码、冲突、重要修饰符、何时使用组件 | best-practices-utility-patterns |
Key Recommendations
关键建议
- Use utility classes directly in markup — compose designs by combining utilities
- Customize with directive — define design tokens as CSS variables, not in JS config
@theme - Mobile-first responsive design — unprefixed utilities for mobile, prefixed for breakpoints
- Use complete class names — never construct classes dynamically with string interpolation
- Leverage variants — stack variants for complex conditional styling ()
dark:md:hover:bg-blue-600 - Prefer CSS-first configuration — use ,
@theme, and@utilityover JavaScript configs@custom-variant - Use oklch for custom colors — Tailwind v4 defaults to oklch; prefer it for perceptual uniformity
- Use rem for custom breakpoints — e.g., not
--breakpoint-3xl: 90rem1440px - Know the scale shifts — shadow, rounded, and blur all shifted down one step in v4
- Use (not
@custom-variant) — for defining custom variants like class-based dark mode@variant
- 直接在标记中使用实用类——通过组合实用类来构建设计
- 使用指令自定义——将设计标记定义为CSS变量,而非JS配置
@theme - 移动端优先的响应式设计——无前缀的实用类用于移动端,带前缀的用于断点
- 使用完整类名——永远不要通过字符串拼接动态构建类名
- 利用变体——堆叠变体实现复杂的条件样式(如)
dark:md:hover:bg-blue-600 - 优先使用基于CSS的配置——使用、
@theme和@utility而非JavaScript配置@custom-variant - 使用oklch定义自定义颜色——Tailwind v4默认使用oklch;优先选择以保证感知一致性
- 使用rem定义自定义断点——例如而非
--breakpoint-3xl: 90rem1440px - 了解层级变更——v4中阴影、圆角和模糊效果的层级均下移一级
- 使用(而非
@custom-variant)——用于定义类基暗色模式等自定义变体@variant