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:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 â‹… 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 â‹… 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 â‹… 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 2024 â‹… 5 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