Href gives the target address for a link. It may be a file path, a complete web URL, or a fragment such as #projects that points inside the current document. The important question is always where the link leads and whether the person reading the page can predict it.
What You Need to Know
For fragments to work, the destination needs a matching unique id. Relative paths resolve against the current document's URL unless a base element changes the rule.
Links are the pathways through a website. The words on a link should help someone choose where to go before clicking it.
Example: See It in Action
Try this markup in a fresh practice file. The next section explains the parts worth paying attention to.
This is a short HTML fragment. To try it as a webpage, put it inside the body element of a complete HTML document.
<h2 id="projects">My Projects</h2>
<p>Explore what I built this year.</p>
<a href="#projects">Jump to projects</a>
<a href="about.html">Read the about page</a>
Let’s Understand the Example
- An a element becomes a working link when it has an href. Its visible text should tell readers where they will go.
- An h2 starts a major section beneath the page topic.
- A p groups a paragraph of related text. Start another paragraph for a new thought.
- Href contains the destination of a link. A value starting with # points to an id on the same page.
- Id gives one element a unique name within the page, so a link, label, or script can point to it.
The first example link points to #projects. Check whether that destination exists relative to the document. A link changes where readers go; it does not perform an action such as saving a form.
What Should You See?
You should see a link named “Jump to projects”. Selecting it leads to #projects when that destination exists. Try it with the mouse and the keyboard.
Imagine this link is read out of context. Would its text still explain the destination? Rename unclear links before trying to make them look nicer with CSS.
Try It Yourself
- Make a link that jumps to another heading on the same page.
- Save your changes, open or refresh the page, and compare the result with the original example.
- Tell a friend what your change did. If you are not sure, undo it, refresh the page, and compare.
Common Beginner Mistakes
- Writing href="projects" when the actual page is projects.html.
- Avoid dead links, empty href values and clickable-looking text without a real destination.
Key Takeaways
- Href gives the target address for a link.
- An a element becomes a working link when it has an href. Its visible text should tell readers where they will go.
- For fragments to work, the destination needs a matching unique id. Relative paths resolve against the current document's URL unless a base element changes the rule.