HTML File Paths (Absolute vs Relative)
HTML File Paths (Absolute vs Relative)
HTML file paths tell the browser where to find linked resources — images, stylesheets, scripts, and other pages. Understanding the difference between absolute and relative paths is essential for building websites that don't break when moved between folders, domains, or environments (like from localhost to production).
Absolute Paths
An absolute path (or absolute URL) includes the full web address, starting with http:// or https://:
<img src="https://example.com/images/logo.png" alt="Logo">
<a href="https://example.com/about.html">About</a>Use absolute paths when linking to resources on a different website or domain.
Relative Paths
A relative path points to a file's location relative to the current page's location — no domain name needed. Use relative paths for internal links within your own site:
<!-- Same folder -->
<img src="logo.png" alt="Logo">
<!-- Subfolder -->
<img src="images/logo.png" alt="Logo">
<!-- Parent folder -->
<img src="../logo.png" alt="Logo">Understanding Dot Notation
./— current folder (often optional and omitted)../— move up one folder level../../— move up two folder levels
<!-- File structure:
project/
index.html
pages/
about.html
images/
logo.png
-->
<!-- Inside pages/about.html, linking to images/logo.png: -->
<img src="../images/logo.png" alt="Logo">Root-Relative Paths
A path starting with a single forward slash / is relative to the domain root, regardless of the current page's location — extremely useful for large sites to avoid broken links after restructuring folders:
<img src="/images/logo.png" alt="Logo">
<a href="/about">About Us</a>Absolute vs Relative: When to Use Each
- Use absolute paths for external resources (other websites, CDNs, third-party APIs).
- Use relative or root-relative paths for internal site resources — this makes your site portable between local development, staging, and production environments without editing every link.
Common Mistakes
- Hardcoding a full domain (
https://mysite.com/...) for internal links, which breaks when moving to a new domain or staging environment. - Miscounting
../levels, causing broken images and 404 links. - Using Windows-style backslashes (
\) instead of forward slashes (/) in paths — web paths always use forward slashes.
Key Takeaways
- Absolute paths include the full URL with domain; used for external resources.
- Relative paths are based on the current file's location; ideal for internal links.
../moves up one directory level; root-relative paths start with/.- Relative and root-relative paths make sites portable across environments.
Press Run to execute.
Press Run to execute.
Given the file structure: project/index.html, project/css/style.css, project/images/hero.jpg, write the correct relative link and image tags for use inside index.html.
Press Run to execute.
This is a self-check — compare your result with the expected output above.
Was this page helpful?