A relative link is a link that specifies the path to a file relative to the current page location:
page.html 📁folder/page.html 📂../page.html ⬆️/page.html 🏠#section ⚓?param=value 🔗<!-- Main types of relative links -->
<a href="about.html">About</a>
<a href="pages/contact.html">Contact</a>
<a href="../index.html">Home</a>
<a href="/products.html">Products</a>Relative links are like giving directions: “go straight”, “turn left” instead of a full address! They specify the path relative to the current location. 🧭
<!-- Relative link -->
<a href="relative/path">Link</a>
<!-- Absolute link for comparison -->
<a href="https://example.com/full/path">Link</a>Simply specify the filename:
<!-- Folder structure:
/current-folder/
├── index.html (current page)
└── about.html
-->
<a href="about.html">About</a>
<a href="contact.html">Contact</a>
<a href="services.html">Services</a>Specify folder and file with slash:
<!-- Folder structure:
/current-folder/
├── index.html (current page)
└── pages/
├── about.html
└── contact.html
-->
<a href="pages/about.html">About</a>
<a href="images/photo.jpg">Photo</a>
<a href="css/style.css">Styles</a>Use ../ to go up one level:
<!-- Folder structure:
/root/
├── index.html
└── subfolder/
└── page.html (current page)
-->
<a href="../index.html">Home</a>
<a href="../about.html">About</a>
<a href="../../other.html">Two levels up</a>Start with / for path from site root:
<!-- Always from site root -->
<a href="/index.html">Home</a>
<a href="/products/catalog.html">Catalog</a>
<a href="/images/logo.png">Logo</a>Links to page sections:
<!-- On current page -->
<a href="#header">To header</a>
<a href="#footer">To footer</a>
<!-- On another page -->
<a href="about.html#team">Team</a>
<a href="../contact.html#form">Form</a>Add parameters to links:
<!-- Parameters for current page -->
<a href="?category=books">Books</a>
<a href="?sort=price&order=asc">By price</a>
<!-- Parameters for another page -->
<a href="catalog.html?page=2">Page 2</a><nav>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/products">Products</a>
<a href="/contact">Contact</a>
</nav><div class="breadcrumbs">
<a href="/">Home</a> /
<a href="../catalog.html">Catalog</a> /
<span>Product</span>
</div><div class="gallery">
<img src="images/photo1.jpg" alt="Photo 1">
<img src="images/photo2.jpg" alt="Photo 2">
<img src="../shared/logo.png" alt="Logo">
</div><head>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="../shared/common.css">
<script src="js/script.js"></script>
</head>| Relative | Absolute |
|---|---|
about.html | https://site.com/about.html |
../index.html | https://site.com/index.html |
/products | https://site.com/products |
| Depend on current path | Full address |
<!-- Portable - works everywhere -->
<a href="contact.html">Contact</a>
<!-- Domain-bound -->
<a href="https://mysite.com/contact.html">Contact</a>../ 🌀<!-- Good: main navigation from root -->
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
<!-- Good: related files relatively -->
<link rel="stylesheet" href="css/page.css">
<img src="images/banner.jpg" alt="Banner">❌ Wrong:
<!-- Extra slash at start -->
<a href="/folder//page.html">Link</a>
<!-- Wrong number of ../ -->
<a href="../../folder/page.html">Link</a>✅ Correct:
<a href="/folder/page.html">Link</a>
<a href="../folder/page.html">Link</a>Relative links are the foundation of HTML navigation:
file.htmlfolder/file.html../file.html/file.htmlUse them to create flexible and portable site structure! 🎯