Course contentsShow
← Web Development
Lesson 90 of 2,9822. HTML FundamentalsFree lesson

Base URL and Document Relationships

Using <base> for relative URLs and <link> for alternate versions and canonicals.

Base URL and Document Relationships

You'll learn how to set a default URL for all relative links on a page and tell browsers about alternate versions of your document.

What Is the <base> Element?

Imagine you're giving directions, and every time you say "turn left at the corner" people need to know which neighborhood you're talking about. The <base> element is like announcing "all my directions start from City Hall" at the beginning—it sets a starting point for all relative URLs on your page.

When you write <a href="about.html">, the browser needs to know where "about.html" lives. Without <base>, it assumes the current page's location. With <base href="https://example.com/">, all relative links now resolve from that URL instead.

<head>
  <base href="https://example.com/blog/">
  <!-- Now href="post.html" points to https://example.com/blog/post.html -->
</head>

Important: Only use one <base> element, and place it early in <head> before any URLs.

What Are Document Relationships with <link>?

You've already learned <link> for stylesheets and favicons. It also declares relationships between your page and other versions of itself:

<link rel="canonical" href="https://example.com/article">
<!-- Tells search engines: "This is the original version" -->

<link rel="alternate" hreflang="es" href="https://example.com/es/article">
<!-- Says: "Here's a Spanish version" -->

The canonical link prevents duplicate content issues when the same page exists at multiple URLs. The alternate link helps users and search engines find translations or mobile versions.

Key Takeaway: Use <base> to set a default URL for relative links, and <link rel="canonical"> or <link rel="alternate"> to tell browsers and search engines about other versions of your document.