×

Please try us on a desktop

Our app relies on some desktop browser features to render design files and interactable code. Unfortunately, we haven't been able to make these work on mobile yet.

Please try Kombai using a chromium based browser on desktop.

🎉  It's Here!! | The Marketers' Guide to High-Performing Email Designs Download Report

Tailwind

Complete Guide to React and Tailwind CSS Integration

Published on: May 13, 2023

Tailwind CSS has gained rapid popularity among web developers for its utility-first approach, allowing for rapid and responsive web design. React, on the other hand, is a JavaScript library known for its flexibility, scalability, and efficient handling of complex user interfaces. Combining these two powerful tools will enable you to create visually stunning and highly interactive applications with ease.

In this comprehensive guide, we will take a deep dive into the integration of Tailwind CSS with React, exploring best practices and useful tips to help you harness the full potential of both technologies. We will discuss how to set up your environment, create custom components, and optimize your development workflow.

Installing React and Tailwind

Before diving into the integration of Tailwind CSS with React, we need to set up a development environment. We will start by installing the necessary packages and configuring the required files.

1. Create a New React App

To create a new React application, you will need to have Node.js and NPM (or yarn) installed on your machine. Run the following command in your terminal:

npm init vite@latest tailwind-react-app --template react

This command will create a new directory called tailwind-react-app and set up a new React project inside it using the Vite template for React. Change to the new directory:

cd tailwind-react-app

2. Install and Configure Tailwind CSS

Now, let's install Tailwind CSS and its dependencies:

npm install tailwindcss@latest postcss@latest autoprefixer@latest

Next, create a configuration file for Tailwind CSS:

npx tailwindcss init -p

This command will generate two new files: tailwind.config.js and postcss.config.js. Open tailwind.config.js and add your custom configurations, if needed.

Now, create a new CSS file in the src directory of your React application and name it tailwind.css. Import the Tailwind CSS base styles and any additional configurations:

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

Now, import the tailwind.css file in your src/index.js file:

import './tailwind.css';

Building Custom Components in React and Tailwind

With Tailwind CSS properly set up, you can now start building custom React components using the utility-first classes provided by Tailwind. Let's create a simple example - a button component.

Create a Button Component

Create a new file called Button.js in your src/components directory and add the following code:

import React from 'react';

const Button = ({ children, onClick }) => {
  return (
    <button
      className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
      onClick={onClick}
    >
      {children}
    </button>
  );
};

export default Button;

In this example, we have created a reusable button component that applies several Tailwind CSS classes to style the button, including background color, text color, font weight, padding, and border radius.

Use the Button Component in Your Application

Now, you can use the Button component in your application. Open the src/App.js file and import the Button component:

import Button from './components/Button';

Then, use the Button component in your application, for example:

<Button onClick={() => console.log('Button clicked!')}>Click me</Button>

Now, whenever the button is clicked, it will log "Button clicked!" to the console.

Best Practices for Using Tailwind CSS with React

When working with Tailwind CSS and React, it is essential to keep in mind best practices to ensure maintainable and scalable code-

1. Use @apply to DRY Up Your CSS

If you find yourself repeating the same set of Tailwind CSS classes in multiple components, consider using the @apply directive to create reusable classes. In your tailwind.css file, add the following:

.btn-primary {
  @apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded;
}

Now, you can use this custom class in your components:

<button className="btn-primary" onClick={onClick}>
  {children}
</button>

2. Extract Components with Variants

When creating components that have multiple variations, extract the common styles into a base component, and then extend or compose that base component for each variant.

For example, if you have a primary and secondary button, create a BaseButton component with the common styles, and then create PrimaryButton and SecondaryButton components that extend the BaseButton component. Let's see this with an example-

// BaseButton.jsx
const BaseButton = ({ children, onClick, className }) => (
  <button
    className={`font-bold py-2 px-4 rounded ${className}`}
    onClick={onClick}
  >
    {children}
  </button>
);

// PrimaryButton.jsx
const PrimaryButton = ({ children, onClick }) => (
  <BaseButton
    className="bg-blue-500 hover:bg-blue-700 text-white"
    onClick={onClick}
  >
    {children}
  </BaseButton>
);

// SecondaryButton.jsx
const SecondaryButton = ({ children, onClick }) => (
  <BaseButton
    className="bg-gray-500 hover:bg-gray-700 text-white"
    onClick={onClick}
  >
    {children}
  </BaseButton>
);

Now, you can use PrimaryButton and SecondaryButton components in your application to apply different styles for each variant.

3. Use Tailwind CSS Responsively

Tailwind CSS makes it easy to create responsive designs with its built-in breakpoint classes. To apply a style only at a specific breakpoint, prefix the class with the breakpoint name, such as md:text-lg or lg:bg-red-500. Remember that Tailwind CSS is a mobile first framework. So, when building the frontend, style the mobile UI first and then extend it to the larger screens.

Conclusion

In this comprehensive guide, we have explored the integration of Tailwind CSS with React, setting up the environment, creating custom components, and optimizing the development workflow.

By following these best practices and tips, you will be well on your way to creating stunning and responsive web applications with Tailwind CSS and React. The only advice we can give you from here is to practice and work on more and more Tailwind React projects.

No other tutorial or guide will give you the same experience as practice. We hope this article helped you in picking up Tailwind CSS with React and we wish you all the best for your future projects.

Happy Coding!