How to specify image paths in CSS files?

I use the Yii structure and have a CSS file that uses some images for background and the like.

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

I tried relative paths, but the same css file is accessed using html pages of different depths, for example:

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

therefore, relative paths do not work (at least not in all browsers).

Is there some Yii way to do this, or should I just use absolute paths and do with it?

+7
source share
2 answers

The paths to images in CSS files always refer to the CSS file, not the page that links to the CSS file.

Therefore, it doesn’t matter that you use the CSS file in HTML pages at different depths of the path, since it always looks from the CSS location.

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

+15
source

You can try using semi-limited paths starting with "/". Like "/images/image1.jpg".

This always refers to the root of the website.

Yours faithfully

0
source

All Articles