tailwind-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWhen to use
适用场景
Use this skill when working with Tailwind CSS code. AI agents are trained on Tailwind v3 data and
consistently generate outdated patterns - wrong config files, deprecated directives, removed
utilities, and verbose class lists. This skill enforces Tailwind CSS v4 patterns.
在处理Tailwind CSS代码时使用本技能。AI Agent基于Tailwind v3数据训练,会持续生成过时的代码模式——错误的配置文件、已弃用的指令、已移除的工具类以及冗长的类名列表。本技能可强制遵循Tailwind CSS v4的代码模式。
Critical Rules
核心规则
1. Use CSS @import Instead of @tailwind Directives
1. 使用CSS @import替代@tailwind指令
Wrong (agents do this):
css
@tailwind base;
@tailwind components;
@tailwind utilities;Correct:
css
@import "tailwindcss";Why: Tailwind v4 removed directives entirely. Use a standard CSS
statement.
@tailwind@import错误写法(AI Agent常这么生成):
css
@tailwind base;
@tailwind components;
@tailwind utilities;正确写法:
css
@import "tailwindcss";原因: Tailwind v4已完全移除指令,需使用标准CSS的语句。
@tailwind@import2. Use CSS-First Configuration with @theme
2. 使用@theme实现CSS优先配置
Wrong (agents do this):
js
// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
brand: "#3b82f6",
},
spacing: {
18: "4.5rem",
},
},
},
};Correct:
css
@import "tailwindcss";
@theme {
--color-brand: #3b82f6;
--spacing-18: 4.5rem;
}Why: Tailwind v4 uses CSS-first configuration with the directive. No
needed for most projects.
@themetailwind.config.js错误写法(AI Agent常这么生成):
js
// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
brand: "#3b82f6",
},
spacing: {
18: "4.5rem",
},
},
},
};正确写法:
css
@import "tailwindcss";
@theme {
--color-brand: #3b82f6;
--spacing-18: 4.5rem;
}原因: Tailwind v4采用CSS优先的配置方式,通过指令实现。大多数项目无需文件。
@themetailwind.config.js3. Use @tailwindcss/postcss Plugin
3. 使用@tailwindcss/postcss插件
Wrong (agents do this):
js
// postcss.config.mjs
export default {
plugins: {
"postcss-import": {},
tailwindcss: {},
autoprefixer: {},
},
};Correct:
js
// postcss.config.mjs
export default {
plugins: {
"@tailwindcss/postcss": {},
},
};Why: In v4, and are handled automatically. The plugin is
, not .
postcss-importautoprefixer@tailwindcss/postcsstailwindcss错误写法(AI Agent常这么生成):
js
// postcss.config.mjs
export default {
plugins: {
"postcss-import": {},
tailwindcss: {},
autoprefixer: {},
},
};正确写法:
js
// postcss.config.mjs
export default {
plugins: {
"@tailwindcss/postcss": {},
},
};原因: 在v4版本中,和已自动集成。需使用插件,而非。
postcss-importautoprefixer@tailwindcss/postcsstailwindcss4. Use Modern Opacity Syntax
4. 使用现代透明度语法
Wrong (agents do this):
html
<div class="bg-red-500 bg-opacity-50">
<div class="text-blue-600 text-opacity-75"></div>
</div>Correct:
html
<div class="bg-red-500/50">
<div class="text-blue-600/75"></div>
</div>Why: The , , , and
utilities were removed in v4. Use the slash modifier syntax.
bg-opacity-*text-opacity-*border-opacity-*placeholder-opacity-*错误写法(AI Agent常这么生成):
html
<div class="bg-red-500 bg-opacity-50">
<div class="text-blue-600 text-opacity-75"></div>
</div>正确写法:
html
<div class="bg-red-500/50">
<div class="text-blue-600/75"></div>
</div>原因: Tailwind v4已移除、、和工具类,需使用斜杠修饰符语法。
bg-opacity-*text-opacity-*border-opacity-*placeholder-opacity-*5. Use CSS Variables for Custom Colors
5. 使用CSS变量定义自定义颜色
Wrong (agents do this):
html
<div class="bg-[#3b82f6]"></div>Correct:
css
@theme {
--color-brand: #3b82f6;
}html
<div class="bg-brand"></div>Why: Arbitrary values () should be rare. Define custom colors in so
they're reusable and consistent.
bg-[#3b82f6]@theme错误写法(AI Agent常这么生成):
html
<div class="bg-[#3b82f6]"></div>正确写法:
css
@theme {
--color-brand: #3b82f6;
}html
<div class="bg-brand"></div>原因: 应尽量避免使用任意值写法(如)。在中定义自定义颜色可确保复用性和一致性。
bg-[#3b82f6]@theme6. Use Container Queries with @container
6. 使用@container实现容器查询
Wrong (agents do this):
html
<div class="md:flex-row flex-col"></div>Correct (when sizing should be based on parent, not viewport):
html
<div class="@container">
<div class="flex flex-col @md:flex-row">
<!-- Responds to container width, not viewport -->
</div>
</div>Why: Tailwind v4 has built-in container query support. Use on the parent and
, , variants on children for component-level responsive design.
@container@sm:@md:@lg:错误写法(AI Agent常这么生成):
html
<div class="md:flex-row flex-col"></div>正确写法(当尺寸需基于父容器而非视口时):
html
<div class="@container">
<div class="flex flex-col @md:flex-row">
<!-- 响应容器宽度而非视口宽度 -->
</div>
</div>原因: Tailwind v4内置容器查询支持。在父元素上使用,在子元素上使用、、变体可实现组件级响应式设计。
@container@sm:@md:@lg:7. Use @utility for Custom Utilities
7. 使用@utility定义自定义工具类
Wrong (agents do this):
css
@layer utilities {
.content-auto {
content-visibility: auto;
}
}Correct:
css
@utility content-auto {
content-visibility: auto;
}Why: Tailwind v4 uses directive instead of for custom utilities.
@utility@layer utilities错误写法(AI Agent常这么生成):
css
@layer utilities {
.content-auto {
content-visibility: auto;
}
}正确写法:
css
@utility content-auto {
content-visibility: auto;
}原因: Tailwind v4使用指令替代来定义自定义工具类。
@utility@layer utilities8. Use Correct Important Modifier
8. 使用正确的!important修饰符
Wrong (agents do this):
js
// tailwind.config.js
module.exports = {
important: true,
};Correct:
css
@import "tailwindcss" important;Why: The option moved from config to the CSS import statement in v4.
important错误写法(AI Agent常这么生成):
js
// tailwind.config.js
module.exports = {
important: true,
};正确写法:
css
@import "tailwindcss" important;原因: Tailwind v4中,配置项已从配置文件移至CSS导入语句中。
important9. Use Renamed Utilities
9. 使用重命名后的工具类
Wrong (agents do this):
html
<div class="shadow-sm ring-1 ring-gray-900/5">
<div class="blur-sm">
<div class="rounded-sm"></div>
</div>
</div>Correct:
html
<div class="shadow-xs ring-1 ring-gray-900/5">
<div class="blur-xs">
<div class="rounded-xs"></div>
</div>
</div>Why: In v4, was renamed to , to , to
, to , etc. The old values now map to what was previously
the default.
shadow-smshadow-xsshadowshadow-smblur-smblur-xsrounded-smrounded-xs-sm错误写法(AI Agent常这么生成):
html
<div class="shadow-sm ring-1 ring-gray-900/5">
<div class="blur-sm">
<div class="rounded-sm"></div>
</div>
</div>正确写法:
html
<div class="shadow-xs ring-1 ring-gray-900/5">
<div class="blur-xs">
<div class="rounded-xs"></div>
</div>
</div>原因: Tailwind v4中,已重命名为,重命名为,重命名为,重命名为等。原有的后缀现在对应之前的默认值。
shadow-smshadow-xsshadowshadow-smblur-smblur-xsrounded-smrounded-xs-sm10. Use Modern Gradient Syntax
10. 使用现代渐变语法
Wrong (agents do this):
html
<div class="bg-gradient-to-r from-blue-500 to-purple-500"></div>Correct:
html
<div class="bg-linear-to-r from-blue-500 to-purple-500"></div>Why: Tailwind v4 renamed to and added support for other
gradient types like and .
bg-gradient-to-*bg-linear-to-*bg-conic-*bg-radial-*错误写法(AI Agent常这么生成):
html
<div class="bg-gradient-to-r from-blue-500 to-purple-500"></div>正确写法:
html
<div class="bg-linear-to-r from-blue-500 to-purple-500"></div>原因: Tailwind v4已将重命名为,并新增了和等其他渐变类型的支持。
bg-gradient-to-*bg-linear-to-*bg-conic-*bg-radial-*11. Use not-* Variant for Negation
11. 使用not-*变体实现否定样式
Wrong (agents do this):
html
<div class="hover:bg-blue-500">
<!-- No way to style non-hovered state specifically -->
</div>Correct:
html
<div class="not-hover:opacity-75 hover:opacity-100"></div>Why: Tailwind v4 added the variant for styling elements that do NOT match a condition.
not-*错误写法(AI Agent常这么生成):
html
<div class="hover:bg-blue-500">
<!-- 无法专门设置非 hover 状态的样式 -->
</div>正确写法:
html
<div class="not-hover:opacity-75 hover:opacity-100"></div>原因: Tailwind v4新增了变体,用于为不满足特定条件的元素设置样式。
not-*12. Use @starting-style for Entry Animations
12. 使用@starting-style实现入场动画
Wrong (agents do this):
css
.modal {
opacity: 0;
transition: opacity 0.3s;
}
.modal.open {
opacity: 1;
}Correct:
html
<div class="starting:opacity-0 opacity-100 transition-opacity duration-300"></div>Why: Tailwind v4 supports via the variant for CSS-only entry
animations without JavaScript.
@starting-stylestarting:错误写法(AI Agent常这么生成):
css
.modal {
opacity: 0;
transition: opacity 0.3s;
}
.modal.open {
opacity: 1;
}正确写法:
html
<div class="starting:opacity-0 opacity-100 transition-opacity duration-300"></div>原因: Tailwind v4通过变体支持,无需JavaScript即可实现纯CSS入场动画。
starting:@starting-stylePatterns
推荐模式
- Define all custom design tokens in blocks
@theme - Use as the single entry point
@import "tailwindcss" - Use container queries (+
@container) for component-level responsive design@md: - Use the slash modifier for opacity: ,
bg-blue-500/50text-gray-900/75 - Use for custom utilities,
@utilityfor custom variants@variant - Use for gradients (not
bg-linear-to-*)bg-gradient-to-*
- 在块中定义所有自定义设计令牌
@theme - 使用作为唯一入口
@import "tailwindcss" - 使用容器查询(+
@container)实现组件级响应式设计@md: - 使用斜杠修饰符设置透明度:、
bg-blue-500/50text-gray-900/75 - 使用定义自定义工具类,使用
@utility定义自定义变体@variant - 使用实现渐变(而非
bg-linear-to-*)bg-gradient-to-*
Anti-Patterns
反模式
- NEVER create a unless you need JavaScript-based dynamic config
tailwind.config.js - NEVER use - use
@tailwind base/components/utilities@import "tailwindcss" - NEVER use ,
bg-opacity-*- use slash modifier syntaxtext-opacity-* - NEVER use for custom utilities - use
@layer utilities@utility - NEVER use - use
bg-gradient-to-*bg-linear-to-* - NEVER use or
postcss-importwith v4 - they're built inautoprefixer - NEVER use when you mean the smallest shadow - it's now
shadow-smshadow-xs
- 除非需要基于JavaScript的动态配置,否则绝不创建文件
tailwind.config.js - 绝不使用——应使用
@tailwind base/components/utilities@import "tailwindcss" - 绝不使用、
bg-opacity-*——应使用斜杠修饰符语法text-opacity-* - 绝不使用定义自定义工具类——应使用
@layer utilities@utility - 绝不使用——应使用
bg-gradient-to-*bg-linear-to-* - 绝不与v4版本搭配使用或
postcss-import——它们已内置autoprefixer - 当需要最小阴影时,绝不使用——现在对应的是
shadow-smshadow-xs