Lesson 16 of 51 · Module 3, lesson 1 of 6 · 4 min

Folders and relative paths

Write file paths that work on your computer and on a web server, and avoid the three mistakes that break them.

What you'll learn

  • What a relative path is, and how to write one
  • How to go into a folder and up out of a folder
  • The three path mistakes that break most beginner websites

Why this matters

In the next lessons you will link pages together and add images. Both need a path: the address of another file. When a link does not work or an image shows a broken icon, the path is wrong nine times out of ten. This lesson makes paths clear before you need them.

A small website

Here is a website folder with a few files. You will build one like it:

A folder tree: my-website contains index.html, about.html, an images folder with tomato-soup.jpg, and a recipes folder with soup.html. From index.html, the path to about.html is "about.html"; the path to the photo is "images/tomato-soup.jpg". From recipes/soup.html, the path back to the home page is "../index.html", and the path to the photo is "../images/tomato-soup.jpg".

my-website/
├─ index.html
├─ about.html
├─ images/
│  └─ tomato-soup.jpg
└─ recipes/
   └─ soup.html

Relative paths

A relative path starts from the folder of the page you are writing in. Think of giving directions from where you stand.

FromToPathHow to read it
index.htmlabout.htmlabout.htmlSame folder: the file name is enough.
index.htmlthe photoimages/tomato-soup.jpgGo into images, then the file.
recipes/soup.htmlindex.html../index.html.. means "the folder above this one".
recipes/soup.htmlthe photo../images/tomato-soup.jpgGo up one folder, then into images.

Three pieces are all you need:

  • a file name, such as about.html;
  • a folder name and a slash to go into a folder, such as images/;
  • two dots and a slash to go up one folder: ../. You can repeat it: ../../ goes up two folders.

Absolute URLs

An absolute URL is a full web address, starting with https://. Use it for pages and files on other websites:

https://www.wikipedia.org/

For your own files, always use relative paths. They work on your computer and on a web server, without changes.

The three mistakes that break paths

1. A slash at the start

The code img src="/images/tomato-soup.jpg" with the first slash highlighted in red. On your computer, a path that starts with a slash begins at the top of your disk, so the browser looks for C:/images/tomato-soup.jpg on Windows, or /images/tomato-soup.jpg on macOS, and the image is broken. On a web server, the same path begins at the website's top folder and can work. Fix: leave out the first slash and write images/tomato-soup.jpg.

A path that starts with / does not start from your page. It starts from the very top. On a web server, that is the top of the website — so tutorials often use it. But on your computer, the "top" is the top of your whole disk, and the file is not there. While you learn, never start a path with /.

2. Capital letters that do not match

On your computer, Tomato-Soup.jpg and tomato-soup.jpg may open the same file. On most web servers they are two different names, so the link breaks as soon as your site goes online. Keep every file and folder name in lower case (see the naming rules in Files, folders and file extensions), and type paths exactly.

3. Backslashes

Windows shows folder paths with backslashes: images\tomato-soup.jpg. Web paths always use a forward slash: images/tomato-soup.jpg.

Try it yourself

In VS Code, create this in your my-website folder:

  1. A folder called images. (Right-click in the Explorer panel → New Folder….)
  2. A folder called recipes, with a new file soup.html inside it.

Now answer, without looking at the table: what path goes from recipes/soup.html to images? You will use these folders in the next lessons.

Common mistakes

  • A leading slash, such as /images/photo.jpg. Broken on your computer. Fix: images/photo.jpg.
  • Wrong case, such as Images/Photo.JPG for a file called photo.jpg. Works on some computers, fails online. Fix: lower case everywhere, typed exactly.
  • Backslashes, such as images\photo.jpg. Fix: forward slashes only.
  • Forgetting ../ in a page inside a subfolder. From recipes/soup.html, images/photo.jpg looks for recipes/images/photo.jpg. Fix: ../images/photo.jpg.
  • Spaces in names, such as my photo.jpg. Fix: my-photo.jpg.

Check yourself

  1. From index.html, what is the path to recipes/soup.html?
  2. From recipes/soup.html, what is the path to about.html?
  3. Why does /images/photo.jpg fail when you open your page from your own computer?

Answers

  1. recipes/soup.html
  2. ../about.html
  3. A path that starts with / begins at the top of the disk, not at your website folder, so the browser looks in the wrong place.

Next: with paths clear, you are ready to connect pages with links.

Chat with us on WhatsApp