HTML Tutorial menu

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:

ElementAttributeExample
<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

text
ali-computers/
  index.html
  about.html
  css/
    style.css
  images/
    logo.png
    laptops/
      dell.jpg
  products/
    laptops.html

All 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.

PathMeaning
about.htmlA file in the same folder
images/logo.pngA file inside the images folder
images/laptops/dell.jpgA file two folders down
../index.htmlGo up one folder, then find index.html
../../file.htmlGo up two folders

From index.html, the logo is at images/logo.png:

Example

html
<!-- 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

html
<!-- 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

html
<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

html
<!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 »
TypeExampleBest for
Relativeimages/logo.pngFiles inside your own site
Root-relative/images/logo.pngShared menus on a live server
Absolutehttps://example.com/logo.pngFiles on other websites

Common Mistakes

  1. Wrong slash. Windows shows paths like images\logo.png. On the web, always use a forward slash: images/logo.png.
  2. Wrong case. Web servers often treat Logo.PNG and logo.png as different files. Use lowercase file names.
  3. Spaces in names. my photo.jpg can cause problems. Use hyphens: my-photo.jpg.
  4. 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?

Learn HTML with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp