How to specify image paths in CSS files?

僤鯓⒐⒋嵵緔 提交于 2020-01-01 03:13:27

问题


I'm using Yii framework and have a CSS file that uses some images for background and similar.

Other PHP code can use Yii::app()->request->baseUrl to prefix the resources with correct path. However, css file is not PHP, so I cannot use the code.

I tried with relative paths, but the same css file is accessed by html pages of different depth, for example:

http://mysite/controller/action1/10
http://mysite/controller

so relative paths don't work (at least, not in all browsers).

Is there some Yii-way of doing this, or should I just use absolute paths and be done with it?


回答1:


The paths to images in CSS files are always relative to the CSS file, not to the page referencing the CSS file.

So it shouldn't matter that you are using the CSS file in HTML pages at different path depths, as it always looks from the location of the CSS.

For example, if your CSS file is in /Content/Css and your images are in /Content/Images then you should always reference your images as url(../Images/something.png).




回答2:


You may try using semi-relative paths that start with "/". Like "/images/image1.jpg".

This are always relative to the root of the website.

Regards



来源:https://stackoverflow.com/questions/5841859/how-to-specify-image-paths-in-css-files

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