ComparisonComparisons

<button> vs <a>

<button> performs an action on the page, e.g. submitting a form. <a> navigates: it changes the address or opens a resource. Both work with the keyboard.

On this page

Side-by-side comparison

#
  • Main purpose

    <button>
    Performing an action: submitting a form, running a JS script
    <a>
    Going to a different address or resource
  • Attribute required to work

    <button>
    None, type is optional
    <a>
    href, otherwise it’s neither clickable nor focusable
  • Changes the URL

    <button>
    No, except for a form submitted with the get method
    <a>
    Yes, that’s its main job
  • Can open in a new tab

    <a>
    Yes, with target="_blank"
  • Adds an entry to the browsing history

    <a>
    Yes
  • When to use it

    <button>
    Buttons in forms, JS actions
    <a>
    Links to pages, anchors, files to download

Example

#
HTML
<button type="button" onclick="alert('Changes saved')">Save changes</button>
<a href="/pricing">Go to pricing</a>
Preview

Definition and usage

#

<button> performs an action within the current page: it submits a form, clears fields, or runs your JavaScript code when it has type="button". On its own, it doesn’t change the page address, except for a form submitted with the get method.

<a> is for navigation: it changes the address, jumps to an anchor on the same page, or opens a file for download. Without an href attribute, it’s just a placeholder, neither clickable nor focusable. It can open in a new tab (target="_blank"), and unlike a button, it adds an entry to the browsing history, so the "back" button makes sense.

A simple rule: if a click just does something on the same page, use <button>. If it takes you elsewhere, use <a>. Never build a clickable <div> for either role, because you’ll lose keyboard and screen reader support that you’d otherwise get for free.

Syntax

#
Syntax
<button type="button">...</button>
<a href="address">...</a>

More examples

#
A button in a form submits it by default
HTML
<form onsubmit="event.preventDefault(); this.querySelector('output').textContent = 'Sent!'">
  <button>Send</button>
  <output></output>
</form>
Preview

A button without type="button" has a default type of submit inside a form.

A link that opens in a new tab
HTML
<a href="https://developer.mozilla.org" target="_blank" rel="noopener noreferrer">
  MDN documentation (new tab)
</a>

Good practices

#
  • If a click just does something on the same page, use <button>. If it takes you elsewhere, use <a>.
  • Don’t build clickable <div> elements for either role. You’ll lose the keyboard support that comes with them for free.
  • A link without href isn’t focusable or clickable. It’s just a placeholder.

Related entries

#

Spotted a mistake or missing an example? Write to us.