ūü§©
Windframe Import update is finally here!
postImage

How to install Tailwind CSS with 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 with 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 with Laravel can prove difficult at first, 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 and easily.

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

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

r
laravel new my-project
cd 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

r
npm 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

r
npm 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

r
Yarn add tailwindcss

Installing Tailwind CSS in Laravel 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

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

r
npm 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

r
npx 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

js
module.exports = {
purge: [],
theme: {
extend: {},
},
variants: {},
plugins: [],
}.

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

js
// webpack.mix.js
mix.js(‚Äúresources/js/app.js‚ÄĚ, ‚Äúpublic/js‚ÄĚ)
.postCSS(‚Äúresources/css/app.css‚ÄĚ, ‚Äúpublic/css‚ÄĚ, [
require(‚Äútailwindcss‚ÄĚ),
 require("autoprefixer"),
]);

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

js
/* ./resources/css/app.css */
@import "~tailwindcss/base.css";
@import "~tailwindcss/components.css";
@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.

html
<!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>
<!-- ... --->

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 with 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:

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

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.

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.Download the free react template

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

...