文档
主题
主题
使用 CSS 变量或您选择的 CSS 框架进行主题设置。
您可以选择使用 CSS 变量或您选择的 CSS 框架的实用类进行主题设置。
实用类
<div class="bg-zinc-950 dark:bg-white" /> 要使用实用类进行主题设置,请将 tailwind.cssVariables 在您的 components.json 文件中设置为 false。
{
"style": "default",
"tailwind": {
"config": "tailwind.config.js",
"css": "app/app.css",
"baseColor": "slate",
"cssVariables": false
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils"
}
} 或使用 uno。
{
"style": "default",
"uno": {
"config": "uno.config.ts",
"css": "app/app.css",
"baseColor": "slate",
"cssVariables": false
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils"
}
} CSS 变量
<div class="bg-background text-foreground" /> 要使用 CSS 变量进行主题设置,请将 tailwind.cssVariables 在您的 components.json 文件中设置为 true。
{
"style": "default",
"tailwind": {
"config": "tailwind.config.js",
"css": "app/globals.css",
"baseColor": "slate",
"cssVariables": true
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils"
}
} 或使用 uno。
{
"style": "default",
"uno": {
"config": "uno.config.ts",
"css": "app/app.css",
"baseColor": "slate",
"cssVariables": true
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils"
}
} 约定
我们对颜色使用简单的 background 和 foreground 约定。background 变量用于组件的背景颜色,foreground 变量用于文本颜色。
当变量用于组件的背景颜色时,background 后缀将被省略。
给定以下 CSS 变量
--primary: 222.2 47.4% 11.2%;
--primary-foreground: 210 40% 98%; 以下组件的 background 颜色将是 hsl(var(--primary)),foreground 颜色将是 hsl(var(--primary-foreground))。
<div class="bg-primary text-primary-foreground">Hello</div> CSS 变量必须在没有颜色空间函数的情况下定义。有关更多信息,请参阅 Tailwind CSS 文档。
变量列表
以下是可用于自定义的变量列表
--background: 0 0% 100%;
--foreground: 222.2 47.4% 11.2%; --muted: 210 40% 96.1%;
--muted-foreground: 215.4 16.3% 46.9%; --card: 0 0% 100%;
--card-foreground: 222.2 47.4% 11.2%; --popover: 0 0% 100%;
--popover-foreground: 222.2 47.4% 11.2%; --border: 214.3 31.8% 91.4%; --input: 214.3 31.8% 91.4%; --primary: 222.2 47.4% 11.2%;
--primary-foreground: 210 40% 98%; --secondary: 210 40% 96.1%;
--secondary-foreground: 222.2 47.4% 11.2%; --accent: 210 40% 96.1%;
--accent-foreground: 222.2 47.4% 11.2%; --destructive: 0 100% 50%;
--destructive-foreground: 210 40% 98%; --ring: 215 20.2% 65.1%; --radius: 0.5rem; 添加新颜色
要添加新颜色,您需要将它们添加到您的 CSS 文件和您的 tailwind.config.js 文件中。
:root {
--warning: 38 92% 50%;
--warning-foreground: 48 96% 89%;
}
.dark {
--warning: 48 96% 89%;
--warning-foreground: 38 92% 50%;
} module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))"
}
}
}
}; 或使用 uno。
export default defineConfig({
theme: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))"
}
}
}); 您现在可以在您的组件中使用 warning 实用程序类。
<div class="bg-warning text-warning-foreground" /> 其他颜色格式
我建议使用 HSL 颜色进行主题设置,但如果您喜欢,也可以使用其他颜色格式。
有关使用 rgb、rgba 或 hsl 颜色的更多信息,请参阅 Tailwind CSS 文档。