bootstrap styling doesn't work in some views in Laravel 5.2

我只是一个虾纸丫 提交于 2019-12-08 21:13:28

@ThomasKim Is absolutely right. The path to your bootstrap file is relative. So when you're on localhost:8000 it can correctly traverse to css/bootstrap.min.css.

However when you're on /post/1 then the URL request for the css file changes to:

localhost:8000/post/1/css/bootstrap.min.css

And we know that's incorrect. To resolve this, use an absolute path by prepending a /.

<link href="/css/bootstrap.min.css" rel="stylesheet"/>

This will ensure that it always attempts to traverse the file from public/css/bootstrap.min.css. public is omitted in the URL structure, but that is where all HTTP requests resolve to.

UPDATE

It would actually be best to use Laravel's asset or secure_asset functions to resolve these assets instead:

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

The asset helper will resolve directly from /public

Note that asset will use the schema of the page request to resolve the dependency, whereas secure_asset will force the dependency over https no matter what the schema of the request is.

To make life super easy, use Laravel's asset() helper:

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

Yes, it provides the exact same thing as referencing it as simply "/css/..." but I find it to be good practice, especially when things get a bit more advanced later down the line.

You must have used

<link rel="stylesheet" type="text/css" href="css/app.css">

in your main blade layout template, that why facing this problem

Use

<link rel="stylesheet" type="text/css" href="/css/app.css">

and this will resolve the issue.

Just append a forward slash / before css file path, which indicates the public directory of your project.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!