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:
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.
| From | To | Path | How to read it |
|---|---|---|---|
index.html | about.html | about.html | Same folder: the file name is enough. |
index.html | the photo | images/tomato-soup.jpg | Go into images, then the file. |
recipes/soup.html | index.html | ../index.html | .. means "the folder above this one". |
recipes/soup.html | the photo | ../images/tomato-soup.jpg | Go 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
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:
- A folder called
images. (Right-click in the Explorer panel → New Folder….) - A folder called
recipes, with a new filesoup.htmlinside 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.JPGfor a file calledphoto.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. Fromrecipes/soup.html,images/photo.jpglooks forrecipes/images/photo.jpg. Fix:../images/photo.jpg. - Spaces in names, such as
my photo.jpg. Fix:my-photo.jpg.
Check yourself
- From
index.html, what is the path torecipes/soup.html? - From
recipes/soup.html, what is the path toabout.html? - Why does
/images/photo.jpgfail when you open your page from your own computer?
Answers
recipes/soup.html../about.html- 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.


