DEV Community

Kauna Hassan
Kauna Hassan

Posted on

117 2 2 4 2

Using Tailwind CSS with React.js: A Concise Guide

Tailwind CSS is a utility-first CSS framework that enables developers to quickly build modern and responsive user interfaces. When combined with React.js, a popular JavaScript library for building user interfaces, the two technologies synergize to streamline the development process. In this article, we'll explore how to integrate Tailwind CSS seamlessly into a React.js project.

Prerequisites

Before diving into the integration process, make sure you have both Node.js and npm (Node Package Manager) installed on your machine. You can install them by visiting Node.js and following the installation instructions.

Step 1: Create a React App

If you haven't already set up a React app, use the following commands to create one:

npx create-react-app my-tailwind-app
cd my-tailwind-app
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Tailwind CSS

Next, install Tailwind CSS and its dependencies using npm:

npm install tailwindcss postcss autoprefixer
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure Tailwind CSS

Create a tailwind.config.js file in the root of your project and configure it:

// tailwind.config.js
module.exports = {
  content: [
    "./src/**/*.{js,ts,jsx,tsx}",
    "./public/index.html",
  ],
  theme: {},
  plugins: [],
};
Enter fullscreen mode Exit fullscreen mode

Step 4: Create PostCSS Configuration

Create a postcss.config.js file in the project root:

// postcss.config.js
module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
};
Enter fullscreen mode Exit fullscreen mode

Step 5: Import Tailwind CSS in your styles

Open the src/index.css file and import Tailwind CSS:

/* src/index.css */
@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
Enter fullscreen mode Exit fullscreen mode

Step 6: Use Tailwind CSS Classes in React Components

Now, you can use Tailwind CSS classes directly in your React components. For example:

// src/components/MyComponent.jsx
import React from 'react';

const MyComponent = () => {
  return (
    <div className="bg-blue-500 text-white p-4">
      This is a Tailwind CSS styled component.
    </div>
  );
};

export default MyComponent;
Enter fullscreen mode Exit fullscreen mode

Step 7: Run your React App

Finally, start your React app to see the integration in action:

npm start
Enter fullscreen mode Exit fullscreen mode

Visit http://localhost:3000 in your browser, and you should see your React app with Tailwind CSS styles applied.

Conclusion

Integrating Tailwind CSS with React.js offers a powerful combination for efficient and responsive web development. By following these steps, you can quickly set up and leverage Tailwind CSS classes in your React components. This approach not only enhances your development speed but also ensures a consistent and visually appealing user interface.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (2)

Collapse
 
thota_niharika_ede6c7f51c profile image
thota niharika

Line 33:9: React Hook useEffect has missing dependencies: 'appHeader' and 'dispatcher'. Either include them or remove the dependency array react-hooks/exhaustive-deps

ERROR in ./src/index.css (./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[1]!./node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[2]!./node_modules/source-map-loader/dist/cjs.js!./src/index.css)
Module build failed (from ./node_modules/css-loader/dist/cjs.js):
Error: Package path ./base is not exported from package /Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/tailwindcss (see exports field in /Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/tailwindcss/package.json)
at /Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/enhanced-resolve/lib/ExportsFieldPlugin.js:117:7
at Hook.eval as callAsync
at Resolver.doResolve (/Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/enhanced-resolve/lib/Resolver.js:715:16)
at /Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:76:17
at /Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/enhanced-resolve/lib/DescriptionFileUtils.js:148:13
at /Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/enhanced-resolve/lib/forEachBail.js:39:13
at onJson (/Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/enhanced-resolve/lib/DescriptionFileUtils.js:133:6)
at /Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/enhanced-resolve/lib/DescriptionFileUtils.js:86:7
at runCallbacks (/Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:52:4)
at /Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:279:5
at /Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:598:9
at runCallbacks (/Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:52:4)
at /Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:279:5
at /Users/4887502/Desktop/Eye4/eye3_frontend/node_modules/graceful-fs/graceful-fs.js:123:16
at FSReqCallback.readFileAfterClose as oncomplete

webpack compiled with 1 error and 2 warnings

Collapse
 
pankajsharma1997 profile image
Pankajsharma1997

Good

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay