Lesson 5 of 296
Files, folders and paths
What you will learn
- How a project is organised
- What a file extension is
- What a path is
- The difference between an absolute path and a relative path
- Rules for naming files
On this page
Files and folders
A file holds content: code, text, an image. A folder (also called a directory) holds files and other folders. A project is one main folder with everything inside it. That main folder is called the root of the project.
File extensions
The letters after the last dot in a file name are the extension. It tells you and the computer what kind of file it is.
| Extension | Type of file |
|---|---|
.html | HTML page |
.css | CSS styles |
.js | JavaScript code |
.ts | TypeScript code |
.json | Data in JSON format |
.md | Text notes (Markdown) |
.png .jpg .svg | Images |
A small project
my-site/
index.html
about.html
css/
style.css
js/
app.js
images/
logo.pngindex.html is a special name. When someone visits a folder on a website without naming a file, the server sends index.html by default. That is why the home page of a site is nearly always called index.html.
What is a path?
A path is the address of a file or folder. It lists the folders you go through to reach it, separated by /.
Absolute paths
An absolute path starts from the very top and gives the full address. It works from anywhere.
/Users/sam/projects/my-site/css/style.css (on a computer)
https://example.com/css/style.css (on the web)Relative paths
A relative path starts from the file you are in now. It is shorter, and it keeps working when you move the whole project to another computer or to a server.
| Symbol | Meaning |
|---|---|
./ | This folder (the folder of the current file) |
../ | One folder up |
../../ | Two folders up |
Examples
Use the project above.
From index.html, link to the CSS file. Both index.html and the css folder are in my-site, so go into css:
./css/style.cssYou can also write css/style.css. It means the same thing.
From css/style.css, use the logo image. You are inside css. First go up one folder to my-site, then into images:
../images/logo.pngFrom js/app.js, reach about.html. Go up one folder:
../about.htmlA path that starts with /
On a website, a path that starts with a single / begins from the root of the site, not from the current file.
/images/logo.pngThis means "the images folder at the top of the website", wherever the current page is.
Rules for naming files
- Use lowercase letters:
about.html, notAbout.html. Many servers treat these as two different files. - No spaces. Use a hyphen:
contact-us.html, notcontact us.html. - Use clear names:
invoice-list.js, notfile2.js. - Always keep the correct extension.
Common mistakes
- The page works on your computer but images are missing on the server. The usual reason is capital letters.
Logo.pngandlogo.pngare the same file on Windows and Mac, but different files on most servers (Linux). - Using an absolute path from your own computer, such as
/Users/sam/.... It cannot work on anyone else's machine. Use relative paths inside a project. - Forgetting
../when the file you want is in a different folder.
How to say it in an interview
"An absolute path gives the full location from the root, so it works from anywhere. A relative path starts from the current file, using ./ for the same folder and ../ to go up a level. Inside a project I use relative paths so the project keeps working when it is moved or deployed."
Key words
| Word | Meaning |
|---|---|
| File | One item that holds content |
| Folder / directory | A container for files and folders |
| Root | The top folder of a project or website |
| Extension | The letters after the dot that show the file type |
| Path | The address of a file or folder |
| Absolute path | A full address from the top |
| Relative path | An address that starts from the current file |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.