如何使用 daisyUI 的主题?
daisyUI 原生已经有很多精美制作的主题,你可以直接获取它们。 每一个主题都定义了一些全局的变量,这些变量会影响所有的 daisyUI 组件。 要使用一个主题,只需要在 tailwind.config.js 里添加主题名字,然后在 HTML 标签里添加 data-theme 属性即可:module.exports = {
//...
daisyui: {
themes: ["light", "dark", "cupcake"],
},
} <html data-theme="cupcake"></html> 我推荐使用 theme-change,这样你可以方便的切换主题,并且主题的选择会存储在本地存储里。 module.exports = {
//...
daisyui: {
themes: [
"light",
"dark",
"cupcake",
"bumblebee",
"emerald",
"corporate",
"synthwave",
"retro",
"cyberpunk",
"valentine",
"halloween",
"garden",
"forest",
"aqua",
"lofi",
"pastel",
"fantasy",
"wireframe",
"black",
"luxury",
"dracula",
"cmyk",
"autumn",
"business",
"acid",
"lemonade",
"night",
"coffee",
"winter",
"dim",
"nord",
"sunset",
],
},
} 默认主题是 light(或者如果是黑暗模式则为 dark ) 你可以通过配置 tailwind.config.js 来改变默认的主题选择 cupcake 会成为 light 模式的默认主题dark 会成为 dark 模式的默认主题data-theme='cmyk' 时,主题会切换为 cmyk module.exports = {
daisyui: {
themes: ["cupcake", "dark", "cmyk"],
},
} themes 配置为 false。 module.exports = {
//...
daisyui: {
themes: false,
},
} 如果你不想引入任何主题,并且禁止所有有语义的颜色名字, 设置 themes 配置为一个空数组。 module.exports = {
//...
daisyui: {
themes: [],
},
} data-theme='THEME_NAME' 即可以让这个标签下面使用对应的主题。 你可以嵌套使用主题,可能是无尽的,尝试吧! 你可以强制让你的 HTML 去使用一个特定的主题。 <html data-theme="dark">
<div data-theme="light">
This div will always use light theme
<span data-theme="retro">This span will always use retro theme!</span>
</div>
</html> tailwind.config.js 文件里添加一个新主题。 在下面这个例子里,我添加了一个新主题叫 mytheme 并且我也引入了 dark 和 cupcake 主题。 mytheme) 会成为默认主题。dark 主题会成为默认的黑暗模式主题。primary 按钮的文字颜色)。 你也可以添加 可选颜色名字 来完全控制所有的颜色。
module.exports = {
//...
daisyui: {
themes: [
{
mytheme: {
"primary": "#a991f7",
"secondary": "#f6d860",
"accent": "#37cdbe",
"neutral": "#3d4451",
"base-100": "#ffffff",
},
},
"dark",
"cupcake",
],
},
} module.exports = {
//...
daisyui: {
themes: [
{
mytheme: {
"primary": "#a991f7",
"secondary": "#f6d860",
"accent": "#37cdbe",
"neutral": "#3d4451",
"base-100": "#ffffff",
"--rounded-box": "1rem", // border radius rounded-box utility class, used in card and other large boxes
"--rounded-btn": "0.5rem", // border radius rounded-btn utility class, used in buttons and similar element
"--rounded-badge": "1.9rem", // border radius rounded-badge utility class, used in badges and similar
"--animation-btn": "0.25s", // duration of animation when you click on button
"--animation-input": "0.2s", // duration of animation for inputs like checkbox, toggle, radio, etc
"--btn-focus-scale": "0.95", // scale transform of button when you focus on it
"--border-btn": "1px", // border width of buttons
"--tab-border": "1px", // border width of tabs
"--tab-radius": "0.5rem", // border radius of tabs
},
},
],
},
} [data-theme="mytheme"] .btn {
border-width: 2px;
border-color: black;
} light 主题,并且改变了这个主题的 primary 和 secondary 的颜色: module.exports = {
//...
daisyui: {
themes: [
{
light: {
...require("daisyui/src/theming/themes")["light"],
primary: "blue",
secondary: "teal",
},
},
],
},
} .btn-twitter 类只会在 light 主题中才会有这些样式 module.exports = {
//...
daisyui: {
themes: [
{
light: {
...require("daisyui/src/theming/themes")["light"],
".btn-twitter": {
"background-color": "#1EA1F1",
"border-color": "#1EA1F1",
},
".btn-twitter:hover": {
"background-color": "#1C96E1",
"border-color": "#1C96E1",
},
},
},
],
},
} 