On this page10 sections
  1. Learning Objectives
  2. 1. An Example Site
  3. 2. Absolute URLs
  4. 3. Relative Paths
  5. 4. Root-Relative Paths
  6. 5. Which to Use
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 021: File Paths

Category4: Page Structure
Lecture021 of 048
Estimated video length8 min
TargetHTML5 · any modern browser
LevelBeginner

Every link, image, stylesheet and script points to a file. A file path tells the browser where that file is: on another website, at the top of your site, or somewhere relative to the current page. Get the path wrong and you get a broken image.


Learning Objectives

  • Tell absolute URLs from relative paths
  • Write paths to the same folder, a sub-folder, the folder above, and the site root
  • Choose the right kind of path for each situation

1. An Example Site

Paths make more sense with a real folder layout:

output
my-site/
├── index.html
├── about.html
├── css/
│   └── styles.css
├── images/
│   └── logo.png
└── blog/
    ├── index.html
    └── first-post.html

2. Absolute URLs

A full web address, including https://. Use it for files on other websites.

html
<img src="https://picsum.photos/id/1080/200/120" alt="Strawberries" width="200" height="120">

3. Relative Paths

A relative path starts from the folder of the current page.

From index.html, you write…It finds…
about.htmlabout.html in the same folder
images/logo.pnglogo.png inside images/
css/styles.cssstyles.css inside css/
From blog/first-post.html, you write…It finds…
index.htmlblog/index.html (same folder)
../index.htmlthe main index.html, one folder up
../images/logo.pnglogo.png: up one folder, then into images/

.. means "go up one folder". ../../ goes up two.

html
<!-- inside blog/first-post.html -->
<link rel="stylesheet" href="../css/styles.css">
<img src="../images/logo.png" alt="My Site logo">
<a href="../about.html">About me</a>

4. Root-Relative Paths

A path starting with / begins at the top of the website, wherever the current page is.

html
<img src="/images/logo.png" alt="My Site logo">
<a href="/blog/first-post.html">First post</a>

These work the same from every page, which makes them handy for menus. They need a web server, though: double-clicking a file on your computer treats / as the top of your hard drive.


5. Which to Use

SituationBest path
A file on another websiteAbsolute: https://…
Your own files, site opened by double-clickingRelative: images/logo.png
Your own files, a navigation menu on a real serverRoot-relative: /images/logo.png

Relative and root-relative paths keep working if you move the whole site to a new domain.


Common Beginner Mistakes

1. Backslashes. images\logo.png is a Windows habit. The web always uses /.

2. Wrong capitals. Web servers usually treat Logo.PNG and logo.png as different files.

3. Paths to your own computer. C:/Users/Ali/Desktop/logo.png only works on your PC. Copy the file into your site folder instead.

4. Forgetting ../. From blog/post.html, images/logo.png looks for blog/images/logo.png.


Practice Questions

  1. What's the difference between an absolute URL and a relative path?
  2. From index.html, write the path to photo.jpg inside the images folder.
  3. What does ../ mean?
  4. From blog/first-post.html, link to about.html in the main folder.
  5. What does a path starting with / mean?
  6. Why shouldn't you use C:/… paths in a website?

Practice Answers

  1. An absolute URL is a full address (https://…); a relative path starts from the current page's folder.
  2. images/photo.jpg
  3. Go up one folder.
  4. <a href="../about.html">About</a>
  5. Start from the top (root) of the website.
  6. They point to a file on your own computer, which nobody else can reach.

Key Takeaways

  • Absolute = full https:// address; use it for other sites.
  • Relative = from the current folder; .. goes up a level.
  • Root-relative = starts with /, from the top of the site.
  • Always use forward slashes and exact capitals.