Can you set, or where is, the local document root?


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" />


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:

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.

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">
By : bstark


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 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.

This video can help you solving your question :)
By: admin