Back to HTML Notes
Topic #113

Relative URL

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:

  1. Same-directory: If linking to another file in about/, just use the filename: href="contact.html".
  2. Parent-relative: To go up one level to styles.css, use ../: href="../styles.css". Each ../ moves up one directory.
  3. Root-relative: To link to index.html at the root, start with /: href="/index.html". This ignores where team.html is 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 of about/ (the root) for styles.css.
  • /favicon.ico: The leading slash forces the browser to look at the very top of the server structure, not relative to about/.
  • /: A common shorthand for the root index file.
  • contact.html: Assumes contact.html exists inside the about/ folder.
  • photos/logo.png: Looks inside the photos subdirectory within the current about/ 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.css breaks portability. Fix: Use /styles.css for 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.PNG is 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.

Want to go beyond the notes?

Join Coding Now Tech Institute's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Relative URL – FAQs

Quick answers about learning Relative URL in HTML.

This free note from Coding Now Tech Institute explains Relative URL in HTML — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every HTML topic on Coding Now Tech Institute, including Relative URL, is 100% free with no signup required.
With focused practice, most students grasp Relative URL in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now