Skip to content

Tailwind CSS - Configuration

The tailwind.config.js file is the heart of any Tailwind CSS project. It’s where you define and customize every aspect of the framework to match your project’s design system. In modern Tailwind (v3+), the configuration works hand-in-hand with the powerful Just-in-Time (JIT) engine, which generates your styles on-demand by scanning your template files. This results in lightning-fast build times and a CSS file that only includes the styles you actually use.

While you can use a CDN for quick demos, a professional setup involves a build process. Here’s the standard way to set up Tailwind CSS in a modern frontend project using a tool like Vite or Next.js.

  • Step 1: Install Dependencies: Install Tailwind CSS and its peer dependencies, PostCSS and Autoprefixer, as development dependencies.
  • Terminal window
    npm install -D tailwindcss postcss autoprefixer
  • Step 2: Initialize Tailwind CSS: Run the init command to generate both tailwind.config.js and postcss.config.js files. The -p flag is crucial for creating the PostCSS config file.
  • Terminal window
    npx tailwindcss init -p
  • Step 3: Configure Template Paths: Open tailwind.config.js and specify the paths to all of your template files in the content array. This is how Tailwind’s JIT engine knows where to look for class names.
  • tailwind.config.js
    /** @type {import('tailwindcss').Config} */
    export default {
    content: [
    './index.html',
    './src/**/*.{js,ts,jsx,tsx}', // Adjust for your project structure
    ],
    theme: {
    extend: {},
    },
    plugins: [],
    }
  • Step 4: Add Tailwind Directives: Create a main CSS file (e.g., src/index.css) and add the @tailwind directives for each of Tailwind’s layers.
  • src/index.css
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
  • Step 5: Import CSS and Start Build Process: Import the CSS file into your main entry point (e.g., main.jsx or App.js) and start your development server. Tailwind will now watch your files and generate CSS as you work.
  • // e.g., src/main.jsx
    import React from 'react'
    import ReactDOM from 'react-dom/client'
    import App from './App.jsx'
    import './index.css' // Import the CSS file
    ReactDOM.createRoot(document.getElementById('root')).render(
    <React.StrictMode>
    <App />
    </React.StrictMode>,
    )

The tailwind.config.js file allows you to customize every detail of the framework. Here are the most important sections:

  • content: As shown above, this is the most critical setting for production builds. It tells Tailwind which files to scan to find utility classes, ensuring that unused styles are removed (tree-shaking), resulting in the smallest possible file size.
/** @type {import('tailwindcss').Config} */
export default {
content: [
'./src/pages/**/*.{html,js,jsx}',
'./src/components/**/*.{html,js,jsx}',
],
// ...
}

theme: This is where you customize your project’s design system. You can adjust colors, fonts, spacing, breakpoints, and more. To preserve Tailwind’s defaults while adding your own, use the extend object.

/** @type {import('tailwindcss').Config} */
export default {
// ...
theme: {
// This will OVERWRITE the entire default color palette
colors: {
'primary': '#1fb6ff',
'secondary': '#7e5bef',
},
extend: {
// This will ADD to the default theme
fontFamily: {
sans: ['Inter', 'sans-serif'],
},
spacing: {
'128': '32rem',
'144': '36rem',
},
borderRadius: {
'4xl': '2rem',
}
}
}
}

plugins: Extend Tailwind’s functionality with official or third-party plugins. Plugins can add new utility classes, components, or base styles.

/** @type {import('tailwindcss').Config} */
export default {
// ...
plugins: [
require('@tailwindcss/forms'),
require('@tailwindcss/typography'),
// require('some-third-party-plugin'),
],
}

darkMode: A first-class feature in Tailwind. Configure how dark mode is activated. The 'class' strategy is the most common and flexible, allowing users to toggle dark mode manually. It works by adding a .dark class to a parent element (usually <html> or <body>).

/** @type {import('tailwindcss').Config} */
export default {
darkMode: 'class',
// ...
}
// Example usage in HTML
// <html class="dark">
// <body class="bg-white dark:bg-slate-900">
// ...
// </body>
// </html>

prefix: Add a custom prefix to all Tailwind utilities to avoid naming conflicts with other CSS libraries. This is especially useful when introducing Tailwind into a large, existing codebase.

/** @type {import('tailwindcss').Config} */
export default {
prefix: 'tw-',
}
// Usage in HTML
// <div class="tw-text-center tw-p-4">...</div>

important: Control the specificity of Tailwind’s utilities. While you can add a ! prefix to any utility (e.g., !p-4), you can configure global behavior.

Setting important to a root selector (like an app ID) is the recommended best practice. It increases specificity without resorting to !important, which can be hard to override and may conflict with third-party libraries that use inline styles.

// Recommended Strategy: Use a selector
/** @type {import('tailwindcss').Config} */
export default {
important: '#app',
}
// Make sure your root element has this ID
// <body id="app">...</body>
// Legacy Strategy (use with caution)
// module.exports = {
// important: true,
// }