On this page10 sections
Lecture 021: File Paths
| Category | 4: Page Structure |
| Lecture | 021 of 048 |
| Estimated video length | 8 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
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:
my-site/
├── index.html
├── about.html
├── css/
│ └── styles.css
├── images/
│ └── logo.png
└── blog/
├── index.html
└── first-post.html2. Absolute URLs
A full web address, including https://. Use it for files on other websites.
<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.html | about.html in the same folder |
images/logo.png | logo.png inside images/ |
css/styles.css | styles.css inside css/ |
From blog/first-post.html, you write… | It finds… |
|---|---|
index.html | blog/index.html (same folder) |
../index.html | the main index.html, one folder up |
../images/logo.png | logo.png: up one folder, then into images/ |
.. means "go up one folder". ../../ goes up two.
<!-- 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.
<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
| Situation | Best path |
|---|---|
| A file on another website | Absolute: https://… |
| Your own files, site opened by double-clicking | Relative: images/logo.png |
| Your own files, a navigation menu on a real server | Root-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
- What's the difference between an absolute URL and a relative path?
- From
index.html, write the path tophoto.jpginside theimagesfolder. - What does
../mean? - From
blog/first-post.html, link toabout.htmlin the main folder. - What does a path starting with
/mean? - Why shouldn't you use
C:/…paths in a website?
Practice Answers
- An absolute URL is a full address (
https://…); a relative path starts from the current page's folder. images/photo.jpg- Go up one folder.
<a href="../about.html">About</a>- Start from the top (root) of the website.
- 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.