URLs tell the browser where to find files like web pages or images. Absolute and relative URLs help link to these files in different ways.
Absolute vs relative URLs in HTML
Absolute URL example: <a href="https://example.com/page.html">Link</a> Relative URL example: <a href="page.html">Link</a>
An absolute URL includes the full web address starting with http:// or https://.
A relative URL points to a file based on the current page's location, without the full address.
<a href="https://www.google.com">Google</a>
about.html in the same folder.<a href="about.html">About Us</a>
contact.html.<a href="../contact.html">Contact</a>
<a href="/images/logo.png">Logo</a>
This page shows two links: one uses an absolute URL to go to Google, the other uses a relative URL to go to an About page in the same folder.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Absolute vs Relative URLs</title> </head> <body> <h1>Links with Absolute and Relative URLs</h1> <p>Absolute URL link to Google:</p> <a href="https://www.google.com" target="_blank" rel="noopener noreferrer">Go to Google</a> <p>Relative URL link to About page (assumed in same folder):</p> <a href="about.html">About Us</a> </body> </html>
Absolute URLs are best when linking outside your website.
Relative URLs keep your links working if you move your site to a new domain.
Use target="_blank" and rel="noopener noreferrer" for safe new tab links.
Absolute URLs include the full web address and work anywhere.
Relative URLs depend on the current page location and are good for internal links.
Choosing the right URL type helps keep your website links working well.
Practice
absolute URL?Solution
Step 1: Understand what an absolute URL is
An absolute URL includes the full address: protocol (http/https), domain, and path.Step 2: Compare options to definition
A full web address including protocol and domain, likehttps://example.com/pageshows a full address with protocol and domain, matching the absolute URL definition.Final Answer:
A full web address including protocol and domain, like https://example.com/page -> Option BQuick Check:
Absolute URL = full web address [OK]
- Confusing relative URLs with absolute URLs
- Thinking a URL starting with / is absolute
- Believing file names alone are absolute URLs
style.css in the same folder?Solution
Step 1: Understand relative URLs in the same folder
A relative URL to a file in the same folder can be just the file name.Step 2: Check options for correct relative URL
style.cssuses juststyle.css, which correctly points to the file in the same folder.Final Answer:
style.css -> Option AQuick Check:
Relative URL same folder = file name only [OK]
- Using a leading slash which points to root folder
- Using full URLs when relative is needed
- Using ../ which points to parent folder
https://example.com/blog/post1.html, what will the browser load for the relative URL ../images/pic.jpg?Solution
Step 1: Understand the meaning of
..in relative URLs..means go up one folder from the current page location.Step 2: Apply to current URL
Current URL folder is/blog/. Going up one folder leads to/. Then addingimages/pic.jpggives/images/pic.jpg.Final Answer:
https://example.com/images/pic.jpg -> Option AQuick Check:
..moves up one folder [OK]
.. means go up one folder in URL path [OK]- Thinking
..stays in the same folder - Appending path without going up folder
- Confusing relative with absolute URLs
<a href="images/photo.jpg">Photo</a> on the page https://example.com/about/index.html, but the image does not load. What is the likely problem?Solution
Step 1: Understand relative URL resolution
The relative URLimages/photo.jpglooks for animagesfolder inside the current folder/about/.Step 2: Check folder existence
If theimagesfolder is not inside/about/, the link will break. This is the likely cause.Final Answer:
The relative URL points to a folder that does not exist relative to current page -> Option DQuick Check:
Relative URLs depend on current folder [OK]
- Assuming relative URLs start from root without slash
- Using absolute URLs unnecessarily
- Ignoring folder structure when linking
https://example.com/products/item.html. Which URL should you use to ensure the link works correctly regardless of the current page location?Solution
Step 1: Understand absolute vs relative URL behavior
Relative URLs depend on current page location, so they may break if page moves.Step 2: Choose URL that works anywhere
An absolute URL with full domain always works regardless of page location.Final Answer:
https://example.com/index.html -> Option CQuick Check:
Absolute URL works anywhere [OK]
- Using relative URLs that break if page moves
- Assuming leading slash always works for all pages
- Not including protocol and domain for absolute URLs
Ready to prove it?
Pass the test and this pattern moves unknown
