如何使用 daisyUI 主题?
daisyUI 带有 35 个内置主题,可以立即改变您网站的整体外观 - 这是一个节省时间的工具,让您可以专注于构建,而不是决定颜色。
您还可以创建自己的自定义主题或自定义内置主题。
您可以通过在@plugin "daisyui"
CSS 文件中添加括号来管理主题。
@import "tailwindcss";
- @plugin "daisyui";
+ @plugin "daisyui" {
+ themes: light --default, dark --prefersdark;
+ }
themes
是您要启用的主题名称的逗号分隔列表。--default
标志,使主题成为默认主题。--prefersdark
标志,使主题成为暗黑模式的默认主题 (prefers-color-scheme: dark)。默认情况下,light
和dark
主题已启用。让我们启用cupcake
主题
@import "tailwindcss";
@plugin "daisyui" {
themes: light --default, dark --prefersdark, cupcake;
}
并为页面设置cupcake
主题
<html data-theme="cupcake"></html>
我建议使用 theme-change,这样您就可以切换主题并将选定的主题保存在本地存储中。
通过将themes
设置为all
:
@import "tailwindcss";
@plugin "daisyui" {
themes: all;
}
要禁用dark
主题,例如,将其从列表中删除。现在只包含 light 主题
@import "tailwindcss";
@plugin "daisyui" {
- themes: light --default, dark --prefersdark;
+ themes: light --default;
}
如果出于某种原因,您想禁用所有主题并删除所有 daisyUI 颜色,请设置themes
设置为false
:
@import "tailwindcss";
@plugin "daisyui" {
themes: false;
}
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>
要添加新主题,请使用@plugin "daisyui/theme" {}
在您的 CSS 文件中,使用以下结构
@import "tailwindcss";
@plugin "daisyui";
@plugin "daisyui/theme" {
name: "mytheme";
default: true; /* set as default */
prefersdark: false; /* set as default dark mode (prefers-color-scheme:dark) */
color-scheme: light; /* color of browser-provided UI */
--color-base-100: oklch(98% 0.02 240);
--color-base-200: oklch(95% 0.03 240);
--color-base-300: oklch(92% 0.04 240);
--color-base-content: oklch(20% 0.05 240);
--color-primary: oklch(55% 0.3 240);
--color-primary-content: oklch(98% 0.01 240);
--color-secondary: oklch(70% 0.25 200);
--color-secondary-content: oklch(98% 0.01 200);
--color-accent: oklch(65% 0.25 160);
--color-accent-content: oklch(98% 0.01 160);
--color-neutral: oklch(50% 0.05 240);
--color-neutral-content: oklch(98% 0.01 240);
--color-info: oklch(70% 0.2 220);
--color-info-content: oklch(98% 0.01 220);
--color-success: oklch(65% 0.25 140);
--color-success-content: oklch(98% 0.01 140);
--color-warning: oklch(80% 0.25 80);
--color-warning-content: oklch(20% 0.05 80);
--color-error: oklch(65% 0.3 30);
--color-error-content: oklch(98% 0.01 30);
/* border radius */
--radius-selector: 1rem;
--radius-field: 0.25rem;
--radius-box: 0.5rem;
/* base sizes */
--size-selector: 0.25rem;
--size-field: 0.25rem;
/* border size */
--border: 1px;
/* effects */
--depth: 1;
--noise: 0;
}
要自定义内置主题,您可以使用与添加新主题相同的结构,但使用与内置主题相同的名称。例如,要自定义light
主题
@import "tailwindcss";
@plugin "daisyui";
@plugin "daisyui/theme" {
name: "light";
default: true;
--color-primary: blue;
--color-secondary: teal;
}
所有其他值将从原始主题继承。
[data-theme="light"] {
.my-btn {
background-color: #1EA1F1;
border-color: #1EA1F1;
}
.my-btn:hover {
background-color: #1C96E1;
border-color: #1C96E1;
}
}
@import "tailwindcss";
@plugin "daisyui" {
themes: winter --default, night --prefersdark;
}
@custom-variant dark (&:where([data-theme=night], [data-theme=night] *));
<div class="p-10 dark:p-20">
I will have 10 padding on winter theme and 20 padding on night theme
</div>