How to load css and js file in laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Asset Loading in Laravel: How to Include CSS and JavaScript in Blade Files

Welcome to the world of Laravel! As you start building dynamic applications, one of the first hurdles you often encounter is correctly linking your static assets—CSS, JavaScript, and images—to your Blade templates. Many new developers run into issues with paths, file permissions, or helper functions.

If you are trying to include files from your public folder using methods like URL::asset(), but the changes aren't reflecting on the screen, don't worry. This is a very common point of confusion. As a senior developer, I can tell you that the issue usually isn't with Laravel itself, but rather with how the file path is constructed relative to the web server root.

This post will walk you through the correct, robust, and modern ways to load CSS and JavaScript files in your Laravel application, ensuring your styles and scripts are loaded exactly as intended.


Understanding Laravel’s Asset Pipeline

In a standard Laravel setup, anything placed inside the public directory is directly accessible via the web server (e.g., Apache or Nginx). The magic lies in using Laravel's built-in helper functions to generate the correct, public-facing URL for these files.

The most reliable way to reference assets is by using the global asset() helper function. This function generates a URL for a file that is located in the public directory.

Best Practice: Always keep your static assets organized within subdirectories inside the public folder, such as /public/css/ and /public/js/.

The Correct Way to Load CSS and JS in Blade

Let's address the specific issue you encountered. If you have structured your files like this:

public/
├── css/
│   └── a.css
└── js/
    └── script.js

And you want to link a.css in your Blade file, the correct implementation using the asset() helper is straightforward and reliable.

Example 1: Linking Stylesheets (CSS)

To link an external stylesheet in your <head> section of a Blade file, use this syntax:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laravel Assets</title>
    {{-- Correct way to link CSS from the public/css directory --}}
    <link href="{{ asset('css/a.css') }}" rel="stylesheet">
</head>
<body>
    <!-- Content goes here -->
</body>
</html>

Notice that by using asset('css/a.css'), Laravel automatically prepends the public path, resulting in a URL like /css/a.css, which the browser can successfully request. This method is consistent and avoids common pathing errors.

Example 2: Including Scripts (JavaScript)

Similarly, for JavaScript files, you should use the script tag, ensuring the file is loaded correctly, often placed just before the closing </body> tag for performance reasons.

    <!-- ... inside <body> tag -->
    <script src="{{ asset('js/script.js') }}"></script>
</body>
</html>

Why did your previous attempt fail? Often, if you were using methods that required specific controller context or if the file structure wasn't exactly what Laravel expected within public, the helper function might not resolve correctly in a simple view context. The key is to rely entirely on asset() for public assets.

Modern Approach: Leveraging Vite for Asset Management

While the method above works perfectly for simple, static files, modern Laravel development heavily favors using Vite for managing CSS and JavaScript. Vite introduces a much more powerful build pipeline that handles asset compilation, bundling, versioning (cache busting), and optimization automatically. This is highly recommended for any project aiming for scalability.

When you use Vite, instead of directly linking static files in Blade, you reference the compiled assets using Vite's directives:

{{-- Vite-based approach --}}
@vite(['resources/css/app.css', 'resources/js/app.js'])

Vite compiles these files into optimized assets and handles the necessary linking for you, ensuring that your assets are always correctly referenced regardless of how complex your project structure becomes. For deeper dives into asset management in Laravel, exploring resources from laravelcompany.com will provide excellent context on modern framework practices.

Conclusion

Loading CSS and JavaScript files in Laravel is fundamentally about understanding the file system relationship between your public directory and the URL the browser expects. Stick to the asset() helper for simple static linking, ensuring your paths are correctly nested within the public folder structure. For larger applications, embrace tools like Vite to manage the complexity of asset compilation. By following these patterns, you ensure a clean, efficient, and maintainable application experience.