What is the difference between absolute and relative links?
Absolute and relative links differ in how they specify the path to a resource (a page, file, image, and so on).
Absolute link
An absolute link contains the full path to the resource: including the protocol (http:// or https://), the domain, and, if needed, folders and files.
Example:
html
<a href="https://example.com/images/photo.jpg">View the photo</a>Features:
- Works always and everywhere, regardless of where the HTML file is located.
- Used when you need to go to another site or specify the page's exact address.
- Downside: if the site or domain changes, the link stops working.
Example of linking between sites:
html
<a href="https://wikipedia.org/wiki/HTML">More on Wikipedia</a>Relative link
A relative link specifies the path relative to the current file or directory. It does not contain a domain or protocol.
Examples:
html
<a href="about.html">About the company</a> <!-- File in the same folder -->
<a href="folder/page.html">More details</a> <!-- Nested folder -->
<a href="../index.html">To the homepage</a> <!-- One level up -->Features:
- Works only within a single site.
- Convenient during development, when moving the site, the links do not need to change.
- The browser fills in the current domain on its own.
Key difference:
| Link type | What it looks like | Where it is used | Example |
|---|---|---|---|
| Absolute | Full URL with a domain | For linking to another site | https://example.com/about.html |
| Relative | Path within the site | For internal pages | about.html or ../index.html |
Conclusion: Absolute links are like a full address with a city and country. Relative ones are like saying "turn the corner" within a single neighborhood.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.