Sub topic 2
Creating links with the a element
This chapter assumes you can write a basic HTML document with html, head, and body. It also assumes you can open a file in a browser and understand file and folder paths. It builds on the idea of elements and attributes and focuses on the element that creates hyperlinks.
What the a element does
The a element (the anchor element) turns its content into a hyperlink. A hyperlink is a connection from the current page to another resource. That resource can be a web page, a file, a position inside a page, or an email address.
An anchor has three parts:
- The start tag
<a>, which carries attributes. - The content, which is what the visitor sees and clicks.
- The end tag
</a>.
The attribute that makes it work is href, short for hypertext reference. Its value says where the link goes.
<p>Read the <a href="https://example.com/">Example site</a> for more.</p>The browser shows "Example site" as clickable text. By default it is underlined and blue. Visited links are usually purple. When a visitor clicks, the browser loads the address in href and replaces the current page.
The three kinds of href value
The href value is a URL. Three forms are common. This chapter introduces them, and the next chapter covers how each one resolves.
<a href="https://example.com/">Example site</a>
<a href="/docs/intro.html">Introduction (root-relative)</a>
<a href="guides/setup.html">Setup guide (relative)</a>
<a href="../index.html">Back to the parent folder</a>| Form | Example | Meaning |
|---|---|---|
| Absolute | `https://example.com/` | Full address, including scheme and host |
| Root-relative | `/docs/intro.html` | Starts at the root of the current site |
| Relative | `guides/setup.html` | Starts at the folder of the current page |
Use absolute URLs for other sites. Use relative or root-relative URLs for pages on your own site, so the links keep working if the domain changes.
What can go inside an anchor
Most often the content is plain text. You can also put other inline elements inside, such as em, strong, code, and img.
<a href="/pricing.html">See <strong>pricing</strong> and <code>plan</code> details</a>Modern HTML also lets an anchor wrap larger blocks, such as a whole card made of a heading and a paragraph. This works because the anchor's content model is transparent. It accepts whatever its parent would accept. Two restrictions apply:
- You cannot put another
ainside ana. - You cannot put other interactive elements, such as
buttonor form controls, inside ana.
Browsers repair nested anchors in unpredictable ways, so avoid them.
Anchors without href
An a element with no href is valid, but it is not a link. It is not focusable with the keyboard, it is not announced as a link, and clicking it does nothing. It is a placeholder for a link that is not available yet.
<nav aria-label="Pagination">
<a href="/page/1.html">Previous</a>
<a>Next (no further pages)</a>
</nav>Do not use <a href="#"> as a stand-in for a button that runs JavaScript. The # alone scrolls the page to the top. The element also announces itself as a link when it behaves as an action. Use button for actions and a for navigation. If activating it changes the address or loads a resource, use a. If it submits, toggles, or opens something in place, use button.
Keyboard and default behavior
Anchors with href come with behavior you get for free:
- They can be reached with the Tab key.
- Enter activates them.
- Right-click offers "open in new tab", "copy link address", and similar choices.
- Assistive technology lists them as links, so users can jump between links.
If you replace an anchor with a span and a click handler, you lose all of these. Using the correct element is the cheapest way to get an accessible link.
Optional attributes you will see
href is the only attribute most links need. A few others are informational:
titleadds a tooltip. Do not rely on it. Touch users and many screen reader users never see it.hreflangstates the language of the target, for examplehreflang="fr".typestates the media type of the target, for exampletype="application/pdf".targetandrelcontrol where the link opens and what the new page can learn about yours. A later chapter covers them.download,mailto:, andtel:links get their own chapter.
A complete example
This page links to another site, to a sibling page, and to a page one folder up. Save it in a folder such as site/guides/ and open it in a browser. The relative links only lead somewhere if those files exist.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Links demo</title>
</head>
<body>
<h1>Where to next?</h1>
<ul>
<li><a href="https://example.com/">Example site</a></li>
<li><a href="setup.html">Setup guide (same folder)</a></li>
<li><a href="../index.html">Back to the parent folder</a></li>
</ul>
</body>
</html>Common errors
- Missing scheme.
href="example.com"is not an absolute URL. The browser treats it as a relative path, so fromhttps://mysite.test/docs/page.htmlit goes tohttps://mysite.test/docs/example.com. Writehttps://example.com/. - Broken relative path. The link works on one page but not another because the pages sit in different folders. Check where the current file lives, or use a root-relative path.
- Unclosed or mismatched quotes.
href="page.html>swallows the rest of the tag. Always close the quotes. - Forgetting the end tag. Without
</a>, the rest of the paragraph becomes part of the link. - Case and extension mismatches.
Setup.htmlandsetup.htmlare different files on most web servers, even if both work on your own computer. - Spaces in paths. Prefer file names without spaces. If you must keep them, write
%20in the URL. - Using links for actions. A link styled as a button that runs a script breaks keyboard and screen reader expectations.
Check your understanding
Exercise
A page at https://mysite.test/docs/page.html contains <a href="example.com">Visit</a>. Where does the link actually go, and how do you fix it so it opens the other site?
Show answer
The value has no scheme, so the browser resolves it as a relative path and goes to https://mysite.test/docs/example.com. Fix it by writing the absolute URL: <a href="https://example.com/">Visit</a>.
Exercise
A teammate writes <a href="#" onclick="openMenu()">Menu</a> to open a dropdown. Which element should replace it, and which behavior of the original is wrong?
Show answer
Use a button element, because the control performs an action and does not navigate. With href="#" the page jumps to the top when clicked, and the element is announced to assistive technology as a link instead of a button.
Exercise
Why is <a href="/a.html">Go <a href="/b.html">now</a></a> a problem?
Show answer
An a element cannot contain another a element. Browsers repair the markup in their own way, so the links may not behave as written. Write two separate anchors instead.