W3docs

HTML <button> Tag

The HTML <button> tag creates clickable buttons. Learn the type attribute, styling, the disabled state, and accessibility, with examples.

The <button> tag creates a clickable button on the web page. Unlike a button built from the <input> tag, a <button> can contain rich content — text, images, or other inline HTML — between its opening and closing tags. This makes it the preferred element for most buttons today, because it is easier to style and lets you mix an icon with a label.

This page covers the <button> syntax, the all-important type attribute (and the bug that comes from forgetting it inside a form), styling, the disabled state, accessibility, and how <button> compares with <input>.

Info

In the past, some developers used <input type="button"> instead of <button> because very old versions of Internet Explorer rendered <button> inconsistently. That quirk is long gone — in modern browsers <button> is the recommended choice.

Syntax

The <button> tag comes in pairs. The content is written between the opening (<button>) and closing (</button>) tags.

Example of the HTML <button> tag:

Example of HTML <button> Tag

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <h1>Here will be our button</h1>
    <button type="button">Click</button>
  </body>
</html>

The type attribute

The type attribute decides what a button does. There are three values:

  • submit — submits the parent form.
  • reset — resets all form controls to their initial values.
  • button — does nothing on its own; you wire up behavior with JavaScript.

The most common gotcha: inside a <form>, a <button> with no type defaults to type="submit". So a button you meant as a plain action button will submit the form (often reloading the page) the moment it is clicked. Always set type="button" explicitly for non-submit buttons inside a form.

Example of submit, reset, and button:

Example of the three button types

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <form action="/submit">
      <label>Name: <input type="text" name="name"></label>
      <button type="submit">Submit</button>
      <button type="reset">Reset</button>
      <button type="button" onclick="alert('Just a button!')">Say hi</button>
    </form>
  </body>
</html>

Submit sends the form, Reset clears the text field, and Say hi only runs its JavaScript without touching the form.

Using CSS styles

You can apply CSS styles to the <button> tag to change the appearance of the button, its size, color, text font, and so on.

Example of the <button> tag with CSS styles:

Example of HTML <button> Tag with CSS color and font properties

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .red-text {
        color: red;
      }
      .big-text {
        font: bold 14px Arial;
      }
    </style>
  </head>
  <body>
    Ordinary button
    <button type="button">Add to the recycle bin</button>
    <hr />
    Button with red text
    <button type="button" class="red-text"><b>HTML Book</b></button>
    <hr />
    Button with increased font size
    <button type="button" class="big-text">Download the book</button>
  </body>
</html>

Result

html button styles

The disabled state

A button with the disabled attribute can't be clicked or focused, and it won't fire its event handlers or submit a form. Browsers also grey it out so users can see it is inactive. This is useful while an action is unavailable — for example, until a form is filled in correctly.

Example of a disabled button

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <button type="button">Active button</button>
    <button type="button" disabled>Disabled button</button>
  </body>
</html>

Attributes

Tip

The <button> tag doesn’t have required attributes; however, we recommend always setting type="button" when the tag is used as an ordinary (non-submit) button, because inside a form an unset type defaults to submit.

AttributesValueDescription
autofocusautofocusSpecifies that the button should receive focus after loading the page.
disableddisabledDeactivates the button. (Used when the button should become active after performing some action.)
formform_idSpecifies one or more forms the button belongs to. If the button has multiple forms, then their identifiers (form_id) must be separated by spaces.
formactionURLDefines the address, where the form data will be sent after clicking on the button. (Used only for the buttons with the type="submit" attribute).
formenctypeapplication/x-www-form-urlencodedDefines how the form-data should be encoded when a form is submitted. (Used only for type="submit"). All symbols are encoded before a form is submitted (default value).
multipart/form-dataEncodes data as multipart MIME.
text/plainSpaces are being replaced by the sign "+", but symbols aren’t encoded.
formmethodgetDefines the method of the HTTP request, which will be used when a form is submitted (only for type="submit"). Passes the form data in the address bar ("name = value"), which are added to the URL of the page after the question mark and are separated by an ampersand (&).
postThe browser communicates with the server and sends the data for processing.
formnovalidateformnovalidateSpecifies that the form-data should not be validated on submission (only for type="submit").
formtarget_blankSpecifies where the response will be shown after the form is submitted (only for type="submit"). Opens the response in a new window.
_selfOpens the response in the current window.
_parentOpens the response in the parent frame.
_topOpens the response in the full width window.
namenameDefines the button name.
typebuttonDefines an ordinary button.
resetButton that clears the form from the input data.
submitButton for sending form data.
valuetextDefines the button value.

The <button> tag also supports the Global Attributes and the Event Attributes.

Accessibility: giving a button an accessible name

Every button needs an accessible name — the text a screen reader announces. For a normal text button, the name is simply the text between the tags. The problem appears with icon-only buttons, where there is no visible text.

If the only thing inside the button is an image, give the <img> an alt attribute that describes the action, not the picture. The button then borrows that text as its name:

<button type="button">
  <img src="search.png" alt="Search">
</button>

Here alt="Search" describes what the button does, so a screen reader announces "Search, button".

If the button has no text and no image with alt — for example an icon drawn with an icon font or an inline SVG — add an aria-label so the button still has a name:

<button type="button" aria-label="Close menu">
  <span class="icon-close" aria-hidden="true"></span>
</button>

The aria-label is the button's accessible name; aria-hidden="true" keeps the decorative icon from being announced. Note that aria-label and an inner <img alt> are separate mechanisms — use whichever fits the markup, and never leave an icon-only button without one of them.

To create a clickable link that looks like a button, use an <a> element styled with CSS. Wrapping a <button> inside an <a> is invalid in HTML5. Here is the correct approach:

How to add a link to a button?

<a href="https://example.com" class="button-link">
  Button Label
</a>

In this example, the <a> element is styled to resemble a button. When the user clicks on it, they will be taken to the linked URL.

<button> vs. <input type="button">

You can create a button with either <button> or the <input> tag (type="button", type="submit", or type="reset"). They behave similarly, but differ in what they can hold:

<button><input type="button"> / <input type="submit">
Label sourceContent between the tagsThe value attribute (plain text only)
Rich contentYes — text, images, icons, other inline HTMLNo — text only
StylingEasier (pseudo-elements, nested elements)More limited
Closing tagRequired (</button>)None — it is a void element
<!-- Rich content is possible -->
<button type="submit"><img src="check.png" alt=""> Save</button>

<!-- Text-only, set through value -->
<input type="submit" value="Save">

Use <button> when you want an icon, formatted text, or finer styling control; reach for <input> when a simple text label is all you need.

Practice

Practice
Inside a form, what type does a button element default to when the type attribute is omitted?
Inside a form, what type does a button element default to when the type attribute is omitted?
Was this page helpful?