Get started with 33% off your first certification using code: 33OFFNEW

How to use Tailwind with Vite

2 min read
Published on 21st February 2024

Vite, a modern frontend build tool, is known for its incredibly fast performance, leveraging native ES modules for lightning-fast server start and hot module replacement (HMR). Pairing Vite with Tailwind CSS, a utility-first CSS framework, can significantly enhance your development workflow. This article guides you through the process of setting up Tailwind CSS in a Vite project.

Understanding Vite and Tailwind CSS

Vite provides a simplified development setup with features like out-of-the-box support for TypeScript, JSX, CSS and fast hot module replacement. Tailwind CSS, known for its utility-first approach, allows for rapid UI development. Combining these tools streamlines the process of creating and styling web applications.

Prerequisites

Before you begin, make sure you have Node.js installed on your system. This is necessary for running Vite and Tailwind CSS.

Step 1: Setting Up a Vite Project

If you’re starting a new project, create a Vite project by running:

npm create vite@latest my-vite-app --template vanilla
cd my-vite-app
npm install

This sets up a basic Vite project. You can replace vanilla with other templates like vue, react, etc., depending on your requirements.

Step 2: Installing Tailwind CSS

Navigate to your project directory and install Tailwind CSS and its peer dependencies via npm:

npm install tailwindcss postcss autoprefixer

Step 3: Configuring Tailwind

Create Tailwind configuration files. This step is crucial for customizing Tailwind to fit your project's needs.

npx tailwindcss init -p

This command creates two files in your project: tailwind.config.js and postcss.config.js.

Step 4: Setting Up Tailwind in Your CSS

In your project, create a CSS file (e.g., tailwind.css) and add the Tailwind directives for base, components, and utilities.

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

Include this file in your project's main entry file (e.g., main.js or index.js):

import './tailwind.css';

Step 5: Configuring Tailwind

Edit the tailwind.config.js file to customize your Tailwind setup, such as defining a custom color palette, setting up breakpoints, or enabling features.

Step 6: Running Your Vite Project

Run your Vite project with:

npm run dev

Vite will start the development server, and you can begin using Tailwind classes in your project.

Why use Tailwind with Vite

It's fast. Vite’s fast HMR and Tailwind’s utility classes make for a quick and efficient development process.

It's easy to confire. Vite’s simple setup and Tailwind’s configurable nature allow for easy customization.

It's a modern toolset. Both Vite and Tailwind are modern tools that follow current web development best practices.

Integrating Tailwind CSS with Vite offers a streamlined, efficient, and modern approach to developing web applications. With Vite's fast build tooling and Tailwind's utility-first framework, you can focus more on building your application and less on setup and configuration.