views:

547

answers:

6

When opening a file from your hard drive into your browser, where is the document root? To illustrate, given the following HTML code, if the page is opened from the local machine(file:///) then where should the css file be for the browser to find it?

<link href="/temp/test.css" rel="stylesheet" type="text/css" />
+1  A: 

It depends on what browser you use, but Internet Explorer, for example, would take you to the root directory of your harddrive (eg. C:/), while browsers such as Firefox does nothing.

Andy
Not sure this answers the question accurately, what does everyone else think?
Sam Murray-Sutton
A: 

Eric, the document root is the folder in which your file is, wherever it may be.

Mario Marinato -br-
+1  A: 

On a Mac, the document root is what you see in the window that appears after you double click on the main hard drive icon on your desktop. The temp folder needs to be in there for a browser to find the CSS file as you have it written in your code.

Actually, you could also write the code like this:

<link href="file:///temp/test.css" rel="stylesheet" type="text/css" />
A: 

As far as local, static html goes, unless you specify it, most browsers will take the location of the html file you are viewing as the root. So any css put in there can just be referenced by it's name only.

The lazy way to get the correct reference for your css file is to open it in your browser. Then just grab the url that you see there - something like:

file:///blah/test.css
and copy that into your stylesheet link on your html:
<link href="file:///blah/test.css" rel="stylesheet" type="text/css">

Either that or you can just take the url for the html file and amend it to refer to the stylesheet.

Then your local page should load fine with the local stylesheet.

Sam Murray-Sutton
A: 

Erick,

If you're interested in setting the document root, you might look at getting a web server installed on your machine, or, if you already have one (like Apache or IIS), storing your project-in-development in the web root of that server (htdocs in Apache, not entirely sure in IIS). If you'd rather leave your files where they are, you can set up virtual hosts and even map them to addresses that you can type into your browser (for example, I have a local.mrwarshaw.com address that resolves to the web root of my personal site's development folder).

If you're on Windows and don't want to mess around with setting up a server on your own, you could get a package like XAMPP or WAMPP, though bear in mind that those carry the extra weight of PHP and MySQL with them. Still, if you've got the space, they're a pretty easy drop-in development environment for your machine.

Brian Warshaw
A: 

You can, but probably don't want to, set the document root on a per-file basis in the head of your file:

<base href="my-root">
bstark