HTML Relative URLs
Suppose you are in a library and someone asks, "Where is the dictionary?" You could answer with a full postal address, but that would be silly. You are already inside the building. It is much easier to say, "Go straight, then second shelf on your left."
That is a relative URL. Instead of the complete web address, it gives directions starting from where you are now. For links between pages on your own website, it is the tool professional developers reach for most often. Once you understand it, you will stop seeing "page not found" errors caused by a wrong path.
What You Will Learnβ
- What a relative URL is and how it differs from an absolute URL
- How to link to a file in the same folder, a subfolder, and the parent folder
- What
./,../, and a leading/mean - How to read a folder structure like a map
- Why file names and letter case matter
- Common mistakes and a simple way to debug a broken path
The Basicsβ
A relative URL leaves out the scheme and the host. The browser fills them in using the address of the current page.
- HTML Code
- Browser Output
<nav>
<a href="about.html">About</a>
<a href="contact.html">Contact</a>
</nav>
The demo links point to # so they stay on this page. In your own project, the browser would turn about.html into the full address, using the folder that index.html lives in.
For example, if the current page is https://example.com/site/index.html, then:
| Relative URL | The browser builds |
|---|---|
about.html | https://example.com/site/about.html |
contact.html | https://example.com/site/contact.html |
Read Your Folder Like a Mapβ
Every path in a relative URL is a set of directions through your project folders. Here is a small website we will use for all the examples.
my-website/
βββ index.html
βββ about.html
βββ css/
β βββ style.css