tailwindcss-core
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTailwind CSS Core v4.1
Tailwind CSS Core v4.1
Overview
概述
Tailwind CSS v4.1 introduces a CSS-first approach that eliminates the need for a traditional file. All configuration is now done directly in your CSS files via specialized directives.
tailwind.config.jsTailwind CSS v4.1引入了CSS优先的方式,不再需要传统的文件。现在所有配置都可以通过专用指令直接在CSS文件中完成。
tailwind.config.jsKey Concepts
核心概念
1. @import "tailwindcss"
1. @import "tailwindcss"
Entry point to load Tailwind CSS. Place at the beginning of your main CSS file.
css
@import "tailwindcss";This directive automatically loads:
- Base utilities
- Responsive variants
- Layers (theme, base, components, utilities)
加载Tailwind CSS的入口点。放置在主CSS文件的开头。
css
@import "tailwindcss";该指令会自动加载:
- 基础工具类
- 响应式变体
- 层级(theme、base、components、utilities)
2. @theme
2. @theme
Directive to define or customize theme values via CSS custom properties.
css
@theme {
--color-primary: #3b82f6;
--color-secondary: #ef4444;
--spacing-custom: 2.5rem;
--radius-lg: 1rem;
}Variables are accessible in generated utilities:
- → classes
--color-*,bg-primary, etc.text-primary - → classes
--spacing-*,p-custom, etc.m-custom - → classes
--radius-*, etc.rounded-lg
通过CSS自定义属性定义或自定义主题值的指令。
css
@theme {
--color-primary: #3b82f6;
--color-secondary: #ef4444;
--spacing-custom: 2.5rem;
--radius-lg: 1rem;
}这些变量可在生成的工具类中使用:
- → 对应类如
--color-*、bg-primary等text-primary - → 对应类如
--spacing-*、p-custom等m-custom - → 对应类如
--radius-*等rounded-lg
3. @source
3. @source
Directive to include additional source files with glob patterns.
css
@source "./routes/**/*.{ts,tsx}";
@source "./components/**/*.{tsx,jsx}";
@source "../packages/ui/src/**/*.{ts,tsx}";Tailwind will scan these files to generate utilities used in your project.
使用通配符模式引入额外源文件的指令。
css
@source "./routes/**/*.{ts,tsx}";
@source "./components/**/*.{tsx,jsx}";
@source "../packages/ui/src/**/*.{ts,tsx}";Tailwind会扫描这些文件,为项目中用到的工具类生成对应样式。
4. @utility and @variant
4. @utility 和 @variant
Directives to create custom utilities and variants.
css
@utility truncate {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
@variant group-hover {
.group:hover &
}用于创建自定义工具类和变体的指令。
css
@utility truncate {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
@variant group-hover {
.group:hover &
}5. @apply
5. @apply
Directive to apply Tailwind classes in your custom CSS rules.
css
.btn {
@apply px-4 py-2 rounded-lg font-semibold;
}
.btn-primary {
@apply bg-blue-500 text-white hover:bg-blue-600;
}在自定义CSS规则中应用Tailwind类的指令。
css
.btn {
@apply px-4 py-2 rounded-lg font-semibold;
}
.btn-primary {
@apply bg-blue-500 text-white hover:bg-blue-600;
}6. @config
6. @config
Directive to load external configuration if needed.
css
@config "./tailwind.config.js";(Optional in v4.1, mainly used for backward compatibility)
用于加载外部配置的指令(如有需要)。
css
@config "./tailwind.config.js";(在v4.1中为可选,主要用于向后兼容)
Dark Mode
深色模式
Dark mode configuration in Tailwind v4.1:
css
@import "tailwindcss";
/* Use system preference */
@variant dark (&:is(.dark *));Or via manual class:
css
@variant dark (&.dark);Tailwind v4.1中的深色模式配置:
css
@import "tailwindcss";
/* 使用系统偏好设置 */
@variant dark (&:is(.dark *));或通过手动类控制:
css
@variant dark (&.dark);Responsive Breakpoints
响应式断点
Breakpoints are defined via :
@themecss
@theme {
--breakpoint-sm: 640px;
--breakpoint-md: 768px;
--breakpoint-lg: 1024px;
--breakpoint-xl: 1280px;
--breakpoint-2xl: 1536px;
}Responsive variants are used with utilities:
html
<div class="text-sm md:text-base lg:text-lg"></div>断点可通过定义:
@themecss
@theme {
--breakpoint-sm: 640px;
--breakpoint-md: 768px;
--breakpoint-lg: 1024px;
--breakpoint-xl: 1280px;
--breakpoint-2xl: 1536px;
}响应式变体可与工具类配合使用:
html
<div class="text-sm md:text-base lg:text-lg"></div>Layer Hierarchy
层级结构
css
@layer theme, base, components, utilities;
@import "tailwindcss";
/* Your customizations */
@layer components {
.btn { @apply px-4 py-2 rounded; }
}
@layer utilities {
.text-shadow { text-shadow: 0 2px 4px rgba(0,0,0,0.1); }
}css
@layer theme, base, components, utilities;
@import "tailwindcss";
/* 你的自定义内容 */
@layer components {
.btn { @apply px-4 py-2 rounded; }
}
@layer utilities {
.text-shadow { text-shadow: 0 2px 4px rgba(0,0,0,0.1); }
}Plugin Integration
插件集成
Load Tailwind plugins:
css
@import "tailwindcss";
@plugin "flowbite/plugin";
@source "../node_modules/flowbite";加载Tailwind插件:
css
@import "tailwindcss";
@plugin "flowbite/plugin";
@source "../node_modules/flowbite";Specificity Order
优先级顺序
In CSS-first, import and declaration order determines specificity:
- - Base utilities
@import "tailwindcss" - - Theme variables
@theme { ... } - - Custom components
@layer components { ... } - - Custom utilities
@layer utilities { ... }
在CSS优先模式下,导入和声明的顺序决定了样式优先级:
- - 基础工具类
@import "tailwindcss" - - 主题变量
@theme { ... } - - 自定义组件
@layer components { ... } - - 自定义工具类
@layer utilities { ... }
CSS-first Mode Benefits
CSS优先模式的优势
- No complex JavaScript config file
- Type-safe via CSS variables
- Declarative and readable configuration
- Better integration with CSS preprocessors
- Simplified maintenance for large projects
- 无需复杂的JavaScript配置文件
- 通过CSS变量实现类型安全
- 声明式且可读性强的配置
- 与CSS预处理器更好地集成
- 简化大型项目的维护
Detailed References
详细参考
See specific files for:
- - Complete theme variable configuration
theme.md - - Syntax and examples of all directives
directives.md - - Advanced configuration and use cases
config.md
如需更多细节,请查看以下文件:
- - 完整的主题变量配置
theme.md - - 所有指令的语法及示例
directives.md - - 高级配置及使用案例
config.md