3

Let's say I have style.css:

#some_element{
   background-image: url('img/picture.gif');
}

The css and picture.gif are located in such a structure:

style.css 
other_style.css
more_style.css
img
   picture.gif

Using php, I combine style.css,other_style.css and more_style.css into cache_style.css which is located far far away from the original location.

In this case, the link will be broken.

In html, we have tag to define (as it name) base url of current document. So that every link and every image can take things base on that base url.

Is there any alternative for css & javascript to do this?

EDIT: some of the css/javascript are minified third party, which is nearly impossible to change "img/picture.gif" into "/something/img/picture.gif"

goFrendiAsgard
  • 4,016
  • 8
  • 38
  • 64

2 Answers2

0

Use an absolute path from the root directory that you're serving:

background-image: url('/img/picture.gif');
Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
0

In javascript you can define a variable to hold your base url, however you can't reference or set CSS with that variable. One approach that would work is to use an absolute link to the gif instead of a relative one. Alternatively, if you know where your cache_style.css file will end up in relation to the root of your site, you can reference the gif by it's relative location to the root. For example if your cache_style.css is located at www.example.com/path/path/cache_style.css, you can reference the img directory by doing this ../../img/picture.gif

jlank
  • 109
  • 4