乐闻世界logo
搜索文章和话题

How to create multiple themes using tailwind css

7 个月前提问
3 个月前修改
浏览次数81

6个答案

1
2
3
4
5
6

当您想要在 Tailwind CSS 中创建多个主题时,可以使用几种不同的方法,例如利用 Tailwind CSS 的官方插件或利用其内建的工具如变体(variants)和配置文件。以下是一个具体的步骤示例:

使用 Tailwind CSS 官方插件:@tailwindcss/custom-forms

  1. 安装插件: 首先,您需要安装这个插件。如果您还没有安装 Tailwind CSS,请先安装它。

    shell
    npm install tailwindcss @tailwindcss/custom-forms
  2. 在配置文件中引入插件: 在 tailwind.config.js 文件中引入这个插件。

    javascript
    // tailwind.config.js module.exports = { // ... plugins: [ // ... require('@tailwindcss/custom-forms'), ], };
  3. 配置多个主题: Tailwind CSS 使用类名前缀来创建多个主题,您可以在配置文件中自定义这些前缀。

    javascript
    // tailwind.config.js module.exports = { // ... darkMode: 'class', // or 'media' if you prefer to use media queries theme: { extend: { // Define the colors for the light theme colors: { theme1: { 'primary': '#...', // Define primary color 'secondary': '#...', // Define secondary color // ... more colors }, // Define the colors for the dark theme theme2: { 'primary': '#...', // Define primary color 'secondary': '#...', // Define secondary color // ... more colors }, }, }, }, variants: { // Define which utilities should be generated for your themes extend: { backgroundColor: ['theme1', 'theme2'], borderColor: ['theme1', 'theme2'], textColor: ['theme1', 'theme2'], // ... other utilities }, }, };
  4. 使用主题相关的类名: 在您的 HTML 或模板文件中,根据需要使用相应的类名来切换主题。

    html
    <!-- 使用主题1 --> <button class="bg-theme1-primary text-white"> Theme 1 Button </button> <!-- 使用主题2 --> <button class="bg-theme2-primary text-white"> Theme 2 Button </button>

利用 CSS 变量和 JavaScript 控制主题

另一种方式是使用 CSS 变量来定义颜色,然后使用 JavaScript 来切换这些变量的值。

  1. 定义 CSS 变量: 在您的 CSS 文件中,您可以这样定义主题颜色:

    css
    :root { --primary-color: #...; /* 默认主题颜色 */ --secondary-color: #...; /* ... */ } .theme1 { --primary-color: #...; /* 主题1颜色 */ --secondary-color: #...; /* ... */ } .theme2 { --primary-color: #...; /* 主题2颜色 */ --secondary-color: #...; /* ... */ }
  2. 在 HTML 中使用 CSS 变量

    html
    <button style="background-color: var(--primary-color); color: white;"> Theme Button </button>
  3. 使用 JavaScript 切换主题: 您可以根据用户的选择或某些条件来使用 JavaScript 切换主题。

    javascript
    // 切换到主题1 document.documentElement.classList.add('theme1'); document.documentElement.classList.remove('theme2'); // 切换到主题2 document.documentElement.classList.add('theme2'); document.documentElement.classList.remove('theme1');

使用上述方法,您可以根据需要创建并切换不同的主题。这可以通过类名控制,CSS 变量,甚至可以用 JavaScript 动态切换,以满足更复杂的应用场景。除了使用官方插件和 CSS 变量以外,还有一种方法是直接在 Tailwind CSS 的配置文件中使用 JavaScript 来动态生成主题。这种方法通常涉及条件逻辑和自定义函数,使得配置更灵活。

使用 JavaScript 动态生成主题

  1. 配置多个主题: 在 Tailwind 的配置文件中,您可以使用 JavaScript 来根据不同的条件动态生成主题配置。

    javascript
    // tailwind.config.js function generateTheme(themeName) { return { 'primary': themeName === 'theme1' ? '#...' : '#...', 'secondary': themeName === 'theme1' ? '#...' : '#...', // ... }; } module.exports = { theme: { extend: { colors: { theme1: generateTheme('theme1'), theme2: generateTheme('theme2'), }, // ... }, }, // ... };
  2. 应用主题相关的类名: 在您的 HTML 或模板文件中,根据需要使用相应的类名来应用不同的主题。

    html
    <!-- 使用主题1 --> <div class="theme1:primary">This text uses the primary color of theme 1</div> <!-- 使用主题2 --> <div class="theme2:primary">This text uses the primary color of theme 2</div>

使用插件创建基于条件的主题

Tailwind CSS 允许开发者编写自定义插件,这意味着您可以创建一个插件来根据特定条件或环境变量来生成主题。

  1. 编写自定义插件: 创建一个新的 JavaScript 文件来编写您的插件逻辑。

    javascript
    // plugins/themePlugin.js module.exports = function({ addBase, config }) { const theme = config('themeName'); addBase({ h1: { color: config(`theme.${theme}.primary`) }, // ...其他基础样式 }); };
  2. 在配置文件中引入自定义插件: 修改您的 tailwind.config.js 文件以包含您的插件,并传递所需的配置。

    javascript
    // tailwind.config.js const themePlugin = require('./plugins/themePlugin'); module.exports = { themeName: 'theme1', // 或者 'theme2', 可能来自环境变量或其他逻辑 // ... plugins: [ themePlugin, // ...其他插件 ], // ... };

结合使用 Tailwind CSS 和其它 CSS 方法

在实际项目中,Tailwind CSS 可以与其他 CSS 方法(如 CSS-in-JS 或者传统的样式表)结合使用。例如,您可以使用 Tailwind 的实用类来处理大部分样式,同时使用组件级别的样式表来管理特定的主题样式。

在以上所有情况中,关键是理解 Tailwind 的配置方式和类生成机制。您可以根据项目需求灵活搭配不同的方法,创建符合设计需求的多主题界面。

2024年6月29日 12:07 回复

我确实找到了使用 CSS 变量的解决方案。

在 CSS 文件中,您可以为每个主题定义样式,如下所示

shell
@tailwind base; @tailwind components; @tailwind utilities; @layer base { html[data-theme='default'] { --color-esther: 34, 39, 46; --color-maximus: 45, 51, 59; --color-linx: 55, 62, 71; } html[data-theme='neon'] { --color-esther: 20, 61, 42; --color-maximus: 13, 82, 66; --color-linx: 20, 82, 11; } }

然后在你的tailwind.config.js文件中你可以这样定义它们

顺风版本^3.1

shell
module.exports = { theme: { colors: { esther: 'rgb(var(--color-esther) / <alpha-value>)', maximus: 'rgb(var(--color-maximus) / <alpha-value>)', linx: 'rgb(var(--color-linx) / <alpha-value>)', }, }, }

Tailwind 低于 3.1 版本

shell
function withOpacity(cssVariable) { return ({ opacityValue }) => { return opacityValue ? `rgba(var(${cssVariable}), ${opacityValue})` : `rgb(var(${cssVariable}))` } } module.exports = { theme: { colors: { esther: withOpacity('--color-esther'), maximus: withOpacity('--color-maximus'), linx: withOpacity('--color-linx'), }, }, }

在 HTML 中,您可以像这样使用这些类:

shell
<html lang="en" data-theme="default"> <body class="bg-esther text-optimus cursor-default"></body> </html>
2024年6月29日 12:07 回复

tw-colors正是这样做的。

tailwind.config.js

shell
const { createThemes } = require('tw-colors'); module.exports = { content: ['./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}'], plugins: [ createThemes({ halloween: { 'primary': 'orange', 'secondary': 'yellow', }, summer: { 'primary': 'pink', 'secondary': 'red', }, winter: { 'primary': 'blue', 'secondary': 'green', }, party: { 'primary': 'steelblue', 'secondary': 'darkblue', }, }) ], };

在类中使用这样的主题:

shell
<html class='theme-halloween'> ... </html>

或者使用数据属性:

shell
<html data-theme='halloween'> ... </html>

然后像平常一样使用顺风课程。具有该类的按钮bg-primary将根据最接近的主题改变颜色。

可以使用一些切换按钮或您喜欢的任何内容动态切换主题

2024年6月29日 12:07 回复

有几个插件支持定义多个 Tailwind 主题并在它们之间进行切换。我特别喜欢tailwindcss-themer 插件,因为它允许您:

  • 只需在任何组件(通常是顶级组件)上添加带有主题名称的 css 类即可切换主题
  • 像平常一样使用 CSS 类(例如text-primary)。这使代码保持干净并且独立于插件(与其他插件相反,其他插件有时需要每个 CSS 类都有特定的前缀)
  • 使用变体进一步微调样式,例如my-theme:font-bold
2024年6月29日 12:07 回复

只是偶然发现了这个问题,因为我正在做与几年前相同的事情,但我不记得了。如果有人再次来到这里,也许这个答案可能会有所帮助。

在我们的例子中,我们正在构建一个应用于 3 个不同网站的主题,但明显改变了颜色和字体,这似乎与问题作者的情况相同。

为此,可以使用顺风预设。我有一个tailwind.preset.js基本上是默认的顺风配置,具有所有基色、间距等。对于每个主题,tailwind.<theme-name>.js都会设置一个单独的主题,其中包含更改并基于预设。

例子tailwind.theme-one.js

shell
module.exports = { presets: [ require('./tailwind.preset.js') ], theme: { colors: { # color changes go here } } }

我们设置了一个 gulp 工作流程,基本上只为每个主题配置渲染一次主 scss 文件。在集成上,随后将加载所需的主题文件。

2024年6月29日 12:07 回复

当您想要在 Tailwind CSS 中创建多个主题时,可以使用几种不同的方法,例如利用 Tailwind CSS 的官方插件或利用其内建的工具如变体(variants)和配置文件。以下是一个具体的步骤示例:

使用 Tailwind CSS 官方插件:@tailwindcss/custom-forms

  1. 安装插件: 首先,您需要安装这个插件。如果您还没有安装 Tailwind CSS,请先安装它。

    shell
    npm install tailwindcss @tailwindcss/custom-forms
  2. 在配置文件中引入插件: 在 tailwind.config.js 文件中引入这个插件。

    javascript
    // tailwind.config.js module.exports = { // ... plugins: [ // ... require('@tailwindcss/custom-forms'), ], };
  3. 配置多个主题: Tailwind CSS 使用类名前缀来创建多个主题,您可以在配置文件中自定义这些前缀。

    javascript
    // tailwind.config.js module.exports = { // ... darkMode: 'class', // or 'media' if you prefer to use media queries theme: { extend: { // Define the colors for the light theme colors: { theme1: { 'primary': '#...', // Define primary color 'secondary': '#...', // Define secondary color // ... more colors }, // Define the colors for the dark theme theme2: { 'primary': '#...', // Define primary color 'secondary': '#...', // Define secondary color // ... more colors }, }, }, }, variants: { // Define which utilities should be generated for your themes extend: { backgroundColor: ['theme1', 'theme2'], borderColor: ['theme1', 'theme2'], textColor: ['theme1', 'theme2'], // ... other utilities }, }, };
  4. 使用主题相关的类名: 在您的 HTML 或模板文件中,根据需要使用相应的类名来切换主题。

    html
    <!-- 使用主题1 --> <button class="bg-theme1-primary text-white"> Theme 1 Button </button> <!-- 使用主题2 --> <button class="bg-theme2-primary text-white"> Theme 2 Button </button>

利用 CSS 变量和 JavaScript 控制主题

另一种方式是使用 CSS 变量来定义颜色,然后使用 JavaScript 来切换这些变量的值。

  1. 定义 CSS 变量: 在您的 CSS 文件中,您可以这样定义主题颜色:

    css
    :root { --primary-color: #...; /* 默认主题颜色 */ --secondary-color: #...; /* ... */ } .theme1 { --primary-color: #...; /* 主题1颜色 */ --secondary-color: #...; /* ... */ } .theme2 { --primary-color: #...; /* 主题2颜色 */ --secondary-color: #...; /* ... */ }
  2. 在 HTML 中使用 CSS 变量

    html
    <button style="background-color: var(--primary-color); color: white;"> Theme Button </button>
  3. 使用 JavaScript 切换主题: 您可以根据用户的选择或某些条件来使用 JavaScript 切换主题。

    javascript
    // 切换到主题1 document.documentElement.classList.add('theme1'); document.documentElement.classList.remove('theme2'); // 切换到主题2 document.documentElement.classList.add('theme2'); document.documentElement.classList.remove('theme1');

使用上述方法,您可以根据需要创建并切换不同的主题。这可以通过类名控制,CSS 变量,甚至可以用 JavaScript 动态切换,以满足更复杂的应用场景。 除了使用官方插件和 CSS 变量以外,还有一种方法是直接在 Tailwind CSS 的配置文件中使用 JavaScript 来动态生成主题。这种方法通常涉及条件逻辑和自定义函数,使得配置更灵活。

使用 JavaScript 动态生成主题

  1. 配置多个主题: 在 Tailwind 的配置文件中,您可以使用 JavaScript 来根据不同的条件动态生成主题配置。

    javascript
    // tailwind.config.js function generateTheme(themeName) { return { 'primary': themeName === 'theme1' ? '#...' : '#...', 'secondary': themeName === 'theme1' ? '#...' : '#...', // ... }; } module.exports = { theme: { extend: { colors: { theme1: generateTheme('theme1'), theme2: generateTheme('theme2'), }, // ... }, }, // ... };
  2. 应用主题相关的类名: 在您的 HTML 或模板文件中,根据需要使用相应的类名来应用不同的主题。

    html
    <!-- 使用主题1 --> <div class="theme1:primary">This text uses the primary color of theme 1</div> <!-- 使用主题2 --> <div class="theme2:primary">This text uses the primary color of theme 2</div>

使用插件创建基于条件的主题

Tailwind CSS 允许开发者编写自定义插件,这意味着您可以创建一个插件来根据特定条件或环境变量来生成主题。

  1. 编写自定义插件: 创建一个新的 JavaScript 文件来编写您的插件逻辑。

    javascript
    // plugins/themePlugin.js module.exports = function({ addBase, config }) { const theme = config('themeName'); addBase({ h1: { color: config(`theme.${theme}.primary`) }, // ...其他基础样式 }); };
  2. 在配置文件中引入自定义插件: 修改您的 tailwind.config.js 文件以包含您的插件,并传递所需的配置。

    javascript
    // tailwind.config.js const themePlugin = require('./plugins/themePlugin'); module.exports = { themeName: 'theme1', // 或者 'theme2', 可能来自环境变量或其他逻辑 // ... plugins: [ themePlugin, // ...其他插件 ], // ... };

结合使用 Tailwind CSS 和其它 CSS 方法

在实际项目中,Tailwind CSS 可以与其他 CSS 方法(如 CSS-in-JS 或者传统的样式表)结合使用。例如,您可以使用 Tailwind 的实用类来处理大部分样式,同时使用组件级别的样式表来管理特定的主题样式。

在以上所有情况中,关键是理解 Tailwind 的配置方式和类生成机制。您可以根据项目需求灵活搭配不同的方法,创建符合设计需求的多主题界面。

2024年6月29日 12:07 回复

你的答案