Skip to content

HTML Button: the button Tag, Types and Examples

A button is <button type="button">Add to cart</button>. Inside a form, use type="submit" for the button that sends it and type="button" for everything else, because a button with no type sends the form. And if clicking it should take people to another page, you want a link, not a button. Try pressing Tab in the examples to see which things your keyboard can reach.

A basic button

A button

That’s a real button. You can reach it with Tab, press it with Enter or Space, and screen readers call it “Add to cart, button”. On its own it does nothing yet: a button needs either a form to send or a bit of JavaScript to run (both are below).

submit, button and reset

The type attribute decides what a button does inside a form:

typeWhat it does
submitSends the form. This is the default when you leave type out
buttonNothing by itself. Use it for buttons that run JavaScript
resetPuts every field back to its starting value
Three types in one form

Change “Latte” to something else and press Start again: the box goes back to Latte. (Order does nothing here because these boxes can’t send forms; on your page it would go to /order?drink=Latte.)

Watch out for this one: a “Show the menu” button written as plain <button> inside a form is a submit button, so clicking it sends the form and reloads the page before your script has done anything. Put type="button" on every button that isn’t meant to send. Outside a form, a button with no type does nothing on its own, but writing the type anyway saves trouble if the code is moved into a form later.

A quick rule that settles most cases:

If clicking it…UseExample
goes to another page or a spot on this one a link, <a href> See the menu, Read more, Back to top
does something on this page, or sends a form <button> Add to cart, Open menu, Send, Close

It matters because links and buttons behave differently. People can open a link in a new tab, copy its address or bookmark it, and screen reader users hear “link” and expect to go somewhere. A link can still look exactly like a button:

A link and a button that look alike

Same class, same look, but the first is a link (it changes the address to #menu) and the second is a button. The navbar tutorial has more on menus made of links.

Style a button with CSS

Browsers give buttons their own small font, grey background and border. Here’s a styled button next to a plain one:

A coffee-brown button
  • font: inherit is the line people forget. Buttons don’t take the page’s font; in Chrome a plain button uses a small 13.33px default font whatever your page uses. Compare the two buttons above.
  • cursor: pointer shows the hand cursor that people expect over something clickable.
  • :focus-visible draws a ring when someone reaches the button with the keyboard. Press Tab in the box to see it. Never remove the outline without putting something in its place.
  • Check that the text color is readable on the button color with the Color Contrast Checker. White on this brown passes easily.

For gradients, shadows, icons and hover effects without writing them by hand, try the HTML Button Generator.

Disabled buttons

Add disabled and the button can’t be clicked, can’t be reached with Tab, and isn’t sent with the form:

Enabled and disabled

Press Tab a few times in that box: the focus skips the second button. That’s fine for a button that truly can’t be used. The downside is that keyboard and screen reader users may never find out it’s there, or why it’s off. If you want people to find the button and learn why it isn’t ready, use aria-disabled="true" with a short reason instead:

aria-disabled with a reason

This button still gets focus and screen readers say “Pay $4.20, dimmed” (or “unavailable”), followed by the reason. But aria-disabled only changes what assistive tech is told: the button still clicks. Block the click yourself:

JavaScript: ignore clicks while aria-disabled is on
const pay = document.querySelector('[aria-describedby="why"]');
pay.addEventListener('click', (event) => {
  if (pay.getAttribute('aria-disabled') === 'true') {
    event.preventDefault(); // stops a submit button from sending the form
    return;
  }
  // ...pay as normal
});

Buttons with icons

Put the icon inside the button next to the words. An inline SVG with stroke="currentColor" takes the button’s text color, so when you recolor the button on hover, the icon follows:

Icon and text, and icon only
  • aria-hidden="true" on the SVG keeps screen readers from trying to read the drawing. The words “Add a cup” already say what the button does.
  • A button with only an icon has no words to read, so give it a name with aria-label="Close". Without it, screen readers announce just “button”.
  • An icon alone works for well-known symbols like close or search. For anything less obvious, show the word too.

Big enough to tap

A plain button in Chrome is only about 21px tall, which is small under a thumb. WCAG 2.2 asks for click targets of at least 24 by 24 CSS pixels (or enough space around a smaller one) at level AA, and 44 by 44 at the stricter AAA level. Apple’s design guidelines also use 44 points, so it’s a sensible size to aim for:

Default size and a 44px button

min-height and min-width make the button at least that big while still letting it grow when the text is long. Leave some space between buttons that sit side by side, too, so a slightly-off tap doesn’t hit the wrong one.

input type="submit" vs button

Older pages often use <input type="submit">. It sends a form just like a submit button:

Two submit buttons

The difference is what can go inside. An input’s label is its value attribute, plain text only. A <button> can hold other tags: bold text, an icon, a price in a <span>. Both are fine, and <button> is easier to style, so it’s what most new code uses. An <input type="submit"> without a value shows the browser’s own word for it, “Submit” in English.

form, name and value

A submit button doesn’t have to sit inside its form. The form attribute points it at a form by id. Give submit buttons a name and value and the server also learns which one was pressed:

Two submit buttons outside the form
<form id="order" action="/order">
  <label for="drink">Drink</label>
  <input id="drink" name="drink" value="latte">
</form>

<!-- Outside the form, but still sends it -->
<button type="submit" form="order" name="size" value="small">Small</button>
<button type="submit" form="order" name="size" value="large">Large</button>

Pressing Large sends /order?drink=latte&size=large. Only the button that was pressed adds its pair. A button can also send to a different address than the form with formaction, which suits a “Save for later” next to the real Send:

One form, two addresses
<form action="/order" method="post">
  <label for="note">Note for the barista</label>
  <textarea id="note" name="note"></textarea>
  <button type="submit">Send</button>
  <button type="submit" formaction="/save-draft">Save for later</button>
</form>

For everything else about forms, from labels to what gets sent, see the HTML forms tutorial.

Make it do something with JavaScript

A type="button" button runs whatever code you attach to its click event. This one is running, so press it a few times (with the mouse, or Tab to it and press Space):

Running above with JavaScript on. The code:
<p>Cups ordered: <output id="count">0</output></p>
<button type="button" id="add">Add a cup</button>

<script>
  const add = document.getElementById('add');
  const count = document.getElementById('count');
  let cups = 0;

  add.addEventListener('click', () => {
    cups += 1;
    count.value = cups;
  });
</script>

The click event fires for a mouse click, a tap, and Enter or Space on the keyboard, so one listener covers everyone. You’ll also see the short inline way, an onclick attribute:

The inline onclick way
<button type="button" onclick="alert('Thanks, Sam!')">Say thanks</button>

It works and is fine for a quick test. For a real page, addEventListener keeps the JavaScript in one place instead of spread through the HTML. Not sure where the script goes? The link JavaScript to HTML tutorial shows the options.

Common mistakes

The biggest one is a <div> dressed up as a button. They can look the same. Now press Tab in this box:

A div and a button
  • A div as a button. Tab skips the div, Enter and Space do nothing on it, and screen readers don’t call it a button. You can patch that with role="button", tabindex="0" and key handlers, but <button> gives you all of it for free.
  • Leaving out type in a form. The button becomes a submit button. See the “Show the menu” trap above.
  • A button inside a link, like <a href="/https/htmlviewer.tech/menu"><button>Menu</button></a>. It isn’t valid HTML, and keyboard users meet two tab stops for one thing. Use a link styled as a button.
  • outline: none with nothing to replace it. Keyboard users lose track of where they are. Style :focus-visible instead.
  • Vague words. “Click here” or “Submit” say little. “Order coffee” or “Send message” tell people what will happen.

Questions people ask

How do I make a button in HTML?

Use the button tag with a type: <button type="button">Add to cart</button>. It can be reached with Tab and pressed with Enter or Space without any extra work. To make it do something, attach JavaScript to its click event, or use type="submit" inside a form to send the form.

What is the default type of a button?

submit. A <button> with no type inside a form sends the form when clicked, which is why a "Show menu" button can reload the page by surprise. Write type="button" on buttons that only run JavaScript, and type="submit" on the one that sends the form.

Should I use a button or a link?

If clicking it goes somewhere, like another page or a section of this one, use a link: <a href="/menu">See the menu</a>. If it does something on the page or sends a form, like adding to a cart or opening a menu, use <button>. A link can be styled to look exactly like a button, so pick by what it does, not by how it looks.

How do I make a link look like a button?

Give the link a class and style it: .btn { display: inline-block; padding: 10px 18px; border-radius: 8px; background: #7c4a2d; color: #fff; text-decoration: none; }, then <a class="btn" href="/menu">See the menu</a>. Don't wrap a <button> in an <a>; that isn't valid HTML.

How do I disable a button in HTML?

Add the disabled attribute: <button type="submit" disabled>Pay</button>. It can't be clicked or reached with Tab. If people should still find it and learn why it's off, use aria-disabled="true" with aria-describedby pointing at a short reason, and block the click in JavaScript, because aria-disabled doesn't stop clicks by itself.

What is the difference between button and input type="submit"?

Both send a form. <input type="submit" value="Send order"> can only show plain text from its value. <button type="submit">Send <strong>order</strong></button> can hold other tags, like an icon or bold text, and is easier to style, so most new code uses <button>.

Why does my button look different from the rest of my page?

Buttons don't inherit the page font. In Chrome a plain button uses a small 13.33px default font, whatever font your page uses. Add font: inherit to your button styles, along with your own padding, background and border, and cursor: pointer for the hand cursor.

How big should a button be?

WCAG 2.2 asks for click targets of at least 24 by 24 CSS pixels at level AA (or enough space around smaller ones) and 44 by 44 at level AAA. 44px is a comfortable size for fingers. min-height: 44px and min-width: 44px set that while letting the button grow with longer text.

How do I add an icon to a button?

Put an inline SVG inside the button next to the text and give the SVG aria-hidden="true", so screen readers read only the words. For a button with only an icon, add aria-label="Close" (or whatever it does) to the button, or screen readers will announce just "button".

Tools that help