How to Install Tailwind CSS in Laravel Project Using Laravel Mix?

Tailwind CSS blog | Published on : 10th January, 2024

In this post, you will learn how to install Tailwind CSS in Laravel project using Laravel Mix, and how to use it to create a beautiful and responsive website and we'll explore integrating Tailwind CSS seamlessly into your Laravel project using the powerful Laravel Mix tool.

Understanding Tailwind CSS:

Tailwind CSS is a utility-first CSS framework that lets you style your website directly in your HTML. It gives you a lot of flexibility and control over your design, without writing complex CSS rules.

Laravel Mix:

Laravel is a popular PHP framework that makes web development simple and enjoyable. It has a powerful feature called Laravel Mix, which is a tool that makes it easy to compile and bundle your assets, such as CSS, JavaScript, images, and fonts.

Step-by-Step Installation Guide:

Step 1: Install Tailwind CSS and Laravel Mix

The first step is to install Tailwind CSS and Laravel Mix in your Laravel project. Open a command prompt, navigate to your Laravel project directory, and run the following commands:

# Install Tailwind CSS and its peer dependencies via npm
npm install tailwindcss postcss autoprefixer

# Install Laravel Mix plugin for Tailwind CSS
npm install laravel-mix-tailwind

These commands install Tailwind CSS and its dependencies like PostCSS and Autoprefixer. Additionally, they install the Laravel Mix plugin for Tailwind CSS, seamlessly integrating it with Laravel Mix.

Step 2: Create Tailwind CSS Configuration File

Generate the Tailwind CSS configuration file by running:

# Create tailwind.config.js file
npx tailwindcss init

This command will create a tailwind.config.js file in your project root directory, which will look something like this:

// tailwind.config.js
module.exports = {
purge: [],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}

This file contains some basic options for Tailwind CSS, such as purge, darkMode, theme, variants, and plugins. You can modify these options to change the default behavior and appearance of Tailwind CSS. For example, you can add the paths to your template files in the purge option, which will remove any unused CSS classes from your final output. You can also add your own custom colors, fonts, spacing, breakpoints, and more in the theme option. You can learn more about the tailwind.config.js file and its options here.

Step 3: Add Tailwind CSS to Laravel Mix Configuration

The third step is to add Tailwind CSS to your Laravel Mix configuration, which is a file that tells Laravel Mix how to compile and bundle your assets. To do this, you need to open the webpack.mix.js file in your project root directory, and add tailwindcss as a PostCSS plugin. Your webpack.mix.js file should look something like this:

// webpack.mix.js
const mix = require('laravel-mix');

mix.js('resources/js/app.js', 'public/js')
.postCss('resources/css/app.css', 'public/css', [
require('tailwindcss'),
]);

This configuration instructs Laravel Mix to compile the app.js file to public/js and process the app.css file to public/css using Tailwind CSS.

Step 4: Add Tailwind CSS Directives to Your CSS File

The fourth step is to add Tailwind CSS directives to your CSS file, which are special comments that tell Tailwind CSS where to inject its base, components, and utilities styles. To do this, you need to open the app.css file in the resources/css directory, and add the following lines:

/_ app.css _/
@tailwind base;
@tailwind components;
@tailwind utilities;

These lines inject Tailwind CSS's base, components, and utilities styles into your CSS file.

Step 5: Start Your Build Process

The final step is to start your build process and use Tailwind CSS in your project. To start your build process, run the following command:

# Run your build process
npm run dev

This command compiles and bundles your assets using Laravel Mix, creating a final app.css file in public/css containing all Tailwind CSS styles.

Step 6: Integrate Tailwind CSS in Your Project

Ensure your compiled CSS file is included in the <head> section of your HTML template. Start using Tailwind CSS utility classes to style your content. For example, create a header in your Blade template:

<!-- app.blade.php -->
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
    <header class="bg-gray-800 py-4 px-6 flex items-center justify-between">
        <a href="/" class="text-white text-xl font-bold">TailwindTap</a>
        <nav class="space-x-4">
            <a href="/changelog" class="text-gray-300 hover:text-white">Changelog</a>
            <a href="/blog" class="text-gray-300 hover:text-white">Blog</a>
            <a href="/contact-us" class="text-gray-300 hover:text-white">Contact</a>
        </nav>
    </header>
</body>
</html>

This example demonstrates how to use Tailwind CSS classes to style a header with a logo and navigation menu. Customize it according to your project requirements.

Congratulations! You've successfully installed and integrated Tailwind CSS into your Laravel project using Laravel Mix. Enjoy the flexibility and efficiency it brings to your web development workflow.

Benefits of Using Tailwind CSS in a Laravel Project with Laravel Mix:

  • Flexibility and Customization: Tailwind CSS is very flexible. Developers can change or add to the default settings as they want for their project. This way, the framework can fit the different design needs of the Laravel app.

  • Responsive Design Made Easy: Tailwind CSS makes responsive designs easy with its responsive classes. Developers can quickly handle layout changes for different screen sizes without writing too many media queries.

  • Performance Optimization: Tailwind CSS and Laravel Mix work well together for better asset compilation. The CSS that is made is small and clean, as it removes the styles that are not used in the final build. This makes the file size less and the performance better.

  • Active Community and Documentation: Tailwind CSS has a lot of fans and good docs. Developers can find help, tips, and demos easily, making it faster to learn and use the framework in Laravel projects.

Read More:
  1. How to Use Tailwind CSS with Python Django?
  2. Setting Up Tailwind CSS In A VueJs Project
  3. Step by Step Guide to Setting Up Tailwind CSS with Laravel
Logo
Our team is creating and sharing useful resources on a regular basis and thousands of developers are using Tailwind CSS to create amazing websites with TailwindTap s free open-source pre-made components and templates.

Hire a Developers

TailwindTap FacebookTailwindTap TwitterLinkedinInstagram