64

I had to split up a long css file. I put the smaller css files within a styles directory.

Now I have to update the the urls to go up one level with the ../ notation.

Is there anyway to specify the base URL from which to load assets like with the base tag in HTML, but with CSS?

Zoltan Toth
  • 46,038
  • 11
  • 115
  • 133
rhlee
  • 3,679
  • 5
  • 29
  • 36

3 Answers3

59

No, there isn't. I suggest to place the CSS images in at least the same level as the CSS file so that you don't need to go backwards in the path. E.g. /css folder for CSS files and /css/images folder for CSS images. Then you can consistently use url('images/name.ext') for CSS images. This way you can place the root /css folder practically everywhere without fiddling with the image URL's.

BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
-1

As an alternative, you could dynamically add a class to your body tag, and use that in selectors to override css URLs depending on which directory your file is served from.

BBaysinger
  • 6,132
  • 11
  • 58
  • 114
-2

An alternative way to set the base directory in the CSS (which seems to be impossible) is to set the base directory of the HTML document with the <base> tag. This tag is not well known in the community but I found a nice tutorial in the web:

https://webdesign.tutsplus.com/articles/quick-tip-set-relative-urls-with-the-base-tag--cms-21399

It seems to be totally a good solution.

Guybrush
  • 682
  • 7
  • 12