2021-01-02
1602
#create react app#react
Anjolaoluwa Adebayo-Oyetoro
4063
Jan 2, 2021 â‹… 5 min read

How to use Tailwind CSS in React to configure Create React App

Anjolaoluwa Adebayo-Oyetoro Maker. Writes sometimes. Playful most times. Loves beautiful UIs.

Recent posts:

Nx Adoption Guide: Overview, Examples, And Alternatives

Nx adoption guide: Overview, examples, and alternatives

Let’s explore Nx features, use cases, alternatives, and more to help you assess whether it’s the right tool for your needs.

Andrew Evans
Mar 28, 2024 â‹… 9 min read
Understanding Security In React Native Applications

Understanding security in React Native applications

Explore the various security threats facing React Native mobile applications and how to mitigate them.

Wisdom Ekpotu
Mar 27, 2024 â‹… 10 min read
Warp Adoption Guide: Overview, Examples, And Alternatives

warp adoption guide: Overview, examples, and alternatives

The warp web framework for Rust offers many enticing features. Let’s see when and why you should consider using warp in your projects.

Ukeje Goodness
Mar 26, 2024 â‹… 8 min read
Integrating Next Js And Signalr For Enhanced Real Time Web App Capabilities

Integrating Next.js and SignalR to build real-time web apps

In this tutorial, you’ll learn how to integrate Next.js and SignalR to build an enhanced real-time web application.

Clara Ekekenta
Mar 25, 2024 â‹… 8 min read
View all posts

21 Replies to "How to use Tailwind CSS in React to configure Create React App"

  1. Hey btw for this “`”scripts”: {
    “build:style”:”tailwind build src/styles/index.css -o src/styles/tailwind.css”,
    “start”: “npm run build:style && react-scripts start”,
    “build”: “react-scripts build”,
    “test”: “react-scripts test”,
    “eject”: “react-scripts eject”
    },“` You still have npm instead of using yarn.

  2. Hi!
    Thanks for the tutorial.
    One thing that might be nice to add is if you want to generate styles based on the taildwind config file, the build:style should be changed to:

    “build:style”: “tailwind build src/styles/index.css -c tailwind.js -o src/styles/tailwind.css”,

  3. Thanks for the tip! This helped a lot.

    I was scratching my head because `tailwindcss(‘./tailwind.js’)` in `postcss.config.js` looked like it should work. But that doesn’t work. Maybe they updated the API?

    The file should look like this:
    “`
    // postcss.config.js

    module.exports = {
    plugins: [require(“tailwindcss”), require(“autoprefixer”)]
    };
    “`

    Also, making `@import` didn’t get me anywhere. I thought maybe I should go that route, but if you’re having trouble getting the tutorial to work I’d updated the build style and update `postcss.config.js` to what I have above first.

  4. Sorry if I’m wrong, isn’t the script “build:style” should be using “postcss” instead of “tailwind”?

  5. You have to add it to your tailwind config. If you want to do the styling for a background add it like below. Not the extra key ‘group’ added to the variants.

    module.exports = {
    // …
    variants: {
    backgroundColor: [‘responsive’, ‘hover’, ‘focus’, ‘group’],
    },
    }

  6. Followed all the steps, then yarn start.
    and I am stuck with this in terminal:

    $ yarn start
    yarn run v1.19.2
    $ npm run watch:css && react-scripts start

    > [email protected] watch:css D:\React\ecommerce\crm\crm
    > postcss src/styles/tailwind.css -o src/styles/app.css -w

    //My app.css has been populated but the app is not starting.
    //Compiled succesfully message is not showing.

  7. Will this work after eject? How do you load the new UI components? …
    Just asking. I honestly got no idea – which is why I prefer things being implemented in a more controlled way.

  8. I think you don’t have to eject the script instead use react-app-rewired more info available here https://github.com/timarney/react-app-rewired

    I have configured tailwind using react-app-rewired

    this is my configuration :

    const tailwindcss = require(‘tailwindcss’);
    module.exports = config => {
    require(‘react-app-rewire-postcss’)(config, {
    plugins: loader => [
    tailwindcss(‘./tailwind.js’),
    require(‘autoprefixer’)
    ]
    });
    return config;
    };

  9. This way your css won’t update when you run it with npm start as it is compiled only once.
    Use npm-run-all (npm install –save-dev npm-run-all) and change your scripts in package.json to run both css watcher and react project paralell (the “run-p” means run paralell).
    “build:tailwind”: “postcss src/tailwind.css -o src/tailwind.generated.css”,
    “watch:tailwind”: “postcss -w src/tailwind.css -o src/tailwind.generated.css”,

    “start”: “run-p watch:tailwind start:react”,
    “start:react”: “react-scripts start”,

  10. Thanks OP! Wonderful article. The code snippet in the end, `sectiom` has a typo. Should be `section`.
    Also, since @tailwind base; @tailwind components; @tailwind utilities; are being imported on the index.css file, index.js must import index.css instead of tailwind.css for the example to work. Thank you!

  11. If ever I needed something to remind me how ludicrously complex React/frond-end development has become this has to be it. So many fragile pieces tied together with duct tape. I need to get back to being productive with Rails or Golang or something. Even Java is better than this.

Leave a Reply