Explain it simply
Explain it simply
A file path is a set of directions. "https://example.com/photo.jpg" is a full street address: it points to the same place no matter where you read it. "images/photo.jpg" is more like "from where I am standing, go into the images room", so where you end up depends on where you started.
In an HTML page, the starting point is that page's own URL. Every time the browser meets a src or href, it combines the path with the page URL, works out one full URL, and requests that address from the server. Images, CSS, JavaScript and links all follow this single rule.
Problem → Why → Solution
The problem: it works on my laptop, then images vanish after deploy
Aye builds a portfolio site on a Windows laptop, double-clicks index.html, and everything looks perfect. After uploading it to a host, the logo is gone, the CSS does not load, and the profile photo is a broken-image icon. Three different mistakes hide in the code, and all three went unnoticed because they happen to work on the laptop.
<!-- Deployed at https://aye.github.io/portfolio/ -->
<link rel="stylesheet" href="/css/style.css">
<img src="C:\Users\Aye\Desktop\portfolio\images\logo.png" alt="Aye's logo">
<!-- the real file on disk is images/profile.jpg -->
<img src="Images/Profile.JPG" alt="Aye smiling at a desk">Why did it work on the laptop?
A path starting with C:\ points into Aye's own hard disk; nobody else's computer has that folder. Windows treats file names case-insensitively, so Images/Profile.JPG still finds images/profile.jpg, but the Linux servers most hosts run treat them as two different files. And /css/style.css starts from the site root, skipping the /portfolio/ subfolder, so the browser requests https://aye.github.io/css/style.css instead.
Core concept: every path is resolved against the page's URL
When the browser loads a page, it remembers that page's URL as the base URL. Every src="..." or href="..." value is resolved against this base into one full URL. The process is the same whether the target is HTML, CSS or JavaScript: from the browser's point of view everything is a URL, never a folder on your computer.
| Kind of path | Example | Starts from |
|---|---|---|
| Absolute URL | https://cdn.example.net/a.png | Nowhere: the address is already complete |
| Root-relative | /images/logo.png | The root of the current site's domain |
| Document-relative | images/a.png, ../css/site.css | The folder that contains the current page |
Resolving a document-relative path works like this: drop everything after the last slash of the page URL and you are left with the page's folder. For https://example.com/blog/post.html that folder is /blog/. images/a.png becomes /blog/images/a.png, ./ means "this folder", and ../ climbs one folder up, so ../css/site.css becomes /css/site.css. You cannot climb above the root, so extra ../ segments simply stop at the domain root.
A trailing slash can change the result. https://example.com/blog/ is inside the blog folder, while https://example.com/blog (no slash) is treated as a file called "blog" in the root folder, so images/a.png resolves to /images/a.png.
URLs are not file-system paths. The separator is always a forward slash (/): browsers silently turn a backslash in an http(s) URL into /, but that is invalid per the spec and other tools may not understand it. Paths are case-sensitive on most servers, and characters such as spaces must be percent-encoded as %20. Naming files in lowercase with hyphens and no spaces avoids all of this.
One element can change the starting point for the whole page: <base href="..."> in the head replaces the page URL as the base for every relative path. It is rarely needed, and it surprises people, because adding it silently re-targets every relative link and image on the page.
- Start from the page's own URL
- Drop the file name to get its folder
- Follow the path; ../ climbs one level
- Request the resulting full URL
A leading / skips the folder and starts at the domain root; a leading https:// needs no walking at all.
General pattern
<!-- absolute: scheme + host + path -->
<script src="https://host.example/path/file.js"></script>
<!-- root-relative: from the domain root -->
<link rel="stylesheet" href="/folder/file.css">
<!-- document-relative: from this page's folder -->
<img src="folder/file.ext" alt="Description">
<a href="../other-page.html">Up one folder</a>The first characters of a path decide where it starts: https:// means a complete address, / means the domain root, anything else means the page's folder. The rule is identical for src, href and every other attribute that takes a URL.
Example: three images, three kinds of path
This code has three img elements, each using a different kind of path. Assume the page is published at https://example.com/blog/post.html and predict which URL the browser requests for each image.
<img src="https://example.com/photo.jpg" alt="From another website">
<img src="/images/logo.png" alt="Logo from root images folder">
<img src="images/profile.jpg" alt="Profile from current folder">Code breakdown
- Absolute URL
<img src="https://example.com/photo.jpg" alt="From another website">The scheme (https:) and host are already there, so wherever the page lives, the browser requests exactly https://example.com/photo.jpg. Use this form for files on other domains and CDNs.
- Root-relative
<img src="/images/logo.png" alt="Logo from root images folder">The leading / ignores the /blog/ folder and starts at the domain root, giving https://example.com/images/logo.png. Moving the page to another folder does not change this URL.
- Document-relative
<img src="images/profile.jpg" alt="Profile from current folder">It starts from the page's folder, /blog/, so the browser requests https://example.com/blog/images/profile.jpg. Move the page to another folder and the target moves with it.
- alt
alt="Profile from current folder"When a path is wrong and the file is not found, the browser shows a broken-image icon with the alt text. Seeing alt text on the page is often your first clue that a path is broken.
If the page is at https://example.com/blog/post.html, the browser requests these three URLs:
1. https://example.com/photo.jpg
2. https://example.com/images/logo.png
3. https://example.com/blog/images/profile.jpg
Images whose file exists are shown; a missing one shows a broken-image icon and its alt text.Tracing paths through a real folder tree
Once a site has pages at different folder depths, it helps to picture the tree when writing relative paths. In the tree below, blog/post.html reaches files in the folder above it with ../.
my-site served at https://example.com/
--------------------------------------
my-site/ -> https://example.com/
|-- index.html
|-- css/
| `-- site.css
|-- images/
| `-- logo.png
`-- blog/
|-- post.html (page we are writing)
`-- images/
`-- profile.jpg| Written in blog/post.html | URL the browser requests |
|---|---|
| images/profile.jpg | https://example.com/blog/images/profile.jpg |
| ../images/logo.png | https://example.com/images/logo.png |
| ../css/site.css | https://example.com/css/site.css |
| ../index.html | https://example.com/index.html |
url() in CSS starts from the stylesheet
If css/site.css says background-image: url("../images/logo.png"), the relative path resolves from the stylesheet's URL (/css/site.css), not from the HTML page, giving /images/logo.png. That is why the result is the same whichever page loads the CSS.
In real projects
Where this shows up in real projects
Most frameworks keep static files in one folder served from the site root; in Next.js, for example, public/logo.png is referenced root-relatively as /logo.png. On large sites with pages at many depths, root-relative paths are more stable because nobody has to count ../../ segments. But when a site is deployed under a subpath such as https://user.github.io/repo/, a leading / skips repo, so you configure the build tool's base path setting (for example Vite's base option).
Absolute URLs are for fonts, libraries and images on a CDN, and for links that must work wherever they are opened, such as in an email template. If you link your own pages with absolute https://mysite.com/... URLs, testing on a staging server keeps jumping to production, so prefer relative or root-relative paths for internal links. Teams also agree on lowercase, hyphenated file names and run a link checker in CI to catch broken links and missing assets before deploying.
Common mistakes
Common mistakes
1. Putting a local drive path in src
Pasting C:\Users\Aye\site\logo.png or file:///C:/... from your editor into src. The browser reads C: as a URL scheme and cannot load it, and even if it appeared on your machine it never will for anyone else. Use a path relative to the project folder, such as images/logo.png.
2. Letter case that does not match the file name
The file on disk is hero.jpg but the HTML says Hero.JPG. It works with the default settings of Windows and macOS, then returns 404 on a Linux server. Name every file in lowercase and write it exactly the same way in the HTML.
3. Root-relative paths under a subpath
When a site is hosted under a subfolder such as /portfolio/, writing /css/style.css skips /portfolio/ and returns 404. Opening index.html by double-clicking makes / point at the drive root (file:///C:/) too. In these situations use a relative path such as css/style.css, or the build tool's base setting.
Best practices
Best practices
- Name files and folders in lowercase with hyphens and no spaces (my-photo.jpg).
- Pick one strategy for internal links across the project, relative or root-relative, and do not mix them.
- Test through a local web server (for example VS Code Live Server) instead of file:// so paths behave as they will in production.
Mini challenge
Mini challenge: wire up a blog post page
Using the folder tree above, write blog/post.html. Assume it is published at https://example.com/blog/post.html and connect every file with a correct path.
- Link css/site.css with a relative path
- Show images/logo.png (in the root folder) with a relative path
- Show blog/images/profile.jpg; every img has alt text
- Add a link back to the home page (index.html) and an absolute link to https://developer.mozilla.org/
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My first post</title>
<!-- TODO: link ../ up to css/site.css -->
</head>
<body>
<!-- TODO: logo from the root images folder -->
<!-- TODO: profile photo from blog/images -->
<!-- TODO: nav links: home page (relative) and MDN (absolute) -->
</body>
</html>Expected: The browser requests https://example.com/css/site.css, https://example.com/images/logo.png and https://example.com/blog/images/profile.jpg. The "Home" link points to https://example.com/index.html and the "MDN Web Docs" link to https://developer.mozilla.org/.
Show hint
post.html sits in blog/, so every file in the root folder needs a leading ../. blog/images/ is in the same folder as post.html, so you can start straight with images/.
Show solution
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My first post</title>
<link rel="stylesheet" href="../css/site.css">
</head>
<body>
<header>
<img src="../images/logo.png" alt="Example Blog logo" width="120" height="40">
<nav>
<a href="../index.html">Home</a>
<a href="https://developer.mozilla.org/">MDN Web Docs</a>
</nav>
</header>
<main>
<h1>My first post</h1>
<img src="images/profile.jpg" alt="The author holding a coffee cup"
width="200" height="200">
<p>Every path on this page starts from the blog/ folder.</p>
</main>
</body>
</html>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- The browser resolves every path in src or href against the page's URL into one full URL.
- https:// is a complete address, / starts at the domain root, anything else starts in the page's folder, and ../ climbs one level.
- Local C:\ paths, mismatched letter case and / paths under a subpath typically break only after deployment.
- url() inside a CSS file resolves from the stylesheet's location, not from the HTML page.
HTML Living Standard — Links — WHATWG