HTML5 Mastery: The Complete Web Foundation
HomeInsightsCoursesHTMLURL Structure & Paths (Absolute vs Relative)
Links & Navigation

URL Structure & Paths (Absolute vs Relative)

Master URL anatomy and path types. Learn when to use absolute vs. relative paths, understand URL components, and avoid common linking mistakes.

What is a URL?

A URL (Uniform Resource Locator) is the address used to locate resources on the web. Understanding URL structure is essential for creating correct links.

💡
Fun Fact: URLs are a type of URI (Uniform Resource Identifier). All URLs are URIs, but not all URIs are URLs.

Anatomy of a URL

Let's break down a complete URL into its components:

BASH
https://www.example.com:443/blog/article?id=123&sort=date#section-2
  ↓       ↓       ↓       ↓    ↓      ↓           ↓            ↓
Scheme  Subdomain Domain Port Path  File    Query String   Fragment

Full breakdown:
├─ Protocol/Scheme: https://
├─ Host/Domain: www.example.com
│  ├─ Subdomain: www
│  ├─ Domain: example
│  └─ TLD: .com
├─ Port: :443 (optional, default for https)
├─ Path: /blog/article
├─ Query String: ?id=123&sort=date
└─ Fragment: #section-2

URL Components Explained:

1. Scheme/Protocol

Defines how to access the resource

  • http:// - Unencrypted HyperText Transfer Protocol
  • https:// - Secure HTTP (encrypted with SSL/TLS)
  • ftp:// - File Transfer Protocol
  • mailto: - Email address
  • tel: - Telephone number
  • file:// - Local file system

2. Domain/Host

The server address where the resource is located

BASH
www.example.com
├─ www (subdomain)
├─ example (domain name)
└─ .com (top-level domain)

3. Port

Optional - network port on the server

  • Default ports (usually omitted):
    • HTTP: port 80
    • HTTPS: port 443
  • Custom ports must be specified: :8080, :3000

4. Path

Location of the resource on the server

BASH
/blog/articles/html-guide
├─ /blog (directory)
├─ /articles (subdirectory)
└─ /html-guide (file or route)

5. Query String

Parameters passed to the resource

BASH
?id=123&category=html&sort=date
├─ ? (starts query string)
├─ id=123 (parameter 1)
├─ & (separator)
├─ category=html (parameter 2)
├─ & (separator)
└─ sort=date (parameter 3)

6. Fragment/Hash

Specific section within the resource

BASH
#section-2
└─ Jumps to element with id="section-2"

Absolute URLs

Absolute URLs contain the complete address, including protocol and domain.

Absolute URL Examples
HTML
<!-- Complete URL with all parts --&gt;
<a href="https://www.example.com/blog/article.html">Article</a>

<!-- External site link --&gt;
<a href="https://www.wikipedia.org">Wikipedia</a>

<!-- Secure vs non-secure --&gt;
<a href="https://secure-site.com">HTTPS Site</a>
<a href="http://old-site.com">HTTP Site</a>

<!-- With port number --&gt;
<a href="http://localhost:3000/app">Local Dev Server</a>

When to Use Absolute URLs:

  • ✅ Links to external websites
  • ✅ Email links (href in HTML emails)
  • ✅ Canonical URLs (SEO)
  • ✅ Open Graph tags (social media)
  • ✅ RSS/XML feeds
  • ✅ API endpoints

Advantages:

  • Work from anywhere (portable)
  • Clear and unambiguous
  • Required for external resources

Disadvantages:

  • Longer and more verbose
  • Harder to move site to new domain
  • Can't easily test on different environments

Relative URLs

Relative URLs specify a path relative to the current page's location.

Relative URL Examples
HTML
<!-- Relative to current directory --&gt;
<a href="about.html">About Page</a>

<!-- Subdirectory --&gt;
<a href="blog/article.html">Blog Article</a>

<!-- Parent directory --&gt;
<a href="../index.html">Parent Level</a>

<!-- Root-relative (starts with /) --&gt;
<a href="/contact">Contact Page</a>

<!-- Same page anchor --&gt;
<a href="#section-2">Jump to Section 2</a>

<!-- Query string only --&gt;
<a href="?page=2">Page 2</a>

Types of Relative URLs:

1. Document-Relative

Relative to the current page's location

HTML
Current page: /blog/articles/post.html

<a href="other-post.html">
→ /blog/articles/other-post.html

<a href="images/photo.jpg">
→ /blog/articles/images/photo.jpg

2. Root-Relative

Relative to the site root (starts with /)

HTML
Current page: /blog/articles/post.html

<a href="/about">
→ /about (from root)

<a href="/images/logo.png">
→ /images/logo.png (from root)

3. Protocol-Relative

Uses current protocol (http or https)

HTML
<!-- Inherits protocol from current page --&gt;
<a href="//example.com/page">Link</a>

If on https://mysite.com:
→ https://example.com/page

If on http://mysite.com:
→ http://example.com/page

When to Use Relative URLs:

  • ✅ Internal site navigation
  • ✅ Images, CSS, and JavaScript files on same domain
  • ✅ Development (works on localhost and production)
  • ✅ Easier site migration

Advantages:

  • Shorter and cleaner
  • Portable between environments (dev, staging, prod)
  • Works with or without www prefix
  • Automatically uses correct protocol

Disadvantages:

  • Can be confusing with multiple directory levels
  • Break if page moves to different location
  • Don't work in emails or external contexts

Navigating Directory Structures

Understanding how to navigate directories with relative paths:

Example Site Structure:

website-root/
├── index.html
├── about.html
├── blog/
│   ├── index.html
│   ├── post-1.html
│   └── post-2.html
├── images/
│   ├── logo.png
│   └── banner.jpg
└── assets/
    ├── css/
    │   └── style.css
    └── js/
        └── script.js

Special Path Symbols:

SymbolMeaningExample
/Root directory (at start)/index.html
./Current directory./about.html
../Parent directory (up one level)../index.html
../../Up two levels../../index.html

Query Strings & Parameters

Query strings pass data to pages through the URL:

Query String Syntax
HTML
<!-- Single parameter --&gt;
<a href="search.html?q=html">Search for HTML</a>
→ /search.html?q=html

<!-- Multiple parameters --&gt;
<a href="shop.html?category=books&sort=price&page=2">Books</a>
→ /shop.html?category=books&sort=price&page=2

<!-- With fragment --&gt;
<a href="article.html?id=123#section-2">Article Section</a>
→ /article.html?id=123#section-2

<!-- URL encoding special characters --&gt;
<a href="search.html?q=html%20%26%20css">Search "html & css"</a>
→ /search.html?q=html%20%26%20css

Common URL Encoding:

CharacterEncodedUsage
Space%20 or +hello%20world
&%26tom%26jerry
=%3D2%2B2%3D4
?%3Fwhat%3F
/%2Fhttp%3A%2F%2F

Common URL Mistakes

❌ Mistake #1: Missing Protocol

HTML
<a href="www.example.com">Link</a>
<!-- Treated as relative path: /www.example.com --&gt;
HTML
<!-- ✅ Correct --&gt;
<a href="https://www.example.com">Link</a>

❌ Mistake #2: Wrong Relative Path

HTML
<!-- From /blog/post.html, trying to link to /about.html --&gt;
<a href="about.html">About</a>
<!-- Goes to /blog/about.html (wrong!) --&gt;
HTML
<!-- ✅ Correct options --&gt;
<a href="../about.html">About</a>  <!-- Up one level --&gt;
<a href="/about.html">About</a>     <!-- Root-relative --&gt;

❌ Mistake #3: Spaces in URLs

HTML
<a href="my page.html">Link</a>
<!-- Breaks! Spaces must be encoded --&gt;
HTML
<!-- ✅ Correct --&gt;
<a href="my-page.html">Link</a>        <!-- Use hyphens --&gt;
<a href="my%20page.html">Link</a>      <!-- Or encode --&gt;

❌ Mistake #4: Mixed Protocols

HTML
<!-- On HTTPS site, linking to HTTP --&gt;
<img src="http://example.com/image.jpg">
<!-- Mixed content warning! --&gt;
HTML
<!-- ✅ Correct --&gt;
<img src="https://example.com/image.jpg">  <!-- HTTPS --&gt;
<img src="//example.com/image.jpg">        <!-- Protocol-relative --&gt;

Best Practices

✅ Do

  • Use root-relative paths for internal navigation
  • Use absolute URLs for external links
  • Encode special characters in URLs
  • Use lowercase for consistency
  • Use hyphens instead of spaces
  • Test all links after moving files
  • Use trailing slashes consistently

❌ Don't

  • Forget protocol on external links
  • Use spaces in URLs
  • Mix relative path levels unnecessarily
  • Use absolute URLs for internal links
  • Include sensitive data in query strings
  • Create overly long URLs
  • Use special characters without encoding

Quick Reference

Path TypeExampleStarts WithUse Case
Absolutehttps://example.com/pagehttp:// or https://External links
Root-relative/about/Internal navigation
Document-relativepage.htmlFilenameSame directory
Parent../page.html../Up one level
Current./page.html./Current directory
Protocol-relative//example.com/page//Inherit protocol

What's Next?

Now that you understand URL structure and paths, let's learn how to build effective navigation menus.