Skip to the content

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.

ExtensionType of file
.htmlHTML page
.cssCSS styles
.jsJavaScript code
.tsTypeScript code
.jsonData in JSON format
.mdText notes (Markdown)
.png .jpg .svgImages

A small project

my-site/
  index.html
  about.html
  css/
    style.css
  js/
    app.js
  images/
    logo.png

index.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.

SymbolMeaning
./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.css

You 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.png

From js/app.js, reach about.html. Go up one folder:

../about.html

A 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.png

This means "the images folder at the top of the website", wherever the current page is.

Rules for naming files

  • Use lowercase letters: about.html, not About.html. Many servers treat these as two different files.
  • No spaces. Use a hyphen: contact-us.html, not contact us.html.
  • Use clear names: invoice-list.js, not file2.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.png and logo.png are 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

WordMeaning
FileOne item that holds content
Folder / directoryA container for files and folders
RootThe top folder of a project or website
ExtensionThe letters after the dot that show the file type
PathThe address of a file or folder
Absolute pathA full address from the top
Relative pathAn address that starts from the current file

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.