HTML File Paths

HTML File Paths

A file path describes the location of a file in a web site’s folder structure.

File paths are used when linking to external files like:

  • Web pages
  • Images
  • Style sheets
  • JavaScripts

Absolute File Paths

An absolute file path is the full URL to an internet file:

<!DOCTYPE html>

<html>

<body>

 

<h2>Using a Full URL File Path</h2>

<img src=”https://www.w3schools.com/images/picture.jpg” alt=”Mountain” style=”width:300px”>

 

</body>

</html>

 

Using a Full URL File Path

Mountain

Relative File Paths

A relative file path points to a file relative to the current page.

In this 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/picture.jpg” alt=”Mountain” style=”width:300px”>

 

</body>

</html>

 

Using a Relative File Path

Mountain

In this 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/picture.jpg” alt=”Mountain” style=”width:300px”>

 

</body>

</html>

 

Using a Relative File Path

Mountain

In this example, the file path points to a file in the images folder located in the folder one level above the current folder:

<!DOCTYPE html>

<html>

<body>


<h2>Using a Relative File Path</h2>

<img src=”../images/picture.jpg” alt=”Mountain” style=”width:300px”>


</body>

</html>


Using a Relative File Path

Mountain

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.