2021-01-02
1583
#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:

React Libraries For Building Forms And Surveys

React libraries for building forms and surveys

Consider using a React form library to mitigate the challenges of building and managing forms and surveys.

Hussain Arif
Dec 11, 2024 â‹… 7 min read
Hoppscotch Vs Postman: A Guide To API Testing

Hoppscotch vs. Postman: a guide to open source API testing

In this article, you’ll learn how to set up Hoppscotch and which APIs to test it with. Then we’ll discuss alternatives: OpenAPI DevTools and Postman.

Chigozie Oduah
Dec 10, 2024 â‹… 5 min read
React Native logo over red background.

Implementing camera functionality in React Native

Learn to migrate from react-native-camera to VisionCamera, manage permissions, optimize performance, and implement advanced features.

Chimezie Innocent
Dec 9, 2024 â‹… 13 min read
Solid Principles For Javascript

SOLID principles for JavaScript

SOLID principles help us keep code flexible. In this article, we’ll examine all of those principles and their implementation using JavaScript.

Frank Joseph
Dec 5, 2024 â‹… 10 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