View all posts

Link or Button

What is a Link?

Definition

A link is an interactive element that redirects the user to a new location which can be another section inside the current page, modifying the URL with a # parameter, or a new page. It can be used to download a file. Once activated, it takes the user to the URL set in its href. The browser records that navigation in its history, so the user can return to the previous page using the back button.

Semantic

The elements needs the attribute href with a valid URL or an IDREF pointing to a section inside the current page to have the semantic value of a link, otherwise it will be considered as generic.

<a href="/URL">
  Go to main page
</a>
Keyboard Interaction

It can only be activated by pressing the key Enter. If the key Space is pressed while the focus is on the link, the page will scroll down.

Screen Reader Interaction

Screen Readers, generally, announce the links in the following way: Link, [accessible name of the link]. It is extremely important to provide a descriptive and correct accessible name to the element.

Bad Practice

It is completely unnaceptable, a bad practice and goes against the native behavior of the element, forcing it to behave as a button by doing the following:

<a href="javascript:void(0)" onclick="openModal()">
  Open Menu
</a>
<a href="#" role="button" onclick="button()">
  Link with role button
</a>

If you need to do this, it means that you need a button

What is a button?

Definition

A button is an interactive element that dispatches an action inside the page where it is located. It does not redirect the user to another place or location nor modifies the url. The actions that are being dispatched can be: open a modal, play a video, post a comment, etc.

Semantic

The button needs the attribute type with a value according to its action:

  • type="button": it is used when the button does not have a default behavior.
  • type="submit": it is used when the button sends information to a server.
  • type="reset": it is used to reset all the values of an input or inputs to its initial value.
<button type=”button” onclick="openModal()">
  Open Modal
</button>
Keyboard Interaction

It can only be activated by pressing the keys Enter or Space.

Screen Readers Interaction

Screen Readers, generally, announce buttons in the following way: "Button, [Accessible Name of the button]". It is extremely important to provide a descriptive and correct accessible name to the element.

Bad Practice

It is completely unnaceptable, a bad practice and goes against the native behavior of the element, forcing it to behave as a link by doing the following:

<button onclick="window.location.href='/'">
  Go to homepage
</button>

If you need to do this, it means that you need a link

Which one should I use: link or button?

  • Do you need an expandable section? use a <button> with the state aria-expanded and the focus should stay put.
  • Do you need to jump to a section on the same page? use an <a href="#section">. There is no page load, updated the url, lands the user at a destination.
  • Do you need to load more items into the current list? use a <button> and when the items load, the focus should be placed in the first item of the new items.
  • Do you need to redirect the user to a new location to read more about something? use an <a href="/destination">.
  • Do you need to open a menu? use a <button> with the states aria-haspopup and aria-expanded.
  • Do you need to play or pause a video? use a <button>.
  • Do you need to dowload a file? use a <a> with the attribute download.
  • Do you need a card that leads to a detail page? use a <a> stretched ::after.

Focus Style for Interactive Elements

It is vital for keyboard and screen reader users to have a visual and logic focus on interactive elements. Never do outline: none without providing a focus style:

a:focus-visible,
button:focus-visible {
  outline: 4px solid #0066ff;
  outline-offset: 4px;
}

Resources