使用 CSS 变量或 Tailwind CSS 进行主题设置。
你可以选择使用 CSS 变量或 Tailwind CSS 工具类进行主题设置。
工具类
<div className="bg-zinc-950 dark:bg-white" />
要使用工具类进行主题设置,请在 components.json
文件中将 tailwind.cssVariables
设置为 false
。
components.json
{
"style": "default",
"rsc": true,
"tailwind": {
"config": "tailwind.config.js",
"css": "app/globals.css",
"baseColor": "slate",
"cssVariables": false
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils"
}
}
CSS 变量
<div className="bg-background text-foreground" />
要使用 CSS 变量进行主题设置,请在 components.json
文件中将 tailwind.cssVariables
设置为 true
。
components.json
{
"style": "default",
"rsc": true,
"tailwind": {
"config": "tailwind.config.js",
"css": "app/globals.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 className="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 /> 的背景色
--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%;
例如 、