DEV Community

Cover image for How to install Tailwind CSS in Laravel
Sampson Ovuoba for Devwares

Posted on • Originally published at devwares.com on

How to install Tailwind CSS in Laravel

Tailwind CSS is a utility-first class CSS framework unlike other CSS frameworks like material UI and Bootstrap.
It offers a lot of advantages in which you can check some of it in our article Why Tailwind CSS is so good.
In this Tailwind CSS tutorial, we are going to look at how to install Tailwind CSS in Laravel. This is especially great because using tailwind with Laravel offers Php developers a lot of advantages that other frameworks fail to do.

Installing tailwind CSS in Laravel is not that simple as it requires a bit of configuration along with the Laravel mix.
However, we are going to work you through on step by step on how to do this.

Table of content

  • Creating the project
  • Install Tailwind CSS
  • Create your configuration file
  • Configure the Tailwind Css to larval Mix
  • Include tailwind in your CSS
  • Building a form using tailwindcss in laravel
  • Conclusion

Install Tailwind CSS with

Creating the project

We must first create our project before we can start using the tailwind CSS in larval. You can create this by running the following command on the Laravel installer

Code

laravel new my-project
cd my-project. 

Enter fullscreen mode Exit fullscreen mode

In the code above we created our new project using the command “laravel new my-project”. You can name your project anything you wish we used my-project in our own. We went ahead to navigate to the root of our app.
We are going to install Laravel dependencies by using the ‘npm’

Code

npm install laravel 

Enter fullscreen mode Exit fullscreen mode

this command will install all the packages you need for the larval app.
After this, we can go ahead to install Tailwind CSS in larval app.

Install Tailwind CSS

These are some of the ways you can install tailwind css in Laravel. These ways include:

Using npm

You can install the Tailwind CSS in larval using this method by running the following command on your terminal

Code

npm install tailwindcss

Enter fullscreen mode Exit fullscreen mode

this command makes sure that tailwind CSS is installed in our project.
Using yarn
You can use yarn which is a good package manager to install your tailwind by running the following command

Code

yarn add tailwindcss
Enter fullscreen mode Exit fullscreen mode

Using a CDN

This method is not the best option to install your tailwind CSS in the Laravel app. It does not enable all the features tailwind CSS has to offer.
You can use this method by simply copying the CDN from the tailwind website

<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
Enter fullscreen mode Exit fullscreen mode

Although, in this project I will be using npm to install the Tailwind CSS with larval. I will go to the root of our application and run the following command in the terminal

Code

npm install -d tailwindcss@latest postcss@latest autoprefixer@latest
Enter fullscreen mode Exit fullscreen mode

Create your configuration file

we must create the configuration file because is used by Laravel mix when building the CSS from the resource folder to the public folder’s app.css.
we can generate the configuration file by running the following command

Code

npx tailwindcss init.
Enter fullscreen mode Exit fullscreen mode

The command above will generate a file on your larval app root, the file will be named tailwind.config.js. open it on your text editor or an IDE. The code below will be seen on it.

Code

module.exports = {
  purge: [],
  theme: {
    extend: {},
  },
  variants: {},
  plugins: [],
}.
Enter fullscreen mode Exit fullscreen mode

The code above is the default setting and you are free to make changes to it. You can find more about the configuration file on the tailwind documentation.

Configure the Tailwind Css to larval Mix

At the root of your application. In Laravel ‘s Mix in the webpack.mix.js, you need to add ‘tailwindcss as a PostCSS plugin. To do this simply, write the following code to the file
Code

// webpack.mix.js

mix.js(resources/js/app.js, public/js)
.postCSS(resources/css/app.css, public/css, [
require(tailwindcss),
 require("autoprefixer"),

]);
Enter fullscreen mode Exit fullscreen mode

Include tailwind in your CSS

You will need to import the tailwind from the app.css using the path ./resources/css/app.css. As this was generated by default and replace the original file contents with @tailwind directives to include Tailwind’s base, component, and utilities styles.

Code

/* ./resources/css/app.css */

@import "~tailwindcss/base.css";
@import "~tailwindcss/components.css";
@import "~tailwindcss/utilities.css";

Enter fullscreen mode Exit fullscreen mode

The new changes will enable tailwind to swap these directives out at build-time along with all the styles it generates based on the configuration design system.
Now import your stylesheet in your main Blade layout and add the appropriate responsive viewpoint meta tag if you haven’t done that already.

<!DOCTYPE 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>
  <!-- ... --->
Enter fullscreen mode Exit fullscreen mode

You can go ahead to build the application you want using Tailwind CSS with Laravel. When you run ‘npm run watch’, ‘npm run dev’, or ‘npm run prod’, Tailwind CSS will be available in your Laravel Mix Project.

Building a form using Tailwind CSS in laravel

As we have seen how to install Tailwind CSS in a laravel project. We are going to build a simple form to demonstrate that our Tailwind CSS is actually working.
Open the resource folder and click on the welcome.blade.php file. Inside the file delete the body of the code.
You can now go ahead and add the following code to the body of the page.

Code:

<body>
       <div class="bg-gradient-to-br from-purple-900 to-indigo-900 
          flex flex-wrap items-center 
          justify-center min-h-screen">
           <form class="w-full max-w-md bg-white shadow-md rounded px-8 pt-8">
            <div class="mb-4">
               <label class="text-gray-800 text-sm block font-bold mb-2 pb-2">Username</label>
               <input class="shadow appearance-none border 
                  rounded w-full py-2 px-3 text-gray-700 leading-tight 
                  focus:outline-none focus:shadow-outline" id="username" type="text" 
                  placeholder="Username">
            </div>
            <div class="mb-6">
                <label class="block text-gray-700 text-sm font-bold mb-2">Password</label>
                <input class="shadow appearance-none border 
                    border-red-500 rounded w-full py-2 px-3 
                    text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline" id="password" 
                    type="password" placeholder="**************">
                <p class="text-purple-500 text-xs italic">Please choose a Password.</p> 
            </div>
            <div class="flex items-center justify-between">
                <button class="bg-purple-500 hover:bg-indigo-700  
                  text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" 
                  type="button"> 
                    Sign in 
                  </button>
                <a class="inline-block align-baseline 
                  font-bold text-sm text-indigo-500 hover:text-indigo-900" href="#"> 
                    Forgot Password?
                  </a>
            </div>
            <div class="px-8 pt-8">
                <p class="text-center 
                  text-gray-900 text-xs 
                  font-bold">@2022 Devwares develop. All rights reserved.
                </p>
            </div>
           </form>
           
       </div>
    </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Below is how our webpage should look once we have saved our file. And reloaded our server.

Tailwind CSS in Laravel

Conclusion

In our tailwind CSS today, we look at how to install Tailwind CSS in laravel project and also went ahead to test whether our Tailwind CSS is working on our laravel by building a sign in form. I hope you enjoyed this tutorial.

Design and code Tailwind CSS websites 3x faster

We created a tool to visually build Tailwind CSS components, prototypes, websites, and web apps. Ship projects faster using an intuitive tailwind builder and editor.Try Windframe out for free.

WINDFRAME

Resources

Top comments (0)