In HTML, we often refer to other files like images, CSS, or JavaScript. To load these elements properly, we must specify the correct file path.
An absolute path is a complete URL starting with the protocol (https://) and domain. Use it when referring to external files.
html
<img src="https://example.com/images/logo.png" alt="Logo">A relative path defines the location relative to the current file. This is the most common method within your projectβs structure.
html
<img src="images/photo.jpg" alt="Photo">If the target file is one level above the current one, use ../ to reach the parent folder.
html
<img src="../assets/image.png" alt="Image from parent folder">If the file is in the same folder as your HTML file, just write the filename.
html
<link rel="stylesheet" href="style.css">If the file is in a subfolder, first write the folder name, then the filename, e.g., 'scripts/main.js'.
html
<script src="scripts/main.js"></script>Always double-check your folder and file structure. Avoid invalid characters (e.g., spaces or accents) and use consistent naming. Remember that filenames can be case-sensitive, especially on servers.
The folder structure below helps illustrate what a basic HTML project might look like:
plaintext
project/
βββ index.html
βββ style.css
βββ images/
β βββ logo.png
βββ scripts/
βββ app.jsIn the structure above, this is how you can access other files from index.html. This kind of visual helps you understand pathing.
Letβs look at a practical example of how to correctly reference various files in a real project:
html
<!-- Reference to an image -->
<img src="images/logo.png" alt="Logo">
<!-- Reference to a CSS file -->
<link rel="stylesheet" href="style.css">
<!-- JavaScript script -->
<script src="scripts/app.js"></script>Specifying paths accurately is crucial, especially for larger projects. Always make sure the path reflects the actual file structure.
β¨ Ask Lara β your AI study partner
Unlock personalized learning support. Lara can explain lessons, summarize topics, and answer your study questions β available from the Go plan and above.
Lara helps you learn faster β exclusive to ReadyTools Go, Plus, and Max members.