简体   繁体   English

为什么我的自定义 Tailwind CSS 实用程序不适用于 React 元素?

[英]Why is my custom Tailwind CSS utility not applying to React elements?

I'm trying to create multiple themes by following this: https://github.com/adamwathan/theming-tailwind-demo我正在尝试通过以下方式创建多个主题: https://github.com/adamwathan/theming-tailwind-demo

My code:我的代码:

// tailwind.config.js
module.exports = {
  purge: [],
  theme: {
    backgroundColor: {
      primary: "var(--color-bg-primary)",
    },
    textColor: {
      primary: "var(--color-text-primary)",
    },
    extend: {},
  },
  variants: {},
  plugins: [],
};

// tailwind.css
@tailwind base;

@tailwind components;

@tailwind utilities;

.theme-TCD {
  --color-bg-primary: #411218;

  --color-text-primary: #e8982e;
}
// My HTML
<div className="theme-TCD">
  <div className="bg-primary">
    <Banner />
  </div>
</div>

I have tried "color-bg-primary" and "backgroundColor-primary" in place of "bg-primary" but that does not work.我尝试用“color-bg-primary”和“backgroundColor-primary”代替“bg-primary”,但这不起作用。

Are you using CRA(Create React App)?你在使用 CRA(Create React App)吗? If so, you'll have to do some setup.如果是这样,您将必须进行一些设置。

First, you'll need to install some dependencies首先,您需要安装一些依赖项

yarn add -D tailwindcss autoprefixer postcss-cli

or要么

npm install --save-dev tailwindcss autoprefixer postcss-cli

After you'll need to create a postcss.config.js在你需要创建一个postcss.config.js

module.exports = {
  plugins: [
    require('tailwindcss'),
    require('autoprefixer'),
  ]
}

Lastly, you'll need to add some scripts to compile the styles最后,您需要添加一些脚本来编译 styles

{
  //...
  "scripts": {
    //... place these after the four scripts created by CRA
    "build:styles": "postcss src/tailwind.css -o src/styles.css", 
    "prebuild": "yarn build:styles",
    "prestart": "yarn build:styles"
  }
}

or要么

{
  //...
  "scripts": {
    //... place these after the four scripts created by CRA
    "build:styles": "postcss src/tailwind.css -o src/styles.css",
    "prebuild": "npm run build:styles",
    "prestart": "npm run build:styles"
  }
}

Source: Setting up Tailwind With create-react-app资料来源: 使用 create-react-app 设置 Tailwind

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM