patterns

Links and Anchors

The essentials of designing and implementing accessible links and anchors on web pages.

Introduction

Accessible web links with descriptive, meaningful text help make your web content usable for everyone, improve accessibility for people who use assistive technology, and aid search engine optimization.

The HTML <a> element is a foundational concept for the Web. 1 Its primary purpose is to create a link that allows users to navigate to new content.

Semantics

A common accessibility issue is the semantic misuse of links as buttons and vice versa. The two have distinct purposes; users of assistive technology will be confused if the choice of HTML element does not match the implied use case. The <button> element implies an action rather than navigation. Buttons are expected to perform some task on the current page, such as submitting a form or running a scripted behavior.

Even visually styling a link to look like a button can be confusing. Users may hesitate to click a link when it presents itself as a button which could have a more consequential effect, such as submitting data or modifying the current page.

Designer notes

When designing accessible links with accessibility in mind, consider the following points:

Three text links ('Watch again', 'Disability Journalism Forum', and 'Event Partners') are arranged in a vertical list. A cursor hovers over the middle link surrounded by a solid red circle. The red circle is centered on the middle link and is near but does not overlap with dashed red circles centered on the surrounding links.
To illustrate the required spacing specified by the minimum 24 CSS pixel rule, a circle with a 24 CSS pixel diameter is drawn around the cursor as it hovers over one of the links in a list of links (from a website published by the BBC). Since the 24 pixel circle does not overlap any neighboring link circles, this spacing would be considered sufficient.

Learn more about accessible focus styles in our Focus Indication Guide.

Assistive technology experience

Instead of reading the entire page aloud, screen reader users may prefer to only listen to a list of the available links. This is useful for index pages where users want to scan for a link they want to follow. In this case, the screen reader will only announce the text for each link independently, without reading any of the surrounding non-link content.

A list of links from a news website presented by a screen reader.
The text from each link is listed without any context from the surrounding content on the web page. The screen reader displays linked article titles such as “Who Was Fernando Villavicencio?” The link text “Leer en español” (read in Spanish) appears four times in the list.

Keeping the length of each link short enough to fit in the screen reader dialog is helpful. In the example above, links over about eight words are clipped.

“Front-loading” your link text is also helpful for screen reader usability, meaning you should keep the distinctive parts of the link text near the beginning of the link. Listening to repeated texts at the start of each link can be annoying when scanning a list of links.

People using voice control software can control their computers without a keyboard or mouse. They can select and click links using a two-step process when viewing a web page. First, they can use the command “Click” to display a number overlaid on each clickable element on the page, then they can say the number to follow a link.

Voice control software has displayed a number (1 through 9) next to each visible link on this news website. By saying a number, the user can follow the corresponding link.

It is much preferred by users of voice control software to use a one-step method of saying “Click” and the accessible name of an element (remembering that accessible name includes the visible text as well as any alternative text). Keep this option available when possible. In cases where the link is labeled by an icon or several links have the same text, the process may require two steps, as described above.

Users who navigate a web page using only a keyboard can press the tab key to move a virtual keyboard focus indicator from one interactive element to the next. Users expect the order this keyboard focus takes will follow the reading order of the language the web page is written in (generally left to right and top to bottom for web pages in English).

Once the user has moved the keyboard focus to a link they would like to follow, they can press the enter key to click the link.

A web page has several numbered red circles overlaid to indicate the tab order of each link on the page. A red line zig-zags down the page, connecting each link and indicating the tab order.
The path that the keyboard focus takes from link to link on this news website has been visualized using an accessibility checker. The links are arranged so that the keyboard focus moves from top to bottom as expected.

Developer notes

Before you start

As part of the requirement for a designed accessible experience, you should confirm the design documentation for links has defined:

HTML structure

Navigable links are composed of content (also known as the anchor text, link label, or link text) and a destination in the form of a document fragment identifier or a URL (Uniform Resource Locator). For example, the content of a link could be text such as “Contact Us,” and the destination could be the address of a web page with information about contacting the organization.

<a href="http://example.com/cntc/index1">
  Contact Us
</a>

To be accessible, the link must contain text content or an alternative text equivalent. You must provide a text alternative for links with an image (or any other non-text) element as their content.

<a href="http://example.com/cntc/index1">
  <img src="/contact-icon" alt="Contact Us">
</a>

For this reason, it is not accessible to create links with no content (or content that cannot be assigned a text equivalent). For example, using CSS to set the background of a link to an icon effectively makes the icon invisible to screen readers, which generally ignore any visual styling. In those cases, the screen reader will fall back to reading out the href value instead. For example, without alternative text, the user would only hear “http://example.com/cntc/index1”.

When the visible content of a link is added using CSS, you might think you could improve the accessibility by adding an aria-label attribute, which would allow you to provide a text label for screen reader users. However, many other users would still be excluded if they were not using a screen reader but applied custom CSS overrides to make the web page accessible, such as in so-called reading mode.

<!-- don't do this -->
<a
  class="icon-contact_us"
  href="http://example.com/cntc/index1"
  aria-label="Contact Us"
></a>

Screen reader users who navigate by listing the links on a web page will miss the context of each link. Using distinctive, visible text to label each link text will help differentiate them for screen reader users and will likely help sighted users keep track of what they are linking to.

Note: The Web Content Accessibility Guidelines (WCAG) Success Criteria 2.4.4 (Level A) and 2.4.9 (Level AAA) discuss link text. The Level A criteria allows link text such as “Read more” if the surrounding content makes it clear what the link is referring to. The Level AAA criteria is more strict and requires link text to be clear on its own, such as “Read more about how we support local communities.”

In some cases, such as a series of “Read more” links, where it would be distracting for sighted users to see the specific context in every link, adding the context as visually hidden text, available only to screen reader users, is a possible compromise.

The examples below demonstrate two techniques for adding context to a “Read more” link series without causing visual clutter. The first technique uses the aria-label attribute on a link to provide an extended label accessible to screen reader users. (Because this is a screen reader-specific issue, using a solution that only works for screen readers is acceptable.)

For a screen reader, the text of the aria-label attribute will replace the text in the link.

<h2>Our Values</h2>
<p>
  We support the environment and all the creatures who live in it.
  <a href="https://example.com" aria-label="Read more… about Our Values">Read more…</a>
</p>
Note: The aria-labelledby attribute can reference the link text if it has an id, so you can provide a list of id values that include both a heading, for example, and the link itself to create a link name that combines the heading text and the link text. It’s a good way to ensure that the visible text is included in the computed accessible name.

The second technique uses CSS to visually hide additional text that will appear as part of the link label for screen reader users and any other user that might override the web page CSS.

<h2>Fair Trade</h2>
<p>
  Our fair trade policy supports local farmers and communities.
  <a href="https://example.com">
    Read more…
    <span class="visually-hidden">
      about Fair Trade
    </span>
  </a>
</p>
The examples from this section both include links that are visually displayed as “Read more.” By applying the techniques described, a screen reader can include additional context for each link. The first is presented as “Read more… about Our Values” by the screen reader. The second is presented as “Read more… about Fair Trade.”

Learn more about hiding web page content accessibly in our Hiding Things Guide.

While the two techniques in this section seem functionally equivalent, it is important to know that not every piece of text on a web page will be available to automated translation services like Google Translate. Some services will not translate text in an aria-label attribute, but all will translate visually-hidden text in the body of a link. It’s essential to test any translation tools or services your website plans to support to ensure the added text is supported by that service.

While having descriptive, visible text is the most accessible way to label a link, there are use cases where a well-understood icon would seem more natural. In these cases, alternative text must be supplied for screen reader users who cannot see the icon.

In the example below, we use an SVG element as an icon but provide visually hidden alternative text which will be available to screen readers. Choose an alternative text that summarizes the meaning of the image; there’s no need to explain that it is an “image,” “icon,” or “picture” in the alternative text.

<div class="social-links">
  <a href="#mastodon"><svg aria-hidden="true" focusable="false" class="icon icon-inline"><use href="/static/img/icons/social.svg#mastodon"></use></svg><span class="visually-hidden">Visit Us on Mastodon</span></a>
</div>

The link with the mastodon icon would be announced by a screen reader like “link, Visit Us on Mastodon”

Learn more about accessible icons in our Icons Guide.

When linking to external websites, it is a good idea from a security and accountability perspective to inform the user that they are about to visit content you are not responsible for.

Don’t rely only on CSS to warn users about external links (or links that open in a new tab). Because content included via CSS is not part of the web page Accessibility API, some assistive technology will not read it. In other cases, users may override the web page CSS to display it in a way that is accessible for them and so may not see content added via CSS. 2 Limit content added via CSS to decorative or supplemental content only.

The example below shows the text “(opens in new tab)” appended via CSS to links with a matching target attribute. A better approach is to add text or an icon to the link element content, as is demonstrated in the next section.

/* don't do this */
a[target]:not([target='_self']):after {
  content: ' (opens in new tab)';
}

New tabs

Opening links in new tabs or windows can make some users’ experience confusing and less accessible, so it should be used rarely but may be justified in the case of external websites as a way to warn the user before they follow the link.

In the following example, the user is notified that the link will be to an external website and will open in a new window. This information is part of the link text itself. We use CSS to enhance that notification by visually displaying a well-understood icon.

.is-external::after {
  width: 1em;
  height: 1em;
  margin: 0 0.15em;
  display: inline-block;
  vertical-align: middle;
  content: "";
  background-color: currentcolor;
  -webkit-mask-image: url("/static/img/guides/links-and-anchors/new-tab.svg");
  mask-image: url("/static/img/guides/links-and-anchors/new-tab.svg");
}
<p>
  <a
    href="https://example.com"
    target="_blank"
    rel="noopener noreferrer"
    class="is-external"
    >Read our Carbon Pledge<span class="visually-hidden"> (external website, opens in new tab)</span></a>
    to see how we are working to protect the environment.
</p>

Read our Carbon Pledge (external website, opens in new tab) to see how we are working to protect the environment.

A screen reader would announce the external link like, “link, sustainable practices, (external website, opens in new tab).” With CSS disabled or overridden, the information is still available as text in the link.

The progressive enhancement of this technique is illustrated below. The icon is displayed when CSS supports it, but without the CSS (for example, when the page is displayed in “reader mode,” the fallback is automatically displayed as plain text.

The text 'Read our Carbon Pledge to see how we are working to protect the environment.' containing an external link on the phrase 'Read our Carbon Pledge.' The external link is indicated with an icon of a square with a diagonal-upwards arrow.
The same link is displayed twice. In the first example, CSS is used to show an “arrow and box” icon to indicate that the link will open in a new window. The second example is without CSS; the icon is not displayed, and a text alternative is visible instead, showing the words “external website, opens in new tab.”

A modifier key is a special key on a keyboard that, when pressed in with another key, temporarily changes the function of that other key. In the case of a person who navigates the web using their keyboard, they are likely to rely on these modifier keys to efficiently accomplish common tasks.

For example, when a link has keyboard focus, a user can follow that link by pressing the enter key, but (on MacOS) if the user holds the command key when pressing the enter key, they can open that link in a new browser tab. (On Windows OS use alt and enter keys.) By pressing shift and enter keys the user can open a link in a new window.

When coding and testing links for keyboard accessibility, you must ensure that your implementation doesn’t create a barrier for these users by disabling or unexpectedly changing these behaviors.

Note: One of the reasons for knowing and using standard HTML elements is that so much of the web platform interface is already embedded in long-established browser behaviors. Anytime you find yourself trying to recreate the behavior of link using some element that isn’t an <a> you are probably doing it wrong.

In some scenarios, you may need to display a link whose destination is the page you are already on. For example, a link to the current page might be included for consistency in a navigation component. However, following such a link would be useless since it goes nowhere new and could cause confusion if it merely reloads the current page.

In these cases, using an <a> element with no destination href could be more usable. A link without a destination is considered a placeholder link, 3 meant to mark up text that may become a link in different circumstances. Functionally, the browser will treat the link as if it were plain text; it is not announced as a link by screen readers, you cannot move keyboard focus to it using the tab key, and it will do nothing if it is clicked.

Stylistically, however, any CSS rules matching an <a> element will equally apply to placeholder links. This might not be very clear to sighted users, that would not be able to distinguish between functioning links and placeholder links visually. To limit a style to only links with an href attribute, use a declaration like: a[href].

The example below displays a list of navigational links on a homepage. The link with the content “Home” is a placeholder link since it would merely link to the page we are already on; it has no destination href attribute and is presented as ordinary text by screen readers.

We’ve added the ARIA attribute aria-current to add additional information for screen readers that the placeholder link represents the current navigational link within a list of links.

.placeholder-link-demo a {
  text-decoration: none;
  font-weight: normal;
}

.placeholder-link-demo a[href]:hover {
  text-decoration: underline;
}

.placeholder-link-demo a[aria-current] {
  font-weight: bold;
}
<nav class="placeholder-link-demo">
  <ul class="u-inline">
    <li><a href="#home" aria-current="page">Home</a></li>
    <li><a href="#about">About</a></li>
    <li><a href="#contact">Contact</a></li>
  </ul>
</nav>

The link with the label “Home” would be announced by a screen reader like “Home, current page, 1 of 3.”

The aria-current attribute can be used in other scenarios too. For example, use the value’ step’ for a series of links that correspond to steps in a process.

<nav>
  <ol>
    <li><a href="#ingredients">Assemble your ingredients.</a></li>
    <li><a href="#base">Prepare the base.</a></li>
    <li><a href="#toppings" aria-current="step">Add the toppings.</a></li>
  </ol>
</nav>

The link with the label “Add the toppings.” would be announced by a screen reader like “Add the toppings., current step, 3 of 3.”

Adding anchors to headings

For longer articles, encourage users to copy and share links to specific parts of the web page by adding visible clues that the heading has an anchor link. The link symbol is a visible but only decorative way of indicating the presence of the anchor link.

h3.is-anchor a {
  position: relative;
  padding-left: 1em;
  margin-left: -1em;
  text-decoration: none;
}

h3.is-anchor a:hover::before,
h3.is-anchor a:focus-visible::before {
  content: "#";
  width: 1em;
  position: absolute;
  left: 0
}
<h3 id="fresh-idea" class="is-anchor">
  <a href="#fresh-idea">A Fresh Idea</a>
</h3>
<p>
  Try a new twist on an old favorite. It's cool, refreshing, and filled with delicious fruit flavors.
</p>

A Fresh Idea

Try a new twist on an old favorite. It’s cool, refreshing, and filled with delicious fruit flavors.

The link heading would be announced by a screen reader like “heading level 3, link, A Fresh Idea”

Testing notes

Test summary

When evaluating links and anchors for accessibility, add these checks to your usual testing process:

  1. Ensure each link contains visible text or a well-understood, conventional icon.
  2. For links containing only an icon, ensure that equivalent text is assigned to the graphic so that a screen reader can announce it.
  3. Ensure that all links are consistently and visually distinct from the surrounding text so the user can identify them. Do not use color alone to identify links; this is inaccessible to users with low color perception.
  4. Links that are not embedded in a block of text should be large enough and/or spaced sufficiently so that a 24 CSS pixel circle centered over the link will not overlap with a similar circle drawn over any surrounding links or interactive elements. (This may not be possible in cases where the placement of elements conveys meaning, such as the locations on a map; treat these as exceptions to the rule.)
  5. Ensure that when navigating the web page using the keyboard, it is visually apparent when a link has keyboard focus.
  6. Ensure that if a screen reader presents all the links as a list, separate from their surrounding context, the resource being linked to can still be understood. (Link text such as “click here,” for example, is inadequate.)
  7. When a link contains text along with an icon, where the icon is decorative-only, the screen reader should ignore the icon.
  8. Ensure that examples of multiple links to the same resource are not adjacent or wrapping each other; these should be combined into a single link instead.
  9. Ensure that any cases of hidden or alternative text available only to screen readers are not different than the visible text or different from the icon’s apparent meaning.

Test rules

This guide has an accompanying formal testing criteria document for this topic. See the Links and Anchors ACT test rules for this guide.

Footnotes

  1. The concept of a “hyperlink,” a way of linking parts of a text to other documents, was described as early as 1945 in a published essay entitled “As We May Think” by Vannevar BushBack to footnote link 1.
  2. See WCAG 2.0, F87: Failure of Success Criterion 1.3.1 due to inserting non-decorative content by using :before and :after pseudo-elements and the ‘content’ property in CSS. Back to footnote link 2.
  3. “If the <a> element has no href attribute, then the element represents a placeholder for where a link might otherwise have been placed if it had been relevant, consisting of just the element’s contents.” 4.5.1 The <a> element, html.spec.whatwg.org Back to footnote link 3.

Attributes

Initial publication date:
Contributors: Michael Mathews (Author), Ian Pouncey (Reviewer).

Updates