Absolute and Relative Links in HTML
Absolute and Relative Links in HTML
When creating links in HTML, you can use absolute or relative URLs in the href attribute.
1. Absolute Links
An absolute link contains the full URL to a resource, including the protocol (http:// or
https://), domain, and path. These links are used when pointing to external websites or
when specifying a complete URL.
Syntax:
Example:
Pros:
Always points to the exact location, no matter where the current file is located.
Useful for linking to external websites.
Cons:
2. Relative Links
A relative link provides the path to a resource relative to the location of the current
document. These are often used for internal links within the same website or project.
Syntax:
Example:
/index.html
/about/page.html
/contact/form.html
Same directory:
<a href="page.html">Go to Page</a>
Parent directory:
<a href="../index.html">Go to Home</a>
Child directory:
<a href="about/page.html">Go to About</a>
1. Same Folder:
2. <a href="file.html">Link to file in the same folder</a>
3. Parent Folder:
4. <a href="../file.html">Link to file in the parent folder</a>
5. Child Folder:
6. <a href="folder/file.html">Link to file in a subfolder</a>
7. Root Folder (starts from the root of the domain):
8. <a href="/images/photo.jpg">Link to image in root/images</a>
Pros:
Easier to manage and update when working within the same website.
Shorter and more readable.
Works well when deploying the same structure across different domains (e.g., staging
and production).
Cons:
Comparison Table