By the end of this lesson, you will be able to construct correct relative URLs for HTML links and assets using root-relative, same-directory, and parent-relative paths.
What it is
A relative URL specifies a location based on the current document's position rather than the absolute domain name. This makes your website portable; if you move the site from a local development folder to a production server, or change the domain, the internal links remain valid without editing every file.
The three primary types are:
- Same-directory (Sibling): Points to a file in the exact same folder as the current HTML file.
- Parent-relative: Moves up one or more directory levels before pointing to a target.
- Root-relative: Starts with a forward slash
/, indicating the path begins at the website's root directory, regardless of the current page's depth.
Why it matters
- Portability: You can develop locally and deploy to any domain without rewriting link structures.
- Maintenance: Changing the domain name requires zero updates to internal HTML files.
- Performance: Browsers resolve relative paths efficiently without needing full protocol/domain parsing for internal navigation.
- Consistency: Root-relative paths ensure that assets like CSS or images load correctly even if the user navigates deep into subdirectories.
Syntax or steps
Assume your website structure looks like this:
root/
├── index.html
├── styles.css
└── about/
├── team.html
└── photos/
└── logo.png
To link from about/team.html:
- Same-directory: If linking to another file in
about/, just use the filename:href="contact.html". - Parent-relative: To go up one level to
styles.css, use../:href="../styles.css". Each../moves up one directory. - Root-relative: To link to
index.htmlat the root, start with/:href="/index.html". This ignores whereteam.htmlis located.
Example
Here is how you would write the head section of about/team.html to correctly reference resources using different relative strategies.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Our Team</title>
<!-- 1. Parent-relative: Goes up from /about/ to root to find styles.css -->
<link rel="stylesheet" href="../styles.css">
<!-- 2. Root-relative: Always starts at the web root, safe for deep pages -->
<link rel="icon" href="/favicon.ico">
</head>
<body>
<header>
<!-- 3. Root-relative: Links back to home regardless of current folder -->
<a href="/">Home</a>
<!-- 4. Same-directory: Links to a sibling file in /about/ -->
<a href="contact.html">Contact Us</a>
</header>
<main>
<!-- 5. Child-relative: Descends into a subfolder -->
<img src="photos/logo.png" alt="Company Logo">
</main>
</body>
</html>
Explanation:
../styles.css: The browser looks in the parent directory ofabout/(the root) forstyles.css./favicon.ico: The leading slash forces the browser to look at the very top of the server structure, not relative toabout/./: A common shorthand for the root index file.contact.html: Assumescontact.htmlexists inside theabout/folder.photos/logo.png: Looks inside thephotossubdirectory within the currentabout/folder.
Common mistakes
- Missing the slash: Writing
href="styles.css"when the file is actually in the parent directory. Fix: Use../styles.css. - Confusing root-relative with absolute: Writing
http://example.com/styles.cssbreaks portability. Fix: Use/styles.cssfor root-relative paths. - Over-nesting parent references: Using too many
../sequences makes code hard to read. Fix: Prefer root-relative paths (/path/to/file) for complex structures. - Case sensitivity: On Linux servers,
/Images/Logo.PNGis different from/images/logo.png. Fix: Ensure the case matches the actual file system exactly.
When to use it
| Path Type | Best For | Risk |
|---|---|---|
| Same-directory | Links between closely related pages (e.g., blog posts). | Fails if the file is moved to a different folder. |
| Parent-relative | Simple sites with shallow directory structures. | Becomes messy and error-prone in deep nesting. |
| Root-relative | CSS, JS, Images, and main navigation across large sites. | Requires the site to be served from the domain root. |
Practice
Guided Exercise: Create a folder structure with index.html at the root and blog/post.html. Write the src attribute for an image located at assets/img/photo.jpg using a root-relative path.
Hint: Start with / and follow the path from the root.
Challenge: Rewrite the previous example using a parent-relative path from blog/post.html.
Expected Output: ../../assets/img/photo.jpg (Up two levels from blog/ to root, then down).
Quick check
Question: If you are on /products/electronics/tv.html and want to link to /css/main.css, what is the most robust relative path?
Answer: /css/main.css (Root-relative). While ../../css/main.css works, root-relative is safer if the page moves deeper into folders.
Summary
Relative URLs allow developers to build portable, maintainable websites by defining paths relative to the current document or the site root. Mastering the distinction between same-directory, parent-relative, and root-relative paths ensures that assets and navigation work correctly regardless of deployment environment or directory depth.