CSS URL References And URL Rewriting

I can get a bit overboard with my virtual paths. I tend to prefer virtual paths over relative paths since they feel safer to use. For example, when applying a background image via CSS, I will tend to do this:

body

{

    background: url('/images/bg.gif');

}

My thinking was that since much of the code I write employs URL rewriting and Masterpages, I never know what the url of the page will be that references this css. However my thinking was wrong.

One problem I ran into is that on my development box, I tended to run this code in a virtual directory. For example, I have Subtext running in the virtual directory /Subtext.Web. So I end up changing the CSS like so:

body

{

    background: url('/Subtext.Web/images/bg.gif');

}

Thus when I deploy to my web server, which is hosted on a root website, I have to remove the /Subtext.Web part. Now if I had read the CSS spec more closely, I would have noticed the following line:

Partial URLs are interpreted relative to the source of the style sheet, not relative to the document.

Thus, the correct CSS in my case (assuming my css file is in the root of the virtual application) is...

body

{

    background: url('images/bg.gif');

}

Now I have true portability between my dev box and my production box.

It turns out that Netscape Navigator 4.x incorrectly interprets partial URLs relative to the html document that references the css file and not the css file itself. Perhaps this was where I got the wrongheaded notion embedded in my head way back in the day.

What others have said

Requesting Gravatar... Alex Jan 12, 2006 3:42 PM
# re: CSS URL References And URL Rewriting
One problem you might encounter.. is that the following style for example :

table
{
behavior: url('htc/grid.htc')
}

actually doesn't work by the css specification

( as I understood , it's because htc is MS standart and they didn't care much about the css spec in this case )

so in this case it's actually relative to the document and not the source of the style sheet .

There are ways to work this out .. but they are pretty ugly..


Requesting Gravatar... Haacked Jan 12, 2006 4:51 PM
# re: CSS URL References And URL Rewriting
Ah... good to know.
Requesting Gravatar... alain Apr 20, 2006 12:25 PM
# re: CSS URL References And URL Rewriting
Alex, I've exactly the problem you're talking about with htc files.
What are the ugly workaround you're talking abount?
Today, I use absolute url but each time I deploy my webapplication on a different context, (like www.xxx.com/dev and www.xxx.com/prod) I must rename the url inside several css files.
Is there something nicer to do?
Requesting Gravatar... Andrzej Sawicki Feb 04, 2007 10:37 AM
# re: CSS URL References And URL Rewriting
the url() is relative to where a css is stored not the virtual directory.
you can use ../ in order to get up directory and find the image
in my case this was
../images/something.png

andrzej
Requesting Gravatar... BILL Sep 25, 2007 2:30 PM
# re: CSS URL References And URL Rewriting
IS IT POSSIBLE TO SPECIFY THE SIZE OF THE (IMAGE URL IN CSS). THX
Requesting Gravatar... Hanan Nov 28, 2007 11:54 PM
# re: CSS URL References And URL Rewriting
You have saved my day.
thanks !
Requesting Gravatar... Mike Dec 18, 2007 12:02 PM
# re: CSS URL References And URL Rewriting
So simple, yet so undocumented. Thanks.
Requesting Gravatar... hucks Apr 08, 2008 2:35 PM
# re: CSS URL References And URL Rewriting
is it possible to make my logo in the template.css file linkable to the main site.

so when i click the logo the page reloads,

my regards hucks.
Requesting Gravatar... sendi Jul 24, 2008 8:40 AM
# re: CSS URL References And URL Rewriting
this post helped me. Thank you for sharing
Requesting Gravatar... Carlos Aug 11, 2008 11:33 AM
# re: CSS URL References And URL Rewriting
It's good! It helped me too, thanks.
Requesting Gravatar... Rick Aug 21, 2008 5:23 PM
# re: CSS URL References And URL Rewriting
Awesome, thanks for the help friend.
Requesting Gravatar... TN Dec 03, 2008 7:26 AM
# re: CSS URL References And URL Rewriting
Just to share my observation regarding usage of url() *for .htc files*

IE7 interprets the path to .htc file in url() function like this
- absolute path (e.g. behavior=url('/styles/HiLite.htc')) won't work
- relative path is relative to the web page (.aspx .php etc files) that use the stylesheet. Note that for ASP.NET even if the master page is used the relative path is still *relative* to the content file
Requesting Gravatar... dotnetguts May 16, 2010 3:59 PM
# re: CSS URL References And URL Rewriting
Did anyone found solution how to access .htc file from .css?
Specially when you are doing URL Rewriting.

In my .Net Project i can able to access .htc file when page is not URL Rewritten, but when I tried to access .htc file for page that is URL Rewritten its not working!

I have tried almost all tricks, anybody any idea?

What do you have to say?

(will show your gravatar)
Please add 4 and 1 and type the answer here: