Definition and Usage
The <button> tag represents a button labeled by its contents.
This tag creates a clickable button on a web page, and the text or image inside the button indicates its function. The type attribute specifies the button's control type.
Basic Example
<button type="submit">Submit Feedback</button>
Specifying the Control Type of a Button with the type Attribute
The <button> tag specifies the control type based on the value of the type attribute. The possible keyword values are as follows:
submit |
A form (<form>) submission button. Clicking this button submits the values of all submittable controls in the form containing the element. This is the default value of the type attribute for the <button> tag. If the type attribute is not specified, the default value is submit. |
|---|---|
reset |
A form (<form>) reset button. Clicking this button resets the values of all controls in the form containing the element. |
button |
The button has no default behavior and does nothing by default when clicked. It represents a generic button control and is primarily used with JavaScript to perform various actions when clicked. |
- Controls
- Refers to elements that allow users to interact with a web page.
The<button>tag, along with the<input>,<textarea>, and<select>tags, are typical control elements that allow users to enter data, make selections, or click.
Examples Based on the Value of the type Attribute
Here are examples based on the type attribute values of the <button> tag.
<button type="submit">
<button type="submit"> is a form (<form>) submission button. Clicking this button submits the values of all submittable controls in the form containing the element.
This is the default value of the type attribute for the <button> tag. If the type attribute is not specified, the default value is submit. That is, a <button> without a specified type attribute is equivalent to <button type="submit">.
<form action="submit_url">
<label for="user-name">Name</label>
<input type="text" id="user-name" name="user-name">
<button type="submit">Submit</button>
</form>
<button type="reset">
<button type="reset"> is a form (<form>) reset button. Clicking this button resets the values of all controls in the form containing the element.
Note!
Resetting the values of all controls in a form means returning the values entered or set by the user to their initial values. It does not mean clearing all input values.
In the following example, no value is set for <input type="text">. In other words, its value is empty. After entering a value, when the user clicks <button type="reset">, the <input type="text"> value resets to its initial empty state.
<form action="submit_url">
<label for="user-name">Name</label>
<input type="text" id="user-name" name="user-name">
<button type="reset">Reset</button>
</form>
Unlike the example above, the following example uses the value attribute to set the initial value of <input type="text"> to John Doe. In this case, clicking <button type="reset"> without changing the value will leave John Doe unchanged. However, if the user changes John Doe to a different value and clicks <button type="reset">, the field resets to its initial value, John Doe.
<form action="submit_url">
<label for="user-name">Name</label>
<input type="text" id="user-name" name="user-name" value="John Doe">
<button type="reset">Reset</button>
</form>
<button type="button">
<button type="button"> has no default behavior and does nothing by default when clicked. It represents a generic button control and is primarily used with JavaScript to perform various actions when the user clicks it.
<button type="button"> can also be used inside a form (<form>), but it can be placed anywhere in a document where general button functionality is needed.
<button type="button" onclick="msg();">Click Me</button>
<!-- Primarily used with JavaScript -->
<script>
const msg = () => {
alert("Welcome.");
}
</script>
Using Related Attributes
In addition to the type attribute, the <button> tag has several related attributes.
disabled Attribute
Use the disabled attribute to disable a button. It prevents user interactions with the button, such as pressing or clicking.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<button type="button" disabled>Disabled Button</button>
autofocus Attribute
Specifies that the control should receive input focus when the page loads. It should be used on only one control element per HTML document. If used multiple times, only the first autofocus attribute encountered is applied. This attribute automatically focuses the control.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<button type="button" autofocus>Focused Button</button>
Even if the autofocus attribute is set, if the user focuses an element other than the one with the autofocus attribute, that element loses focus (blurs).
Browser Compatibility Warning:
On iOS, the autofocus attribute is not supported in any browser due to Apple's policy.
See caniuse.com for more detailed information.
formnovalidate Attribute
Indicates that the form (<form>) should not be validated when submitted with <button type="submit">. It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
Note!
formnovalidate attribute is also available on <input type="image"> and <input type="submit">.
There are two types of form validation:
- Built-in HTML5 validation
- Checking for values in controls with the
requiredattribute
Built-in HTML5 Validation
When used inside a <form> tag, browsers validate <input type="url"> and <input type="email"> by default.
As a built-in HTML5 feature, <input type="url"> automatically checks whether the user entered a URL in the correct format, while <input type="email"> automatically checks whether the user entered an email address in the correct format.
To disable this built-in validation, you can add the novalidate attribute to the <form> tag or add the formnovalidate attribute to <button type="submit">.
Here, we will cover adding the formnovalidate attribute to <button type="submit">.
The following example compares a <button type="submit"> without the formnovalidate attribute set against one that explicitly includes it.
<h3>Validation Performed</h3>
<p>Checks whether the user entered a URL in the correct format</p>
<form>
<label for="user-url">Website Address</label>
<input type="url" id="user-url" name="user-url">
<button type="submit">Submit</button>
</form>
<h3>Validation Not Performed</h3>
<p>Does not check whether the user entered a URL in the correct format</p>
<form>
<label for="user-url-1">Website Address</label>
<input type="url" id="user-url-1" name="user-url-1">
<button type="submit" formnovalidate>Submit</button>
</form>
Note: If you do not enter a URL, the form will be submitted without validation.
Validation Performed
Checks whether the user entered a URL in the correct format
Validation Not Performed
Does not check whether the user entered a URL in the correct format
Checking for Values in Controls with the required Attribute
The required attribute specifies that a control is mandatory, requiring the user to enter or select a value before submitting the form (<form>).
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
The following example compares a <button type="submit"> without the formnovalidate attribute set against one that explicitly includes it.
<h3>required Attribute Validation Performed</h3>
<p>Checks whether a value is specified for the required attribute</p>
<form>
<label for="user-name">Name</label>
<input type="text" id="user-name" name="user-name" required>
<button type="submit">Submit</button>
</form>
<h3>required Attribute Validation Not Performed</h3>
<p>Skips checking for the required value due to formnovalidate</p>
<form>
<label for="user-name-1">Name</label>
<input type="text" id="user-name-1" name="user-name-1" required>
<button type="submit" formnovalidate>Submit</button>
</form>
required attribute validation is performed!
required Attribute Validation Performed
Checks whether a value is specified for the required attribute
required Attribute Validation Not Performed
Skips checking for the required value due to formnovalidate
name Attribute
The name attribute specifies the name of the button.
When a form (<form>) is submitted, this name can be used to send data alongside the value attribute of a <button type="submit">. In JavaScript, the value of the name attribute is referenced to identify the corresponding element within the form.elements API.
value Attribute
The value attribute specifies the value that is sent along with the button's name when the form (<form>) is submitted.
The button used to submit the form (<form>) is <button type="submit">. The value of the value attribute specified for the button used to submit the form (<form>) is used to distinguish which submit button was used to submit the data submitted by the form (<form>) and what its value is. This is especially useful when there are multiple submit buttons, as in the example below.
<form action="submit_url" method="post">
<p>
<label for="user-name">Name</label>
<input type="text" id="user-name" name="user-name">
</p>
<p>
<!-- Each button sends a different value -->
<button type="submit" name="action" value="save">Save</button>
<button type="submit" name="action" value="delete">Delete</button>
</p>
</form>
form Attribute
The form attribute associates the <button> on which this attribute is specified with a specific <form> in the document.
It can be used with all type attribute values of the <button>.
Accessibility Concerns (Using Icon Buttons)
Icon-only buttons lack an accessible name. An accessible name plays a critical role in enabling users—especially those with visual impairments—to understand and interact with a button's functionality using screen readers and other assistive technologies.
Without an accessible name, as shown in the example below, users with visual impairments are unable to interact with the button effectively.
<style>
.svg {
display: block;
width: 1.2rem;
height: 1.2rem;
}
.magnifier-glasses {
fill: transparent;
stroke-width: 1.4;
stroke: #222;
}
.magnifier-handle {
stroke-dashoffset: -7.5;
stroke-dasharray: 7.9, 20;
fill: transparent;
stroke-width: 2.2;
stroke: #222;
}
</style>
<button type="submit">
<svg xmlns="http://www.w3.org/2000/svg" class="svg" viewBox="0 0 20 20">
<circle class="magnifier-glasses" cx="8.2" cy="8.2" r="7.2" />
<path class="magnifier-handle" d="M 8.2 8.2 L 22 22" />
</svg>
</button>
In such cases, you can provide an accessible name for the <button> using the aria-label attribute.
The aria-label attribute, from a web accessibility perspective, uses its text value to provide an accessible name for the current element to users of assistive technologies, such as screen readers.
<style>
.svg {
display: block;
width: 1.2rem;
height: 1.2rem;
}
.magnifier-glasses {
fill: transparent;
stroke-width: 1.4;
stroke: #222;
}
.magnifier-handle {
stroke-dashoffset: -7.5;
stroke-dasharray: 7.9, 20;
fill: transparent;
stroke-width: 2.2;
stroke: #222;
}
</style>
<button type="submit" aria-label="Submit Search">
<svg xmlns="http://www.w3.org/2000/svg" class="svg" viewBox="0 0 20 20">
<circle class="magnifier-glasses" cx="8.2" cy="8.2" r="7.2" />
<path class="magnifier-handle" d="M 8.2 8.2 L 22 22" />
</svg>
</button>
While this makes no visible difference for sighted users, it helps screen reader users understand and navigate the content much more easily and efficiently.
Technical Summary
| Permitted parents | Any element that accepts phrasing content. |
|---|---|
| Permitted content | Phrasing content, but there must be no interactive content descendant and no descendant with the tabindex attribute specified. |
Implicit ARIA role |
button |
Permitted ARIA roles |
checkbox, combobox, link, menuitem, menuitemcheckbox, menuitemradio, option, radio, switch, tab, (button is also allowed, but NOT RECOMMENDED.) |
Things to Keep in Mind
These are some key considerations when using the <button> tag that are common but easy to overlook.
Specifying the type Attribute
The default value of the type attribute for the <button> tag is submit.
When using a button for a purpose other than submitting a form (<form>), if the type attribute is not explicitly specified, the default value submit is applied. Therefore, if a button intended to be used as a generic button inside a <form> tag does not have the type attribute specified, clicking it may cause the form to be submitted.
When using a generic button outside a <form> tag, specify button as the value of the type attribute.
Because both submit and reset are related to form submission, they are not appropriate from either a semantic or accessibility perspective.
Unless the button is intended solely for form submission, be sure to specify the appropriate type attribute.
Properly Distinguishing Links and Buttons
Using the <button> tag for the sole purpose of navigating to another page is not recommended.
Technically, you can use JavaScript with the <button> tag to simply navigate to another page, but this can confuse search engines and may result in wasted resources due to the use of JavaScript.
Even if the UI design looks like a button, it is recommended to use the <a> tag for simple page navigation from an HTML semantic perspective.
Browser Compatibility
| Tag and Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<button>
|
1 | 12 | 1 | 4 |
type
|
1 | 12 | 1 | 4 |
disabled
|
1 | 12 | 1 | 4 |
formnovalidate
|
9 | 12 | 4 | 5.1 |
value
|
1 | 12 | 1 | 4 |
Specifications
| Specification | |
|---|---|
<button>
|
HTML Standard #the-button-element |