Laravel 4 背景图片:网址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16995872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 background-image: url
提问by Lynx
I am trying to get the full path of my image to work across the board instead of using image/blah/blah on my page I have them working with the blade template engine
我试图让我的图像的完整路径全面工作,而不是在我的页面上使用图像/等等/等等 我让他们使用刀片模板引擎
{{ HTML::style('css/bootstrap.css'); }}
{{ HTML::style('css/bootstrap-responsive.css'); }}
{{ HTML::style('css/style.css'); }}
{{ HTML::script('js/jquery.js'); }}
But I am trying to use background-image for a menu bar
但我正在尝试使用背景图像作为菜单栏
background-image: url(images/transparentBackground.png)
Is there a way to use Blade templates to get that image to load the full path?
有没有办法使用 Blade 模板来获取该图像以加载完整路径?
If I use
如果我使用
background-image: url({{ HTML::script('images/transparentBackground.png'); }})
It doesnt work and returns errors.
它不起作用并返回错误。
回答by Rogier
Well, first of all, you'd want to use
嗯,首先,你想使用
url({{ URL::asset('images/transparentBackground.jpg') }})
instead of
代替
`HTML::script()`.
That would work if you are using inline styles.
如果您使用内联样式,那会起作用。
If you are trying to have blade parse a linked stylesheet however, that's just never going to work. Nor should it. In that case, you might want to look into using something like Sass and Compass. With the right configuration, you can have full paths to your images generated automatically.
但是,如果您试图让刀片解析链接的样式表,那将永远行不通。也不应该。在这种情况下,您可能需要考虑使用 Sass 和 Compass 之类的东西。通过正确的配置,您可以拥有自动生成的图像的完整路径。
回答by spezia
I don't understand your problem very well. Why do you need a full path? This is css. Images folder has to be near a css file.
我不太明白你的问题。为什么需要完整路径?这是css。图像文件夹必须靠近 css 文件。
In your case try background-image: url(../images/transparentBackground.png)
在你的情况下尝试 background-image: url(../images/transparentBackground.png)
-- public (folder)
---- css/style.css
---- images/transparentBackground.png
回答by Hussnain sheikh
You can use helper functions Here
您可以在此处使用辅助函数
Here is an example of background-image using helper function
这是使用辅助函数的背景图像示例
background-image: url("{{ public_path('images/bg_invoice.jpg') }}");
background-image: url("{{ public_path('images/bg_invoice.jpg') }}");
public_path is a helper function to the path of public folder in your Laravel app and images is the nested folder in public. Thanks
public_path 是 Laravel 应用程序中公共文件夹路径的辅助函数,图像是公共中的嵌套文件夹。谢谢
回答by M'Baku
For Laravel 5 just do this
对于 Laravel 5,只需执行此操作
style="background-image:url({{ asset('Images/jamb.jpg') }});"
And like @Khandad Niazi said its just for inline CSS
就像@Khandad Niazi 说它只是用于内联 CSS
回答by PottyBert
If you are writing inline styles (via the style="" attribute in the HTML tag, then what @Rogier suggested would work.
如果您正在编写内联样式(通过 HTML 标记中的 style="" 属性,那么@Rogier 建议的内容会起作用。
If you want to use blade to generate CSS then you could do it by creating a CSS controller and passing your variables through that to a blade template to render the CSS (remember to set the content-type header to text/css).
如果您想使用刀片生成 CSS,那么您可以通过创建一个 CSS 控制器并将您的变量通过它传递给刀片模板以呈现 CSS(记住将内容类型标题设置为 text/css)来实现。
回答by Khandad Niazi
HTML::image('url/to/image.jpg', 'alt text');
HTML::image('url/to/image.jpg', 'alt text');
OR
或者
CAN BE USED
可以使用
img src="{{ URL::to_asset('url/to/image.jpg') }}