Search code examples
htmlurlpathrelative-pathabsolute-path

Absolute vs relative links in HTML


I have been looking for a complete definitive explanation of absolute vs relative links in HTML.

  1. <a href="http://www.example.com/page.html">link page</a>
  2. <a href="//page2.html">link page</a>
  3. <a href="/page3.html">link page</a>
  4. <a href="./page4.html">link page</a>
  5. <a href="../page5.html">link page</a>

So in the above list,

  1. is an absolute link
  2. is relative to site root with either http or https
  3. is relative to site root?
  4. is in the current directory?
  5. is up a directory?

Solution

  • This is not HTML. This is absolute or relative paths in all aspects of a operating system.

    1. Yes, there's no interpretation. Is a link without SSL (https)
    2. Yes. It switches automatically to http if user visit the web in http, and https if user visit the web in https.
    3. Yes. It links to the //domain/thelink.ext
    4. Yes. As in a operating system, ./ is the current directory. Many times you must to specify this to ensure is the current.
    5. Yes. As in a operating system, ../ changes to parent directory. You can make nesting and ../../../ will mean that up 3 parent directories.