Skip to main content

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).


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>

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 typeWhat it looks likeWhere it is usedExample
AbsoluteFull URL with a domainFor linking to another sitehttps://example.com/about.html
RelativePath within the siteFor internal pagesabout.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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.