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"

2
  • Define far far away from the original location. Commented Sep 2, 2012 at 1:30
  • 1
    possible duplicate of Specifying base url for css Commented Sep 2, 2012 at 1:31

2 Answers 2

0

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

background-image: url('/img/picture.gif');
Sign up to request clarification or add additional context in comments.

5 Comments

how is that different from what the OP has?
I added the '/' in an edit immediately after posting. My answer is an absolute path whereas OP's code uses a relative path.
had to refresh the whole page to see the / - now it makes sense
I see, but in my case, some of those scripts are third party, which is nearly impossible to edit
I might be wrong, but I believe you both have relative paths, they're just relative to different things (yours to the webroot, his to the file it's contained in).
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

1 Comment

I see, but in my case, some of those scripts are third party, which is nearly impossible to edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.