如何在CSS文件中指定图像路径? [英] How to specify image paths in CSS files?
问题描述
其他PHP代码可以使用Yii :: app() - 我使用Yii框架和一个CSS文件使用一些图像的背景和类似。 > request-> baseUrl来为资源添加正确的路径。但是,css文件不是PHP,所以我不能使用代码。
我尝试了相对路径,但同一个css文件是由不同深度的html页面访问,例如:
http:// mysite / controller / action1 / 10
http:// mysite / controller
因此相对路径不工作(至少不是在所有浏览器中)。
有没有一些Yii方式做这个,或者我应该使用绝对路径,并使用它吗?
b b b b b b b b p>因此,您在不同路径深度的HTML页面中使用CSS文件并不重要,因为它总是从CSS的位置查看。
例如,如果你的CSS文件在 / Content / Css
,你的图像在 / Content / Images
始终以 url(../ Images / something.png)
的形式引用您的图片。
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?
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)
.
这篇关于如何在CSS文件中指定图像路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!