HTML Basics
HTML File Paths
A file path tells the browser where to find a file like an image, stylesheet, or another page. Getting paths right is the number one fix for broken images and links.
What is a File Path?
A file path is the address of a file inside your project or on the web. Whenever you use src or href, you provide a path. There are two kinds: absolute paths give the full location, and relative paths describe the location compared to the current file.
Relative paths are preferred for files within your own website because they keep working even if you move the whole project to a new server or domain.
Absolute vs Relative Paths
| Path | Type | Means |
|---|---|---|
| https://site.com/img/a.png | Absolute | The full URL to a file anywhere on the web. |
| img/a.png | Relative | In the img folder, inside the current folder. |
| a.png | Relative | In the same folder as the current file. |
| ../a.png | Relative | One folder up from the current file. |
| /img/a.png | Root-relative | From the site's root folder. |
Example: Linking Files by Path
<!DOCTYPE html>
<html>
<body>
<!-- Absolute path to a web image -->
<img src="https://picsum.photos/200" alt="Absolute path image">
<!-- Relative path: image in an 'images' subfolder -->
<img src="images/photo.jpg" alt="Relative path image">
<!-- Relative link one folder up -->
<a href="../index.html">Back to home</a>
</body>
</html>More Examples
<!-- Current file: index.html -->
<!-- Target file: blog/post.html -->
<a href="blog/post.html">Read the blog post</a><!-- From deep/inside/page.html to css/style.css at the root -->
<link rel="stylesheet" href="../../css/style.css">Prefer relative paths for your own files. If you later change your domain or move the site into a subfolder, relative links keep working without edits.
Paths are case-sensitive on most web servers. Photo.jpg and photo.jpg are different files online, even though they may look the same on Windows.
Key Takeaways
- Absolute paths give the full URL; relative paths are relative to the current file.
- Use ../ to move up one folder and folder/ to move into one.
- Prefer relative paths for files within your own site.
- Watch out for case sensitivity on real web servers.
