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

Tony Zheng

I'm trying to create multiple themes by following this: 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.

kingisaac95

Are you using 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

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

Lastly, you'll need to add some scripts to compile the 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is my css not applying styling to React component?

Tailwind CSS custom color applying to text but not background in ReactJS

Tailwind CSS not loading custom classes on React app

Tailwind CSS custom gradient with React state and FastAverageColor

Tailwind CSS not applying using CLI

Tailwind css background gradient not applying

Tailwind CSS not applying certain styles

Why is my JS function only applying CSS to a single instance of my React component?

Which has a higher priority, custom utility classes or atomic classes in Tailwind CSS?

my tailwind utility class does not work in production

Why is my jQuery .css() code not applying styles?

Why is my CSS animation not applying to the correct element?

Why my element fails to be centered only in small resolution in Tailwind css?

Why isn't the "disabled:" tailwind prefix not working in my react app?

Navbar in React and Tailwind CSS

Custom font not applying React Native Elements Button component

Why is my CSS style sheet not applying to my HTML document?

Why isn't my css applying to my html page

IE 9 is not respecting my overlay and applying css to contained elements only

Positioning of HTML elements in Tailwind css

Custom group "states" in tailwind css

Why did this CSS behave differently after applying it to my DOM?

Why is my javascript applying css grid properties quickly disappearing?

Why my css style via javascript are not applying on desktop screen size?

Why are Handsontable CSS files not applying to my Angular Frontend?

Tailwind CSS convert more of these inline styles to utility classes

Why Footer is floating in tailwind css

react css class not applying

React not applying css styles