A lightly contentious issue for many web developers revolves around what kind of URL scheme one should use when referencing resources and links within the same site. For example, you can use a fully-qualified absolute URL:
<img src="https://www.kirupa.com/images/foo.png" width=100 height=100>
You can also use the more compact relative URL:
<img src="../images/foo.png" width=100 height=100>
Now, this article isn't about picking sides and telling you why one is superior to the other. The reason is that you need to use both absolute and relative URLs as part of creating your sites and apps. Instead, this article will show you when to use which.
Onwards!
When to Use Absolute URLs
A fully qualified URL such as https://www.kirupa.com/images/foo.png names its scheme, host, and path. Moving the page to a different host won't change that destination. You may also see //www.kirupa.com/images/foo.png, which is a scheme-relative reference. It inherits the base URL's scheme, so it is not completely independent of context. Here are two situations where naming the destination explicitly can help.
You are Pointing to Content Outside of Your Site
If the content lives on another host, our reference needs to name that host. A fully qualified HTTPS URL makes the intended destination explicit:

A path such as ../images/foo.png cannot climb into another domain or subdomain. It stays on the base URL's origin. A reference beginning with // can name another host, but still inherits the scheme. That distinction matters when we move the page between HTTP and HTTPS environments.
Your References Will Change Depending on Context
Some of your HTML will find itself merged with other pieces of HTML and served on a variety of different pages in your site. While this may sound like a bizarre scenario, this is exactly what happens when you employ server side includes or site-wide templates to make your content more modular and reusable.
URLs inside these "template" pages cannot assume that relative paths will still work when they find themselves in different locations. For example, this is what the markup for something on this site that gets server side included looks like:
<div id="bookBannerContent">
<p class="centerImageNoBorder">
<a href="https://www.kirupa.com/book/animation_in_html_css_and_javascript.htm">
<img id="bookAd" src="https://www.kirupa.com/supporter/animation_book_right_banner.png" width="231" height="251"></a>
</p>
<ul>
<li>
<a href="https://www.amazon.com/dp/1482758954" style="background-image: url('https://www.kirupa.com/mini_icons/entypo/shopping_20x20.png');">
Buy on Amazon.com</a>
</li>
</ul>
</div>
Notice that the references in this example name their destinations explicitly, even for content on kirupa.com. The browser receives the completed page and resolves references there; it doesn't know which server-side template supplied a piece of HTML. A root-relative path can also work for shared same-origin assets, provided it includes the correct deployment path.
Use Relative URLs Whenever Possible
Outside of the two cases you saw earlier, for any other kinds of references, use relative URLs. There is no point in cluttering your markup with unnecessary characters when your browser, search engines, and everything else on the planet knows how to work with relative URLs and resolve things properly.
If you do some research on the net, you'll see some other reasons people provide for going with relative URLs. Some of them include:
- Relative URLs can move with a site between staging and production when the directory structure and base URL behavior stay consistent. A fixed production URL keeps pointing at production, even from a staging page.
- Relative spelling does not remove DNS work. The browser first resolves a relative reference to a complete URL. References that resolve to the same URL have the same network destination; DNS caches and existing connections affect whether a lookup is needed.
- A shorter reference can reduce the source text a little, but that is not evidence of a meaningful loading-speed improvement. Measure the actual transferred content and requests before making a performance claim.
There are a few other reasons, but I wouldn't mention them in public. I feel a bit ashamed about listing the three you see here, for I'm not sure how relevant they even are these days.
Conclusion
Neither absolute URLs nor relative URLs are good enough to use exclusively on their own. Use the right one for the right situation. To point to external content or to reference things from inside a site-wide template or server-side included page, I use absolute URLs. For everything else, I use relative URLs.
Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇

