【问题标题】:Tailwind CSS: how to use color variables in tailwind.config.jsTailwind CSS:如何在 tailwind.config.js 中使用颜色变量
【发布时间】:2022-01-27 08:35:28
【问题描述】:

我想将颜色变量(默认或扩展)用于我的扩展主题,例如:

module.exports = {
    content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
    theme: {
        extend: {
            colors: {
                lime: {
                    '100': 'green'
                }
            },
            backgroundColor: {
                skin: {
                    base: 'bg-red-500',
                    secondary: 'lime-100',
                },
            },
        },
    },
    plugins: [],
};

但这不起作用。我怎样才能有一个类bg-skin-base 相当于bg-red-500 和另一个类bg-skin-secondary 相当于bg-[green]

【问题讨论】:

    标签: tailwind-css


    【解决方案1】:

    没有背景颜色。在extend > 颜色下添加的任何颜色都可用于text-bg-border- 等。

    只需在颜色下添加皮肤就可以了。

    tailwind docs reference

    const colors = require("tailwindcss/colors")
    
    extend: {
       colors: {
          lime: {
            '100': 'green'
          },
          skin: {
            base: '#yourhex",
            secondary: '#yourhex",
            third: colors.violet["500"]
          }
       },
    }
    

    您不能像在示例中那样使用 bg-red-500 作为基础。如果您想为顺风颜色起别名,您可以使用十六进制或const colors = require("tailwindcss/colors") 使用顺风颜色,然后您可以使用colors.red[500] 或任何您想要的颜色。

    【讨论】:

    • Just add skin under colors and you should be good. 是什么意思?能否提供代码示例?
    • @abc 添加到答案中
    • 我不想使用自己的十六进制颜色,我想在bg-red-500 中使用红色阴影。 base: 'var(--red-500)' 是我想做的,但是不行。
    • 你必须要求顺风颜色,然后你可以引用任何你想要的顺风颜色(skin.third)就是上面的例子。
    猜你喜欢
    • 2021-11-23
    • 2021-09-02
    • 2021-02-28
    • 2023-02-04
    • 1970-01-01
    • 2021-08-30
    • 2021-03-11
    • 2021-10-19
    • 2022-11-13
    相关资源
    最近更新 更多