HTML Paths Guide

The Map to Your Website's Resources

File paths are one of those things that look trivial until you get one wrong and spend twenty minutes staring at a broken image icon wondering why the file isn't there when you can clearly see it in your folder. Every link and every embedded image requires a path - an address the browser follows to find that resource - and the wrong path is the browser equivalent of getting told to turn left when you should turn right. There are two kinds of paths: absolute, which include the full URL including the domain, and relative, which describe how to get from the current file to the target file. Understanding when to use each one saves you from broken links and missing assets across the lifetime of a project.

What is a Path?

A path is the string of text that tells the browser where a file lives - which folder it's in, what it's called, how to get there from wherever the browser currently is. Paths show up in the src attribute of img tags, the href attribute of anchor and link tags, and anywhere else you're referencing an external file. To make the examples concrete, here's the folder structure we'll use throughout this guide.

text
1my-website/
2├── index.html
3├── about.html
4├── styles/
5│   └── main.css
6├── images/
7│   ├── logo.png
8│   └── team-photo.jpg
9└── blog/
10    ├── index.html
11    └── first-post.html

1. Absolute Paths

An absolute path includes the full URL - protocol, domain, and file path - and points to a specific location on the internet regardless of where the current document is. If you move your website to a different domain or reorganize your folder structure, absolute paths to external resources stay correct because they don't depend on context. Use absolute paths for links to other websites, resources loaded from CDNs, and any situation where you need the full URL because context-independence matters. The one trap with absolute paths to your own site's internal resources is that they break if you move to a new domain - every internal link needs updating, which is why relative paths are preferred for within-site navigation.

html
1<!-- Linking to an external website -->
2<a href="https://www.wikipedia.org">Visit Wikipedia</a>
3
4<!-- Embedding an image from an external source -->
5<!-- Note: hotlinking someone else's image steals their bandwidth -->
6<img src="https://other-site.com/images/their-logo.jpg" alt="Their logo">
7
8<!-- Linking to your own site from an external platform -->
9<a href="https://www.yourwebsite.com/">Home</a>
10
11<!-- Loading a CSS file from a CDN -->
12<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">

A Note on Hotlinking

  • Bandwidth cost - Linking directly to an image hosted on someone else's server uses their bandwidth without permission - every time your page loads, their server pays the cost.
  • No control - You have no control over the image. The owner can delete it, change it, or replace it with something completely different, and your page has no warning.
  • Performance - The browser has to make a separate request to a different server, which adds latency and makes your page load slightly slower.

2. Relative Paths

A relative path describes how to get from the current file's location to the target file, without including the domain. The browser calculates the full URL by combining where the current page is with the relative path you provided. This is the standard approach for linking between pages and assets within your own website, and it's more practical than absolute paths for internal links because the whole site moves together - if you rename your domain or run it on localhost during development, all the relative paths still work without changing anything.

html
1<!-- From index.html in the root folder -->
2<a href="about.html">About Us</a>             <!-- same folder -->
3<img src="images/logo.png" alt="Logo">          <!-- subfolder -->
4<a href="blog/index.html">Visit our Blog</a>   <!-- subfolder -->
5
6<!-- From blog/first-post.html -->
7<a href="index.html">All Blog Posts</a>         <!-- same folder -->
8<a href="../index.html">Go Back Home</a>        <!-- one level up -->
9<img src="../images/logo.png" alt="Logo">       <!-- one level up, then into images -->

Relative Path Syntax

  • filename.ext - No prefix means the file is in the same folder as the current HTML file. The simplest case.
  • folder/filename.ext - The file is inside a subfolder of the current folder. You can chain these: images/2024/photo.jpg.
  • ../filename.ext - Go up one level to the parent folder, then look for the file there. Use ../../ to go up two levels.
  • /filename.ext - Root-relative path - starts from the website's root regardless of where the current file is. Useful but can break if the site isn't hosted at the root of the domain.

Absolute vs Relative Paths: Quick Comparison

The practical decision is usually simple: relative paths for everything within your own site, absolute paths for everything outside it. The one exception is CDN-hosted libraries and fonts, which always need absolute paths since they're hosted externally.

html
1<!--
2Feature           | Absolute Paths              | Relative Paths
3------------------|-----------------------------|---------------------------
4Definition        | Full URL to the resource    | Path from current file
5Starts with       | http://, https://, //       | filename, ./, or ../
6Best for          | External sites, CDNs        | Internal links within site
7Portability       | Breaks if domain changes    | Survives domain changes
8Dev environment   | Needs correct domain        | Works on localhost
9Example           | https://site.com/logo.png   | ../images/logo.png
10-->

Best Practices

Use relative paths for all internal links and assets - this is the most important rule and it's the one that makes moving your site from development to production painless. When you're working locally on your computer and then upload to a live server, relative paths keep working without any changes. Absolute internal paths would all need updating if your domain changes. Be consistent with whether you use ./ or just the filename for same-folder references - both work, but mixing them makes the code harder to read. After building a page, click every link you added to verify the paths are right. Broken links are one of the more embarrassing things to discover after launch and relative path miscalculations are the most common cause.

Key Recommendations

  • Relative for internal, absolute for external - This simple rule covers 95% of decisions. Internal pages and assets use relative paths. External websites and CDN resources use absolute paths.
  • Be consistent with notation - Choose whether to use ./ or nothing for same-folder files and stick with it. Both work but mixing them reduces readability.
  • Test every link - Click every link after adding it, especially after folder reorganization. Relative path bugs are invisible until someone visits the broken page.
  • Root-relative paths with caution - Paths starting with / work from the site root and are predictable, but they break if your site is hosted in a subdirectory rather than the domain root.

Paths: A Simple Rule That Saves Hours

The mental model that makes paths click is thinking of them as directions from where you are right now. An absolute path is like a full postal address - it works from anywhere because it has all the information needed. A relative path is like saying turn left at the corner - it only works if you know where you're starting from, and it works perfectly as long as the starting point is right. Relative paths for internal links, absolute for everything external - once that habit is in place you'll rarely have a broken link that wasn't also a typo

Frequently Asked Questions

My image isn't showing up. What did I do wrong?

Almost always a path problem. Check these things in order: first, the spelling of the filename and folder name - is it logo.png or Logo.png? Case sensitivity matters on Linux servers even if it doesn't matter on your Windows or Mac computer during development. Second, is the file actually in the folder you think it is? Open your file manager and physically look. Third, right-click the broken image in your browser and select Open image in new tab - the URL in the address bar shows you exactly where the browser is trying to find the file, which usually makes the mismatch obvious immediately.

What is a protocol-relative URL?

A URL that starts with // instead of http:// or https://, like //cdn.com/library.js. It tells the browser to use whichever protocol the current page is using. This was popular for avoiding mixed-content warnings when some pages were HTTP and some HTTPS, but since the modern web is almost entirely HTTPS now, the simpler approach is to always use https:// and not need the protocol-relative syntax at all.

Should I use hyphens or underscores in file and folder names?

Hyphens. Search engines treat a hyphen as a word separator, so my-photo.jpg is read as two words: my and photo. Underscores are treated as word connectors, so my_photo.jpg is read as one word: my_photo. Hyphens are better for SEO and also more readable in URLs. Also avoid spaces entirely - they get encoded as %20 in URLs which looks messy and can cause issues in some environments.

When would I use a root-relative path starting with /?

Root-relative paths like /images/logo.png are useful when you have deeply nested pages and want paths that are consistent regardless of how deep you are. Instead of ../../../images/logo.png from a file several folders deep, /images/logo.png always works from anywhere on the site. The downside is they only work correctly when the site is hosted at the root of the domain. If your site is at yoursite.com/blog/ as a subdirectory, /images/logo.png would look for yoursite.com/images/logo.png instead of yoursite.com/blog/images/logo.png.

Why do paths work on my computer but break on the live server?

The most common reason is case sensitivity. Windows and macOS file systems are case-insensitive, so Images/Logo.png and images/logo.png point to the same file on your computer. Linux servers (which run most websites) are case-sensitive, so those are two completely different paths. If your paths work locally but break on the server, go through your HTML and make sure every folder and filename in your paths exactly matches the actual capitalization of those files and folders on the server.