How to add a css file in laravel?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Add a CSS File in Laravel: A Comprehensive Guide
As developers working within the Laravel ecosystem, managing front-end assets like CSS and JavaScript is a fundamental step. Often, the confusion arises not from the syntax itself, but from how Laravel structures the public directory and how Blade templates correctly reference those files. If you are struggling to apply stylesâlike setting margin-bottom on an elementâit usually points to an incorrect file path or a misunderstanding of Laravel's asset pipeline.
This guide will walk you through the correct, modern, and robust ways to include CSS files in your Laravel application, ensuring they load correctly every time.
Understanding Laravel Asset Management
In a standard Laravel setup, all publicly accessible filesâincluding CSS, JavaScript, images, and fontsâmust reside within the public directory. This is because this folder is what the web server (like Apache or Nginx) serves directly to the end-user.
For your browser to find a file like /assets/css/style.css, that file must be placed at public/assets/css/style.css. If you are linking assets directly in your Blade files, you need to tell the browser exactly where to look relative to the application's document root.
Method 1: The Modern Approach â Using Vite (Recommended)
The most current and recommended way to handle asset bundling in modern Laravel applications is by using Vite. Vite handles compilation, asset fingerprinting, and dependency management seamlessly. This method ensures that when you deploy your application, the CSS files are correctly hashed for caching efficiency.
Step 1: Setup Your Assets
Ensure your CSS file is placed within your public assets folder structure. For this example, let's assume you have a standard setup where Vite handles the compilation process:
public/
âââ css/
âââ app.css <-- Your compiled or source CSS fileStep 2: Linking in Your Blade File with @vite
Instead of manually writing <link> tags, Laravel provides the powerful @vite directive, which automatically handles the correct asset paths and versioning. This is significantly cleaner and more maintainable than manual linking.
In your main Blade layout file (e.g., resources/views/layouts/app.blade.php), you would include it like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel App</title>
{{-- This line automatically links all assets managed by Vite --}}
@vite(['resources/css/app.css', 'resources/js/app.js'])
</head>
<body>
<!-- Your content here -->
</body>
</html>When you use @vite, Laravel manages the path resolution, making sure your CSS is correctly loaded regardless of where the file physically resides in the public directory. This approach aligns perfectly with the component-based philosophy that drives frameworks like Laravel and ensures efficient asset delivery.
Method 2: The Traditional Approach â Using the asset() Helper
If you are working on a simpler project or need to link an external stylesheet directly without using Vite, you can use Laravel's built-in asset() helper function. This function generates a URL for a file in the public directory.
To apply margin-bottom to an element by linking a CSS file manually:
<link rel="stylesheet" href="{{ asset('assets/css/style.css') }}">Troubleshooting Your Original Issue
If your initial attempt using <link rel="stylesheet" href="/assets/css/style.css"> did not work, the most likely cause is that you were linking relative to the application root (/) instead of explicitly referencing the public directory assets. Always use the asset() helper when dealing with files stored in the public folder to ensure cross-environment compatibility within your Laravel project structure.
Conclusion
Adding CSS files to a Laravel project is primarily an exercise in correct file path management and leveraging the framework's built-in tools. For new projects and long-term maintenance, embracing Vite and the @vite directive offers superior performance, dependency handling, and development experience. By understanding where your public assets live and utilizing helper functions like asset(), you can ensure that your styles are applied flawlessly across your application. Keep building great applications with Laravel!