Learn HTML and CSS Part 1: Linking the CSS File

In this section, the value of the href attribute is set to /style.css. Why is the slash included, why not just style.css? By the way, style.css works as a substitution, but will it always work in place of /style.css, or /blahblahblah, where blahblahblah is the name of the css file?

The forward slash is an absolute reference to the root directory. Omitting it tells the browser to look for the file in the same directory as the HTML. In this case the HTML is in the root directory along with the CSS so both with and without the slash works.

Other relative path references are ‘./’ for this directory, ‘…/’ for one directory back up the tree, and ‘…/…/’ for two directories back and so on.

Relative references are common but can break easily if your HTML or CSS are moved to another directory. Absolute references can be broken too, I guess but they’re more resilient.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.