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:

Vue logo over a brown background.

A guide to two-way binding in Vue

Learn how to implement one-way and two-way data binding in Vue.js, using v-model and advanced techniques like defineModel for better apps.

David Omotayo
Nov 22, 2024 â‹… 10 min read
TypeScript logo over a pink and white background.

Drizzle vs. Prisma: Which ORM is best for your project?

Compare Prisma and Drizzle ORMs to learn their differences, strengths, and weaknesses for data access and migrations.

Temitope Oyedele
Nov 21, 2024 â‹… 10 min read
Practical Implementation Of The Rule Of Least Power For Developers

Practical implementation of the Rule of Least Power for developers

It’s easy for devs to default to JavaScript to fix every problem. Let’s use the RoLP to find simpler alternatives with HTML and CSS.

Timonwa Akintokun
Nov 21, 2024 â‹… 8 min read
Rust logo over black marble background.

Handling memory leaks in Rust

Learn how to manage memory leaks in Rust, avoid unsafe behavior, and use tools like weak references to ensure efficient programs.

Ukeje Goodness
Nov 20, 2024 â‹… 4 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