More HTML answers

by Danny
Published 3 min read

Buttons do things and links go places. Learn how to choose between them so keyboard, screen reader and browser features all work as users expect.

The short answer

#

Use a link (<a href>) when the user goes somewhere: another page, another site, a section of the current page or a file. Use a button (<button>) when the user does something on the current page: submits a form, opens a dialog, toggles a menu, saves or deletes. How it looks is a styling choice and should not decide which element you use.


_10
<!-- Goes somewhere: a link -->
_10
<a href="/pricing">See pricing</a>
_10
_10
<!-- Does something: a button -->
_10
<button type="button" id="open-menu">Menu</button>

Why it happens

#

The question comes up because both elements look clickable and both can run JavaScript, so it is tempting to pick whichever is easier to style. But the browser, assistive technology and the user all treat them differently.

A link has a URL. Because of that, people can open it in a new tab, copy its address, bookmark it, see the destination in the status bar and use the back button afterwards. Screen readers announce it as a link and list it in their links menu. It is activated with the Enter key.

A button has no URL. Screen readers announce it as a button, which tells the user that pressing it will change something here. It is activated with both Enter and Space. Inside a form it submits by default, and it can be disabled with the disabled attribute.

When the element does not match the behaviour, those expectations break. A link that opens a modal leaves a useless entry in the history and does nothing when opened in a new tab. A button that navigates cannot be opened in a new tab or copied.

How to fix it

#

Pick the element from the behaviour

Ask one question: does this change the URL? If yes, use a link with a real href. If no, use a button. A few common cases:

  • Navigation menus, breadcrumbs, pagination and "read more": links.
  • "Skip to content" that jumps to an id on the page: a link with href="#main".
  • Download a file: a link, optionally with the download attribute.
  • Open or close a dialog, accordion or dropdown: a button.
  • Like, save, delete, add to basket, copy to clipboard: a button.
  • Submit a search form: a submit button, even though the result is a new page, because the form does the navigating.

Style the element, do not swap it

If the design calls for a link that looks like a button, or a button that looks like a link, keep the right element and change the CSS.


_18
.button-like {
_18
display: inline-block;
_18
padding: 0.5rem 1rem;
_18
border-radius: 0.375rem;
_18
background: #1d4ed8;
_18
color: #fff;
_18
text-decoration: none;
_18
}
_18
_18
.link-like {
_18
background: none;
_18
border: 0;
_18
padding: 0;
_18
font: inherit;
_18
color: inherit;
_18
text-decoration: underline;
_18
cursor: pointer;
_18
}

For client-side routing, use the framework's link component (such as Link in Next.js) rather than a button with a click handler that calls router.push. The link component renders a real <a href>, so new-tab and copy-link still work, and the framework can prefetch the route.


_10
import Link from "next/link";
_10
_10
export function Nav() {
_10
return <Link href="/articles">Articles</Link>;
_10
}

Keep router.push for navigation that happens as a result of an action, such as redirecting after a form is saved.

Replace clickable divs

A <div> or <span> with a click handler is neither. It is not focusable with the Tab key, does not respond to Enter or Space and is announced as plain text. You can patch it with role, tabindex and key handlers, but a real <button> gives you all of that for free.


_10
<!-- Avoid -->
_10
<div class="close" onclick="closePanel()">Close</div>
_10
_10
<!-- Prefer -->
_10
<button type="button" class="close" onclick="closePanel()">Close</button>

Common pitfalls

#
  • Using href="#" or href="javascript:void(0)" on a link that runs an action. It is a button pretending to be a link; use a button.
  • Leaving out href on an <a>. Without it the element is not focusable and is not exposed as a link.
  • Forgetting type="button" on buttons inside forms, so they submit the form by accident.
  • Putting a button inside a link, or a link inside a button. Interactive elements must not be nested, and the result is unpredictable for keyboard and screen reader users.
  • Icon-only buttons and links with no accessible name. Add visible text, or an aria-label if the design truly has no text.
#

Was this resource helpful?