Creating a React Vite App and Setting Up Tailwind CSS: A Step-by-Step Guide

Posted by

React Vite is a modern development tool that allows you to create fast and optimized React applications. In this tutorial, we will learn how to create a React Vite app and set up Tailwind CSS with ViteJS. Follow the steps below to get started:

Step 1: Create a new React Vite app
To create a new React Vite app, first, make sure you have Node.js and npm installed on your system. If not, you can download and install them from the official Node.js website.

Open your terminal and run the following command to install the Vite CLI globally:

npm install -g create-vite

Next, create a new React Vite app by running the following command:

create-vite my-react-app --template react

Replace my-react-app with the name of your app. This will generate a new React Vite app with the necessary configurations and dependencies.

Step 2: Install Tailwind CSS
Next, we need to install Tailwind CSS in our React Vite app. To do this, run the following command in your terminal:

npm install tailwindcss postcss autoprefixer

This command will install the necessary dependencies for using Tailwind CSS in your app.

Step 3: Configure Tailwind CSS
After installing the dependencies, we need to configure Tailwind CSS in our React Vite app. Create a tailwind.config.js file in the root of your project folder and add the following configuration:

module.exports = {
  mode: 'jit',
  purge: [
    './index.html',
    './src/**/*.{js,jsx,ts,tsx}'
  ],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

Next, create a postcss.config.js file in the root of your project folder with the following content:

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

Step 4: Import Tailwind CSS in your React components
To use Tailwind CSS styles in your React components, import the Tailwind CSS styles in your index.css file. Create an index.css file in the src folder of your project and add the following imports:

@tailwind base;
@tailwind components;
@tailwind utilities;

Next, import the index.css file in your index.js file:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

Step 5: Start the development server
Now that we have set up Tailwind CSS in our React Vite app, we can start the development server to see our changes in action. Run the following command in your terminal:

npm run dev

This command will start the development server and open your app in the browser. You can now start building your React app with Tailwind CSS styles.

That’s it! You have successfully set up Tailwind CSS in your React Vite app. Happy coding!

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@SlaveObeysOficial
2 months ago

gracias indio, me sirvió