ūü•≥
WindFrame is here: Design, prototype and code
sale icon End of Summer Sale ūüéĀ Get Contrast PRO UI library + admin template with 5 admin dahsboards, 23 pages and 10000+ components at 60% off ūüéČ
postImage

How to install Tailwind CSS in Laravel

blogImage

By Emmanuel Chinonso

Technical Writer

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 can prove difficult as it requires the developer to create their own configuration file ,along with the Laravel mix.

This tutorial, however, will walk you through how to do this efficiently.

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

1laravel new my-project
2cd my-project.

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

1npm install laravel

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

1npm install tailwindcss

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

1Yarn add tailwindcss

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

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

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

1npm install -d tailwindcss@latest postcss@latest autoprefixer@latest

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

1npx tailwindcss init.

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

1module.exports = {
2 purge: [],
3 theme: {
4 extend: {},
5 },
6 variants: {},
7 plugins: [],
8}.

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 laravel 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

1// webpack.mix.js
2mix.js(‚Äúresources/js/app.js‚ÄĚ, ‚Äúpublic/js‚ÄĚ)
3.postCSS(‚Äúresources/css/app.css‚ÄĚ, ‚Äúpublic/css‚ÄĚ, [
4require(‚Äútailwindcss‚ÄĚ),
5 require("autoprefixer"),
6
7]);

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

1/* ./resources/css/app.css */
2@import "~tailwindcss/base.css";
3@import "~tailwindcss/components.css";
4@import "~tailwindcss/utilities.css";

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.

1<!DOCTYPE html>
2 <head>
3 <!-- ... --->
4+ <meta charset="UTF-8" />
5+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+ <link href="{{ asset('css/app.css') }}" rel="stylesheet">
7 </head>
8 <!-- ... --->
9
10

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:

1<body>
2       <div class="bg-gradient-to-br from-purple-900 to-indigo-900 
3 flex flex-wrap items-center 
4 justify-center min-h-screen">
5           <form class="w-full max-w-md bg-white shadow-md rounded px-8 pt-8">
6            <div class="mb-4">
7               <label class="text-gray-800 text-sm block font-bold mb-2 pb-2">Username</label>
8               <input class="shadow appearance-none border 
9 rounded w-full py-2 px-3 text-gray-700 leading-tight 
10 focus:outline-none focus:shadow-outline" id="username" type="text" 
11 placeholder="Username">
12            </div>
13            <div class="mb-6">
14                <label class="block text-gray-700 text-sm font-bold mb-2">Password</label>
15                <input class="shadow appearance-none border 
16 border-red-500 rounded w-full py-2 px-3 
17 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline" id="password" 
18 type="password" placeholder="**************">
19                <p class="text-purple-500 text-xs italic">Please choose a Password.</p> 
20            </div>
21            <div class="flex items-center justify-between">
22                <button class="bg-purple-500 hover:bg-indigo-700  
23 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" 
24 type="button"> 
25 Sign in 
26 </button>
27                <a class="inline-block align-baseline 
28 font-bold text-sm text-indigo-500 hover:text-indigo-900" href="#"> 
29 Forgot Password?
30 </a>
31            </div>
32            <div class="px-8 pt-8">
33                <p class="text-center 
34 text-gray-900 text-xs 
35 font-bold">@2022 Devwares develop. All rights reserved.
36 </p>
37            </div>
38           </form>
39           
40       </div>
41    </body>
42</html>

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.

Create Stunning websites and web apps

Trying to build out all user interfaces and components for your website or web app from scratch can become a very tedious task. A huge reason why we created Contrast Bootstrap to help reduce the amount of time we spend doing that, so we can focus on building some other aspects of the project. Contrast Bootstrap PRO consists of a UI Kit featuring over 10000+ component variants. Together with a template of 5 admin dashboards and 23+ additional multipurpose pages template for building almost any type of website or web app. You can view a demo and learn more about Contrast by clicking here.Try contrast pro

ad-banner

Contrast Bootstrap PRO was built using the most popular CSS framework Bootstrap to help build your next landing, admin SAAS, prelaunch etc project with a clean, prebuilt and well documented template and UI components.Learn more about Contrast

Related Posts

Comments

...