← Back to HTML & CSS, page 4

html-css-0062 · Easy fresher

When should you use an a element and when a button element?

Use a with href when the control goes somewhere, and button when it does something on the current page. Native elements give focus, keyboard activation and correct announcements for free. A button in a form submits by default, so set type="button" when it should not.

Full answer

Use the element that matches what the control does.

  • A link (a with href) takes the user somewhere: another page, another part of the page, a file, or a mail address. Users expect to be able to open it in a new tab, copy the address and bookmark it.
  • A button does something on the current page: submit a form, open a menu, play a video, delete an item. It has no address.

Using the right element gives you behaviour for free. A link is focusable, works with Enter, and shows its address on hover. A button is focusable and works with both Enter and Space. A div with a click handler has none of this. It cannot be reached with the Tab key and is not announced as a control by screen readers, so it needs a role, a tabindex and key handlers to match, and it is better not to build it at all.

Details that matter. An a without href is not focusable and is not a link. A button inside a form submits it by default, because its default type is submit. Write type="button" for buttons that should not submit. You can style a link to look like a button, or a button to look like a link. The element still decides the meaning.

The first button has no type, so it is a submit button. The second is a plain button and does not submit. The a without href cannot receive focus, while the one with href can. The last link looks like a button but still navigates, which is correct because it goes to a new page.

A quick test: if it changes the address, use a link. If it changes something on the current page, use a button.

html
<form id="f" action="/enrol" method="post">
  <input name="email" type="email" required>
  <button id="send">Send</button>
  <button id="menu" type="button" aria-expanded="false">Menu</button>
</form>
<a id="real" href="/courses">See all courses</a>
<a id="fake">Not a link</a>
<style>
  .btn { display: inline-block; padding: 0.5rem 1rem; background: #0a6cff; color: #fff; text-decoration: none; border-radius: 4px; }
</style>
<a class="btn" id="cta" href="/signup">Create account</a>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Links navigate; buttons perform actions.
  • a without href is not focusable and not a link.
  • A button in a form defaults to type submit.
  • Native elements provide keyboard support and roles.
  • Style does not change meaning: a link can look like a button.

Common mistakes

  • Using a div or span with an onclick for something that should be a button.
  • Using a link with href="#" and a click handler to run an action. Use a button for actions.
  • Forgetting that a button in a form submits by default, and then getting a page reload when it should only open a menu.

Follow-ups

  • What is the default type of a button inside a form?
  • How would you make an existing div clickable if you cannot change the markup, and why is it a poor choice?

Tips

  • Use the address test: does it change the URL? Link. Does it act on this page? Button.
  • Mention that a link styled as a button is fine, as long as it navigates.