How to use Tailwind CSS in React JS with Vite for Beginners
If you’re new to React JS and looking to use Tailwind CSS for styling your components, this guide is for you. Tailwind CSS is a popular utility-first CSS framework that makes styling web applications a breeze. By combining it with React JS and Vite, you can create beautiful and responsive UIs with ease.
Step 1: Install Tailwind CSS
To get started, you’ll need to install Tailwind CSS in your React project. You can do this by running the following command in your terminal:
npm install tailwindcss
Step 2: Configure Tailwind CSS
Next, you’ll need to create a Tailwind CSS configuration file. You can do this by running the following command:
npx tailwindcss init
Step 3: Integrate Tailwind CSS with Vite
Now that you have Tailwind CSS installed and configured, you’ll need to integrate it with Vite. You can do this by adding the following code to your Vite config file:
import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'
import VitePlugin from 'vite-plugin-tailwind'
export default defineConfig({
plugins: [
reactRefresh(),
VitePlugin()
],
})
Step 4: Start using Tailwind CSS in your React components
Now that you’ve set up Tailwind CSS with Vite, you can start using it in your React components. Simply include the classes in your JSX like this:
<div className="bg-blue-500 text-white p-4">
Hello, Tailwind CSS!
</div>
Congratulations! You now know how to use Tailwind CSS in React JS with Vite for beginners. Happy coding!