Skip to main content

HTML File Paths

A file path describes the location of a file in a website's folder structure.

We can link any external resource to add in our HTML file with the help of file paths such as images, file, CSS file, JS file, video, etc.

Specify file paths

PathDescription
<img src="picture.jpg">The "picture.jpg" file is located in the same folder as the current page
<img src="images/picture.jpg">The "picture.jpg" file is located in the images folder in the current folder
<img src="/images/picture.jpg">The "picture.jpg" file is located in the images folder at the root of the current web
<img src="../picture.jpg">The "picture.jpg" file is located in the folder one level up from the current folder

HTML File Paths

A file path describes the location of a file in a website's folder structure.

File paths are used when linking to external files, like:

  • Web pages
  • Images
  • Style sheets
  • JavaScripts

There are two types of File Paths:

  1. Absolute File Paths
  2. Relative File Paths

Absolute File Paths

Absolute file path specifies full URL address.

Example:

<!DOCTYPE html>  
<!DOCTYPE html>
<html>
<body>
<h2>Using a Full URL File Path</h2>
<img src="https://www.example.com/images/nature.jpg" alt="image" style="width:300px">
</body>
</html>

Relative File Paths

The relative file path specifies to a file which is related to the location of current page.

In the following example, the file path points to a file in the images folder located at the root of the current web:

<!DOCTYPE html>  
<html>
<body>
<h2>Using a Relative File Path</h2>
<img src="/images/nature.jpg" alt="Mountain" style="width:300px">
</body>
</html>

In the following example, the file path points to a file in the images folder located in the current folder:

<!DOCTYPE html>  
<html>
<body>
<h2>Using a Relative File Path</h2>
<img src="images/nature.jpg" alt="Mountain" style="width:300px">
</body>
</html>

In the following example, the file path points to a file in the images folder located in the folder one level up from the current folder:

<!DOCTYPE html>  
<html>
<body>
<h2>Using a Relative File Path</h2>
<img src="../images/nature.jpg" alt="Mountain" style="width:300px">
</body>
</html>

Best Practice

  • It is best practice to use relative file paths (if possible).
  • When using relative file paths, your web pages will not be bound to your current base URL. All links will work on your own computer (localhost) as well as on your current public domain and your future public domains