HTML File Paths
A file path tells the browser where to find a file, such as an image, a CSS file or another page.
Where File Paths Are Used
You write a file path every time an attribute points to another file:
| Element | Attribute | Example |
|---|---|---|
<a> | href | <a href="about.html">About</a> |
<img> | src | <img src="images/logo.png" alt="Logo"> |
<link> | href | <link rel="stylesheet" href="css/style.css"> |
<script> | src | <script src="js/app.js"></script> |
<video>, <audio> | src | <video src="media/intro.mp4" controls></video> |
If the path is wrong, the browser cannot find the file. You get a broken image, a page without styles, or a "404 Not Found" page.
An Example Website Folder
Imagine Ali is building a website for his computer shop in Karachi. His folder looks like this:
Example
ali-computers/
index.html
about.html
css/
style.css
images/
logo.png
laptops/
dell.jpg
products/
laptops.htmlAll the examples below use this folder.
Relative Paths
A relative path starts from the folder of the current page. It is the most common type of path inside your own website.
| Path | Meaning |
|---|---|
about.html | A file in the same folder |
images/logo.png | A file inside the images folder |
images/laptops/dell.jpg | A file two folders down |
../index.html | Go up one folder, then find index.html |
../../file.html | Go up two folders |
From index.html, the logo is at images/logo.png:
Example
<!-- inside index.html -->
<img src="images/logo.png" alt="Ali Computers logo">
<a href="products/laptops.html">See our laptops</a>From products/laptops.html, the page is one folder deeper, so you must go up first with ../:
Example
<!-- inside products/laptops.html -->
<link rel="stylesheet" href="../css/style.css">
<img src="../images/laptops/dell.jpg" alt="Dell laptop">
<a href="../index.html">Back to home</a>Think of ../ as "go back one folder". Each ../ moves you one level up.
Root-Relative Paths
A path that starts with a slash / begins at the root (top folder) of the website, no matter where the current page is.
Example
<img src="/images/logo.png" alt="Ali Computers logo">
<link rel="stylesheet" href="/css/style.css">This works the same from every page, which is handy for menus and headers. But it only works when the site is served by a web server. If you open the file directly from your computer (a file:// address), / means the top of your hard disk, and the files are not found.
Absolute Paths (Full URLs)
An absolute path is a full web address with https:// and the domain name. Use it for files on another website.
Example
<!DOCTYPE html>
<html>
<body>
<h2>Absolute and Relative Links</h2>
<p><a href="https://www.wikipedia.org/">Wikipedia (absolute URL)</a></p>
<p><a href="about.html">About page (relative path)</a></p>
<p><a href="#top">Top of this page (same page)</a></p>
</body>
</html>Try it Yourself »| Type | Example | Best for |
|---|---|---|
| Relative | images/logo.png | Files inside your own site |
| Root-relative | /images/logo.png | Shared menus on a live server |
| Absolute | https://example.com/logo.png | Files on other websites |
Common Mistakes
- Wrong slash. Windows shows paths like
images\logo.png. On the web, always use a forward slash:images/logo.png. - Wrong case. Web servers often treat
Logo.PNGandlogo.pngas different files. Use lowercase file names. - Spaces in names.
my photo.jpgcan cause problems. Use hyphens:my-photo.jpg. - Forgetting `../`. A page inside a subfolder must go up before it can reach files in other folders.
A simple habit saves many broken links: lowercase names, hyphens instead of spaces, and relative paths for your own files.
Exercise
Exercise
A page is at products/laptops.html. Which path reaches css/style.css from that page?
