
When a project works on your computer but its images or styles vanish on someone else’s, check the file paths first. This guide shows how HTML and CSS locate files and how to keep those links working when you move or publish your project.
1. What is a file path?
A path tells the browser where to find a resource, such as an image, a stylesheet, a JavaScript file, or a video. Here, image.png is in the same folder as the HTML file.
<img src="image.png" alt="A view of the project">
2. Three ways to write a path
2.1. Relative paths
Relative paths are resolved against a base URL. In a typical HTML page, that is the URL of the document. Use the filename for a resource in the same folder, a folder name and slash to go down, and ../ to go up one level.
<img src="image.png" alt="Project preview">
<img src="images/photo.jpg" alt="Project preview">
<img src="../photo.jpg" alt="Project preview">
<img src="../../photo.jpg" alt="Project preview">
2.2. Root-relative paths
A leading slash starts at the website’s origin. If your project is deployed under /course/, /images/photo.jpg still points to the site’s top-level images directory.
<img src="/images/photo.jpg" alt="Project preview">
2.3. Absolute URLs
An absolute URL includes the scheme and hostname. Use one when you need to address a specific host, such as the server that hosts your images.
<img src="https://example.com/images/photo.jpg" alt="Project preview">
3. Keep references independent of your computer
A path such as C:\Users\Alex\Desktop\site\image.png points to your local drive. It cannot locate the image on the web server or on your classmate’s computer. Put the asset inside the project and refer to it with a path such as images/image.png.
4. Which folder does CSS start from?
When an external stylesheet uses a relative URL, the browser resolves it from the stylesheet’s URL. In this example, style.css has to go up from css/ before it can enter images/.
project/
├── index.html
├── css/
│ └── style.css
└── images/
└── background.jpg
/* css/style.css */
body {
background-image: url("../images/background.jpg");
}
5. Before you hand in or upload the project
- Keep assets in predictable folders such as
css/,js/, andimages/. - Check for references to your desktop or home directory and replace them with project paths.
- Open the project from a different folder or computer and check that every image and stylesheet loads.
- Match filename capitalization exactly. A local computer may accept
Photo.jpgfor a file calledphoto.jpg, while your server may treat them as separate names.
6. File path reference
| Path | Example | Where resolution starts |
|---|---|---|
| Same folder | image.png | The base directory |
| Subfolder | images/photo.jpg | The base directory |
| Parent folder | ../photo.jpg | One level above the base directory |
| Multiple levels up | ../../images/photo.jpg | Two levels above the base directory |
| Root-relative | /images/photo.jpg | The website’s origin |
| Absolute URL | https://example.com/photo.jpg | The specified host |
| Local drive path | C:\Users\Alex\Desktop\photo.jpg | Your own computer only |