In this tutorial, you will learn HTML file path
In HTML, file paths are used to specify the location of resources such as images, videos, stylesheets, and scripts. There are two types of file paths: relative and absolute.
- Relative file paths: A relative file path is used when the resource is located in the same directory or a subdirectory as the HTML file. For example, if you have an image called “image.jpg” located in the same directory as the HTML file, you can use the following code to display it:
<img src="image.jpg" alt="My Image">
- Absolute file paths: An absolute file path is used when the resource is located on a different server or a different directory on the same server. Absolute file paths start with the protocol (http or https) and the domain name. For example, if you have an image located on another website, you can use the following code to display it:
<img src="https://www.example.com/images/image.jpg" alt="My Image">
- It’s important to note that if you use a relative file path and move the HTML file to another directory, the path to the resource will no longer be valid and the resource will not be displayed. To avoid this problem, you can use absolute file paths.
- You can also use the “../” to go back one level in the directory tree, for example, if you have an image in a subfolder called images and the HTML file is located one level above, you can use the following code to reference the image:
<img src="../images/image.jpg" alt="My Image">
- When working with local files, you can also use file:// protocol to reference a file on your local machine, for example:
<img src="file:///C:/Users/yourusername/Desktop/image.jpg" alt="My Image">
In summary, file paths in HTML are used to specify the location of resources such as images, videos, stylesheets, and scripts. You can use relative file paths when the resource is located in the same directory or a subdirectory as the HTML file, or you can use absolute file paths when the resource is located on a different server or a different directory on the same server. You can also use “../” to go back one level in the directory tree and use file:// protocol to reference a file on your local machine.