HTML File Paths
Whenever HTML refers to another file — an image, a stylesheet, a script, another page — it needs a path telling the browser where that file lives. Getting paths right is one of the first practical hurdles of building a real website, so it is worth understanding exactly how they are resolved.
Paths appear in attributes such as:
| Element | Attribute |
|---|---|
<a> |
href |
<img>, <script>, <iframe>, <video>, <audio>, <source> |
src |
<link> |
href |
<form> |
action |
Absolute vs. relative paths
An absolute URL contains everything: protocol, domain and path.
<img src="https://example.com/images/logo.png" alt="Logo">A relative path is resolved starting from the location of the current page. It is shorter and keeps working when you move the whole site to another domain or open it from your disk.
Imagine this folder structure, with the current page being blog/post.html:
my-site/
├── index.html
├── logo.png
├── css/
│ └── style.css
└── blog/
├── post.html ← we are here
├── photo.jpg
└── img/
└── cat.jpgPath written in blog/post.html |
Points to |
|---|---|
photo.jpg |
blog/photo.jpg (same folder) |
img/cat.jpg |
blog/img/cat.jpg (subfolder) |
./photo.jpg |
blog/photo.jpg (./ means "this folder") |
../logo.png |
my-site/logo.png (../ goes up one folder) |
../css/style.css |
my-site/css/style.css |
/logo.png |
logo.png at the root of the web server |
Note: A path starting with
/is root-relative: it starts from the top of the website, not the top of your hard disk. If you open files directly withfile://,/logo.pngmeans the root of your drive, which is rarely what you want. Prefer paths without the leading slash while learning.
Seeing how a browser resolves paths
JavaScript's URL class resolves a relative path against a base URL exactly like the browser does. Edit the paths in the list and run again to experiment.
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
<p>Current page: <code id="base"></code></p>
<table border="1" cellpadding="6" style="border-collapse: collapse">
<thead><tr><th>Written path</th><th>Resolves to</th></tr></thead>
<tbody id="rows"></tbody>
</table>
<script>
const base = "https://my-site.example/blog/post.html";
const paths = ["photo.jpg", "img/cat.jpg", "./photo.jpg",
"../logo.png", "../css/style.css", "/logo.png",
"../../too-far.png"];
document.getElementById("base").textContent = base;
const rows = document.getElementById("rows");
for (const p of paths) {
const tr = document.createElement("tr");
tr.innerHTML = "<td><code></code></td><td><code></code></td>";
tr.children[0].firstChild.textContent = p;
tr.children[1].firstChild.textContent = new URL(p, base).href;
rows.appendChild(tr);
}
</script>
</body>
</html>Notice that ../../too-far.png cannot climb above the root — the browser just stops at the top.
Paths in links and images
The playground below has no real files around it, so we use data URIs for the images: the image data is written into the path itself. On a real site you would write src="img/star.svg".
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
<p>An image from a data URI (works anywhere, no file needed):</p>
<img alt="Orange star" width="80" height="80"
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23e34c26' d='M12 2l3 7h7l-5.5 4.5 2 7.5L12 17l-6.5 4 2-7.5L2 9h7z'/%3E%3C/svg%3E">
<p>A relative image path that does not exist here shows its <code>alt</code> text:</p>
<img src="images/missing.png" alt="(missing.png could not be found)">
</body>
</html>The second image demonstrates what a broken path looks like: the browser shows the alt text (or a broken-image icon). When an image or stylesheet "doesn't load", the path is the first thing to check.
Best practices
- Use relative paths for files inside your own site; use absolute URLs only for other sites.
- Use lowercase file names without spaces:
team-photo.jpg, notTeam Photo.JPG. Many web servers are case-sensitive, soLogo.pngandlogo.pngare different files. - If a name must contain a space, it becomes
%20in a URL — another reason to avoid them. - Always use forward slashes
/, even on Windows. - Open the browser DevTools Network tab to see which file requests fail with a 404 error.
Tip: Keep a predictable structure:
css/,js/,img/folders next toindex.html. Then every page at the top level can usecss/style.css, and pages one level down use../css/style.css.
Test yourself
In shop/cart.html, which path points to index.html in the site's top folder?
index.html./index.html../index.htmlshop/index.html
What does a path starting with /, like /img/logo.png, mean?
- Relative to the current folder
- Relative to the root of the website
- Relative to the user's home folder
- It is invalid
Why are relative paths preferred for files on your own site?
- They load faster over HTTPS
- They keep working if the site moves to another domain or folder
- Browsers cache them forever
- Absolute paths are not allowed in
src