Understand the idea

An anchor with an href connects a document to another location. That location can be a page, a file, an email address, or a section of the current page.

A path beginning with a single / starts at the origin's root. Other relative paths are resolved against the document's base URL, normally the current page URL. A base element can change that base, so these examples assume none is present. A fragment such as #contact points to an element with a matching id. Use meaningful link text so a reader can predict the destination.

Read the example

This is an HTML fragment. Place it inside the body of a complete HTML document, unless the example explicitly identifies head content.

HTML · EXAMPLE
<a href="/html/">HTML articles</a>
<a href="#contact">Contact details</a>

<section id="contact">
  <h2>Contact</h2>
  <a href="mailto:42@html-code.fyi">Email Mario</a>
</section>

A small mistake, explained

What goes wrong

From /projects/gallery/, href="images/photo.jpg" requests /projects/gallery/images/photo.jpg, not /images/photo.jpg.

How to fix it. Inspect the resolved URL in the browser. Check spelling, case and the directory structure. Test root-relative paths through a local web server rather than by opening a file directly.

Try it yourself

Make a link to a section, then deliberately change its id. Restore the exact match and test again.

Further reading

HTML Living Standard — links

MDN — the document base URL

Original explanation and example prepared for HTML code FYI with AI assistance. Test the code in your own context. How these guides are made.