如何在 CSS 文件中指定图像路径? [英] How to specify image paths in CSS files?

查看:86
本文介绍了如何在 CSS 文件中指定图像路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Yii 框架并有一个 CSS 文件,该文件使用一些图像作为背景和类似图像.

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

其他 PHP 代码可以使用 Yii::app()->request->baseUrl 为资源添加正确路径的前缀.但是,css文件不是PHP,所以我不能使用代码.

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.

我尝试使用相对路径,但是不同深度的html页面访问同一个css文件,例如:

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).

是否有一些 Yii 方法可以做到这一点,还是我应该只使用绝对路径并完成它?

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

推荐答案

CSS 文件中图像的路径总是相对于 CSS 文件,而不是引用 CSS 文件的页面.

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

因此,在不同路径深度的 HTML 页面中使用 CSS 文件应该无关紧要,因为它总是从 CSS 的位置看.

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.

例如,如果您的 CSS 文件在 /Content/Css 中,而您的图片在 /Content/Images 中,那么您应该始终将您的图片引用为 url(../Images/something.png).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆