Implementing Multiple Layouts with Different CSS Frameworks in Laravel Using Vite
Introduction:
In this article, we will delve into the process of incorporating distinct layouts, each utilizing different CSS frameworks within a Laravel application, all powered by Vite. Specifically, we will employ Tailwind CSS for one layout (layout1) and Bootstrap 5 for another (layout2).
Getting Started:
To begin, let’s create a new Laravel project by executing the following command:
composer create-project laravel/laravel ProjectName
For the sake of simplicity, we’ll utilize Laravel Breeze as a starting point for authentication, which conveniently installs Tailwind CSS and Vite. Follow these steps:
cd ProjectName
composer require laravel/breeze --dev
php artisan breeze:install //for now here we will select 0-blade
php artisan migrate
npm install
npm run dev
Adding Bootstrap 5:
Next, integrate Bootstrap 5 into our project by running the following npm command:
npm i bootstrap sass @popperjs/core --save-dev
Now, let’s make some file adjustments:
领英推荐
@import '~bootstrap/dist/css/bootstrap.min.css';
import './bootstrap';
//import bootstrap bootstrap
import * as Popper from '@popperjs/core'
window.Popper = Popper
import 'bootstrap'
Configure Vite:
Open app/vite.config.js and update the plugins to include both layouts:
plugins: [
laravel({
input: [
"resources/css/layout1.css", "resources/js/layout1.js",
"resources/css/layout2.css", "resources/js/layout2.js"
],
refresh: true,
}),
],
Incorporate Layouts:
Finally, add the relevant CSS and JS in the respective layouts:
<!doctype html>
<html lang="en">
<head>
<! - Required meta tags →
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Layout 1!</title>
@vite(['resources/css/layout1.css', 'resources/js/layout1.js'])
</head>
<body>
//other code
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<! - Required meta tags →
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Layout 2!</title>
@vite(['resources/css/layout2.css', 'resources/js/layout2.js'])
</head>
<body>
//other code
</body>
</html>
Conclusion: