Paths and the base href

In: Uncategorized

28 Oct 2009

I always preferred accessing assets from the web root like this <img src="/images" /> or in my css as url(/images). Simple, consistent.

However, when forced to delve a little deeper, I had to make some additional distinctions.

First, “/” starts at the web root (obviously), but what’s the root if the page is accessed by the filesystem (e.g.
<base href="file:///C:/Documents and Settings/bentrupk/project" />)? It’s after the 3rd slash. If for some reason you need/want to work locally from the filesystem, you’ll probably need to switch to relative URLs.

So when you say “./”, is it relative to current page or the asset that loaded it (e.g. css files)? What if you have a base tag? The answer is that it’s relative to the base href (if it’s set). Otherwise, it’s relative to the page or the css file that referenced it.