In this post i will show, how to install Tailwind CSS 3 in Laravel 9.
I will do this in steps wise:
- Install Laravel Project
- Setup Tailwind CSS
- Add Tailwind CSS to app.css
- Import Vite Assets to Laravel Blade
- Run Vite Development Server
Step 1. Install Laravel Project
Install Laravel 9 using the below command.
composer create-project laravel/laravel TailwindCSSExample;
Step 2: Setup Tailwind CSS
In this step, I will install Tailwind CSS and It’s dependency.
Tailwind CSS Dependency
- PostCSS
- auto-prefixer
We will install using below command.
First install Tailwind CSS.
npm install -D tailwindcss
Install the dependency.
npm install -D postcss autoprefixer
Now i have to generate Tailwind and post CSS configuration file. Will do using the below command.
npx tailwindcss init -p
The above command will create two configuration file.
tailwind.config.js
/** @type {import('tailwindcss').Config} */ module.exports = { content: [], theme: { extend: {}, }, plugins: [], }
Now in this file , we do some change for Laravel blade file. Like below code.
/** @type {import('tailwindcss').Config} */ module.exports = { content: [ "./resources/**/*.blade.php", "./resources/**/*.js", ], theme: { extend: {}, }, plugins: [], }
postcss.config.js
module.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, }, }
Step 3: Add Tailwind CSS to app.css
Now add the below line in app/resources/css/app.css
@tailwind base; @tailwind components; @tailwind utilities;
now remove ‘resources/css/app.css‘, from vite.config.js. Now vite.config.js is look like below code.
vite.config.js
import { defineConfig } from 'vite'; import laravel from 'laravel-vite-plugin'; export default defineConfig({ plugins: [ laravel({ input: [ 'resources/js/app.js' ], refresh: true, }), ], });
Now i will do some change in resources/js/app.js , this changes will use for importing css via Javascript.
Add import ‘../css/app.css’ this to resources/js/app.js. And this file will looks like below.
resources/js/app.js
import './bootstrap'; import '../css/app.css'
Step 4: Import Vite Assets to Laravel Blade
I will change resources/views/welcome.blade.php , this is already in your project, You can also create a new blade.
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Install Tailwind CSS 3 in Laravel 9 With Vite 3</title> @vite('resources/js/app.js') </head> <body class="antialiased"> <div class="flex justify-center items-center h-screen"> <h1 class="text-3xl text-purple-600 font-bold">Install Tailwind CSS 3 in Laravel 9 With Vite 3</h1> </div> </body> </html>
Step 5: Run Vite Development Server
Run the below command to start the Vite Development server.
npm run dev
Step 6: Run Laravel Development Server
php artisan serve
Now all done. Happy Coding..
We are Recommending you
Increase Session Timeout Limit in Laravel9
Laravel9: Add Text or Image Watermark
Laravel 9- Crop Image and Upload to S3
Summernote Editor With Image Upload in Laravel
Laravel 9 send mail using SMTP
How to Export data in Excel and CSV
404 Not Found The requested URL was not found on this server
Laravel 9 Upload Zip and Extract on server
Laravel 9 – How to Get User Country, City, ZIP from IP
Laravel 9 : Deploy a project on Amazon AWS