Including a css file in a blade template?

Viewed 178639

I want to include a css file in my Laravel blade template.

I've tried:

@include(public_path('css/styles.css'))

But it says view does not exist. It does exist.

How can I include a css file?

Please note, I know this is not the correct way to link css files, but for my use case it is (I'm not building a website).

10 Answers

in your main layout put this in the head at the bottom of everything

@stack('styles')

and in your view put this

@push('styles')

    <link rel="stylesheet" href="{{ asset('css/app.css') }}">

@endpush

basically a placeholder so the links will appear on your main layout, and you can see custom css files on different pages

just put your CSS file into resources/css then you need to compile it with mix and your CSS file will be in public/css use:

<link rel="stylesheet" type="text/css" href="{{ asset('css/app.css') }}" >

to refer to it ... i recommend this video

Most of the answers does not include the building of the css file. Adding the following to the HTML.

<link rel="stylesheet" type="text/css" href="{{ asset('css/app.css') }}" >

And adding the file /resources/css/app.css to the project.

Assuming it is a fresh project, running the following commands does the trick. Off course using run prod for the deployment build.

npm install
npm run dev

include the css file into your blade template in laravel

  1. move css file into public->css folder in your laravel project.
  2. use <link rel="stylesheet" href="{{ asset('css/filename') }}">

so css is applied in a blade.php file.

Work with this code :

{!! include ('css/app.css') !!}
Related