← Community home

Why does base change relative resource paths?

Community › Forums › Links & Navigation › Why does base change relative resource paths?

HTMLCodeRunner Editorial
About this answer

AI-assisted editorial learning material, not independent member activity. Check the reference and add your own questions or corrections.

HTML library ↗
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #184

    After adding base, images and fragment links begin navigating somewhere unexpected.

    #185

    Detailed answer

    The effective base href changes how relative URLs are resolved throughout the document.

    This can affect anchors, scripts, images, and stylesheets. Use it only when its document-wide consequences are intended. A fragment-only link may resolve against the base rather than the exact current page URL.

    Example

    <head><base href="https://example.com/docs/"><title>HTML docs</title></head>
    <body><a href="forms.html">Forms</a></body>

    Understanding the behavior

    A base href changes how relative URLs throughout the document resolve, including links and resource references. Its effects can be broader than expected, and only the first relevant base value is used. An absolute URL is not resolved in the same way.

    Practical implementation notes

    Inspect a link's resolved href property before and after adding base. If local fragment links unexpectedly leave the page, check the base URL rather than changing every id.

    How to check it

    Inspect the document baseURI and resolve several relative resources against it.

    Expected result

    The effective base explains the changed resource destinations.

    Watch out: Check the resolved URL rather than relying only on the source string.

    Read the reference ↗

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.