How to create a link
Understand what a link does and how to make one.
What is a link?
A link, or hyperlink, is an interactive component on a web page. When a user clicks or presses it, it sends the browser to another location or resource.
A link’s target location or resource is identified with a URL (Uniform Resource Locator).
These are some examples of URLs:
- Web page: https://www.w3.org/WAI/WCAG21/quickref/
- Section of a web page: https://www.w3.org/TR/WCAG21/#adaptable
- Email address: mailto:web.standards@dia.govt.nz
- Phone number: tel:+6441234567
- PDF file: https://digital.govt.nz/assets/Digital-government/Strategy/Strategy-for-a-Digital-Public-Service.pdf
If the interactive component that you want to create does not send the user to another location or resource — in other words, if there is no URL involved — then you probably want to use a different one, like a button.
The difference between links and buttons
Note: A link should not:
- be used for creating a button
- be designed to look like a button.
Designing a link to look like a link and a button to look like a button is important. This is because each has a distinct role and how the component looks will tell the user something about:
- how to interact with it (a link cannot be activated with the keyboard’s Space key while a native HTML button can)
- what to expect (activating a link will take you to another location or resource, while activating a button will trigger some action on the page).
Read more about links versus buttons
- A Complete Guide to Links and Buttons — CSS-Tricks
- Links vs Buttons and Do’s & Don’ts for Accessibility — Steve Barnett
- Links vs Buttons in Modern Web Applications — Marcy Sutton
Create a link
There are a few ways to create a link.
Native HTML links
Using the <a>
or <area>
element with the href attribute
A link created using an <a>
or <area>
element with an href
attribute is keyboard accessible by default. This means that it can be focused using the Tab key and activated by pressing the Enter key.
An <a>
or an <area>
element without an href
attribute is not a link.
<a>
element
Using the <a>
element with an href
attribute is the most common way to make a link in HTML — the ‘a’ stands for ‘anchor’.
The href
(hypertext reference) attribute identifies the anchor’s target and takes a valid URL as its value.
With an <a href>
element, the link’s text or content normally sits within the <a>
element’s opening and closing tags, but it can come from other sources. See How links get accessible names.
For more information and examples of how to use the <a>
element, see:
- <a>: The Anchor element — MDN Web Docs
- Creating hyperlinks — MDN Web Docs.
- The
a
element — HTML Living Standard
<area>
element
The <area>
element with an href
attribute creates a link in an image map, that is, an image with geometric regions that are hyperlinked.
The href
(hypertext reference) attribute identifies the <area>
element’s target and takes a valid URL as its value.
An <area>
element, just like an <img>
element, requires an alt
attribute that provides a text equivalent for people who cannot see or understand what the linked region of the image map represents.
For more information and examples of how to use the <area>
element and image maps, see:
- Image Maps — Web Accessibility Tutorials — W3C
- <map>: The Image Map element — MDN Web Docs
- <area>: The Image Map Area element — MDN Web Docs
- Add a hitmap on top of an image — MDN Web Docs
- Image maps — HTML Living Standard
Custom links
In almost all cases, it’s recommended to use native HTML link elements.
Sometimes, however, a developer will repurpose some other HTML element to serve as a link. Such custom links need extra work to make them accessible. For more on what some of that work involves, see:
More information on creating a custom link
- Link — ARIA Authoring Practices Guide — W3C: Includes examples of clickable HTML
<span>
and<img>
elements made into accessible links. - Using the link role — MDN Web Docs