Using Tailwind CSS with PostCSS
Introduction π
Tailwind CSS integrates beautifully with PostCSS, a powerful CSS processing tool used in many modern frontend workflows. When using Tailwind with PostCSS, you unlock advanced features like:autoprefixing, nesting support, minification, and cleaner build pipelines.
>>βPostCSS + Tailwind = a modern, optimized, and production-ready CSS workflow.β β¨
Why Use Tailwind with PostCSS? π§
- β‘ Enables plugins like Autoprefixer automatically
- π§Ή Automatically removes unused CSS in production
- π§ Plays well with bundlers (Vite, Webpack, Next.js, etc.)
- π¦ Cleaner and more scalable file structure
- π± Native CSS features like nesting via plugins
Setup: Tailwind + PostCSS π
Step 1: Create a Project Folder π
Create folder
mkdir tailwind-postcss
cd tailwind-postcssStep 2: Initialize npm π¦
npm init
npm init -yStep 3: Install Tailwind + PostCSS + Autoprefixer π
Install required packages
npm install -D tailwindcss postcss autoprefixerStep 4: Create Configuration Files ποΈ
Generate Tailwind config
npx tailwindcss init -pThis command creates two files:
| File | Purpose |
|---|---|
| tailwind.config.js | Customize Tailwind settings |
| postcss.config.js | Load Tailwind + Autoprefixer plugins |
postcss.config.js Structure π§©
postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};Step 5: Create the Input CSS File π¨
src/input.css
@tailwind base;
@tailwind components;
@tailwind utilities;Note
π‘ For Tailwind v4+, you can replace the above with @import "tailwindcss"
Step 6: Build CSS Using PostCSS ποΈ
Use the PostCSS CLI to process Tailwind and generate final CSS.
Build command
npx postcss src/input.css -o dist/output.cssFor auto-watching changes:
Watch mode
npx postcss src/input.css -o dist/output.css --watchStep 7: Use Tailwind in HTML π
index.html
<!DOCTYPE html>
<html>
<head>
<link href="./dist/output.css" rel="stylesheet">
</head>
<body class="bg-gray-100 flex items-center justify-center h-screen">
<div class="p-6 bg-white shadow-xl rounded-xl">
<h1 class="text-3xl font-bold text-blue-600">Tailwind + PostCSS π</h1>
</div>
</body>
</html>Folder Structure ποΈ
Project Structure
project/
βββ src/
β βββ input.css
βββ dist/
β βββ output.css
βββ index.html
βββ postcss.config.js
βββ tailwind.config.js
βββ package.jsonAdvantages of PostCSS Workflow π
- π¬οΈ Perfect for production builds
- π§© Easy plugin support (nesting, custom media, etc.)
- π¦ Integrates with bundlers & frameworks
- β‘ Optimized CSS output
Production Build π
Minified production build
NODE_ENV=production npx postcss src/input.css -o dist/output.css --verboseNote
π§Ή Tailwind automatically removes unused classes in production whenNODE_ENV=production is set.
Additional Resources π
>>βPostCSS is the engine, Tailwind is the power β together they build perfect CSS.β π