HTML file paths tell the browser where to find resources like images, videos, scripts, and other documents. They ensure that all required files load correctly on a webpage.
src (source) attribute specifies the path to files such as images, videos, or scripts.<img src="path/image.jpg" alt="description"> inserts an image using its file path.There are two main ways to reference a file path in HTML: Absolute and Relative.
An absolute file path specifies the full URL or complete location of a resource, starting from the root of the website or including the domain name.
http:// or https://), domain, and path to the resource.<img src="https://www.example.com/images/picture.png" alt="My Image">
<!DOCTYPE html>
<html>
<head>
<title>Absolute file path</title>
</head>
<body>
<h2>Loading an image from an external URL</h2>
<img src="images/intricate.png"
alt="Absolute Path Image"
style="width: 400px;" />
</body>
</html>
Relative file paths locate resources based on the HTML file’s current location, keeping links portable.
<img src="images/picture.jpg" alt="My Image">
<!DOCTYPE html>
<html>
<head>
<title>Relative file path</title>
</head>
<body>
<h2>File present in the same folder or subfolder</h2>
<img src="images/intricate.png"
alt="IntricateDevo Logo"
style="width: 400px;">
</body>
</html>
In this example, the relative file path "images/intricate-logo.png" indicates that the image file is located in a subfolder named "images" relative to the current HTML file.
When using relative file paths, you can use specific characters to navigate through your project's folders:
images/picture.jpg)/), which tells the browser to look for the resource starting from the root directory of the server. <img src="/images/picture.jpg">./ refers to the current directory.../ moves up one directory level.<img src="../images/picture.jpg"> (Goes up one directory, then looks into the images folder)To maintain a healthy and easily manageable project, follow these best practices:
/css, /js, and /images). This makes it easier to manage and reference your resources.-) or underscores (_) instead (e.g., my-image.jpg).Which relative path notation is used to move UP one directory level before looking for a file?