Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
The <button> HTML element is an interactive element activated by a user with a mouse, keyboard, finger, voice command, or other assistive technology. Once activated, it then performs an action, such as submitting a form or opening a dialog.
By default, HTML buttons are presented in a style resembling the platform the user agent runs on, but you can change buttons' appearance with CSS.
This element's attributes include the global attributes.
autofocusThis Boolean attribute specifies that the button should have input focus when the page loads. Only one element in a document can have this attribute.
commandSpecifies the action to be performed on an element being controlled by a control <button> specified via the commandfor attribute. The possible values are:
"show-modal"The button will show a <dialog> as modal. If the dialog is already modal, no action will be taken. This is a declarative equivalent of calling the HTMLDialogElement.showModal() method on the <dialog> element.
"close"The button will close a <dialog> element. If the dialog is already closed, no action will be taken. This is a declarative equivalent of calling the HTMLDialogElement.close() method on the <dialog> element. When used with the value attribute, the button's value will be passed as the dialog's returnValue property.
"request-close"The button will trigger a cancel event on a <dialog> element to request that the browser dismiss it, followed by a close event. This differs from the close command in that authors can call Event.preventDefault() on the cancel event to prevent the <dialog> from closing. If the dialog is already closed, no action will be taken. This is a declarative equivalent of calling the HTMLDialogElement.requestClose() method on the <dialog> element. When used with the button's value attribute, the value will be passed as the dialog's returnValue property.
"show-popover"The button will show a hidden popover. If you try to show an already showing popover, no action will be taken. See Popover API for more details. This is equivalent to setting a value of show for the popovertargetaction attribute, and also provides a declarative equivalent to calling the HTMLElement.showPopover() method on the popover element.
"hide-popover"The button will hide a showing popover. If you try to hide an already hidden popover, no action will be taken. See Popover API for more details. This is equivalent to setting a value of hide for the popovertargetaction attribute, and also provides a declarative equivalent to calling the HTMLElement.hidePopover() method on the popover element.
"toggle-popover"The button will toggle a popover between showing and hidden. If the popover is hidden, it will be shown; if the popover is showing, it will be hidden. See Popover API for more details. This is equivalent to setting a value of toggle for the popovertargetaction attribute, and also provides a declarative equivalent to calling the HTMLElement.togglePopover() method on the popover element.
Custom valuesThis attribute can represent custom values that are prefixed with a two hyphen characters (--). Buttons with a custom value will dispatch the CommandEvent on the controlled element.
commandforTurns a <button> element into a command button, controlling a given interactive element by issuing the command specified in the button's command attribute. The commandfor attribute takes the ID of the element to control as its value. This is a more general version of popovertarget.
disabledThis Boolean attribute prevents the user from interacting with the button: it cannot be pressed or focused.
formThe <form> element to associate the button with (its form owner). The value of this attribute must be the id of a <form> in the same document. (If this attribute is not set, the <button> is associated with its ancestor <form> element, if any.)
This attribute lets you associate <button> elements to <form>s anywhere in the document, not just inside a <form>. It can also override an ancestor <form> element.
formactionThe URL that processes the information submitted by the button. Overrides the action attribute of the button's form owner. Does nothing if there is no form owner.
formenctypeIf the button is a submit button (it's inside/associated with a <form> and doesn't have type="button"), specifies how to encode the form data that is submitted. Possible values:
If this attribute is specified, it overrides the enctype attribute of the button's form owner.
formmethodIf the button is a submit button (it's inside/associated with a <form> and doesn't have type="button"), this attribute specifies the HTTP method used to submit the form. Possible values:
If specified, this attribute overrides the method attribute of the button's form owner.
formnovalidateIf the button is a submit button, this Boolean attribute specifies that the form is not to be validated when it is submitted. If this attribute is specified, it overrides the novalidate attribute of the button's form owner.
This attribute is also available on <input type="image"> and <input type="submit"> elements.
formtargetIf the button is a submit button, this attribute is an author-defined name or standardized, underscore-prefixed keyword indicating where to display the response from submitting the form. This is the name of, or keyword for, a browsing context (a tab, window, or <iframe>). If this attribute is specified, it overrides the target attribute of the button's form owner. The following keywords have special meanings:
Defines the <button> element as an interest invoker. Its value is the id of a target element, which will be affected in some way (normally shown or hidden) when interest is shown or lost on the invoker element (for example, by hovering/unhovering or focusing/blurring it). See Using interest invokers for more details and examples.
nameThe name of the button, submitted as a pair with the button's value as part of the form data, when that button is used to submit the form.
popovertargetTurns a <button> element into a popover control button; takes the ID of the popover element to control as its value. Establishing a relationship between a popover and its invoker button using the popovertarget attribute has two additional useful effects:
Specifies the action to be performed on a popover element being controlled by a control <button>. Possible values are:
"hide"The button will hide a shown popover. If you try to hide an already hidden popover, no action will be taken.
"show"The button will show a hidden popover. If you try to show an already showing popover, no action will be taken.
"toggle"The button will toggle a popover between showing and hidden. If the popover is hidden, it will be shown; if the popover is showing, it will be hidden. If popovertargetaction is omitted, "toggle" is the default action that will be performed by the control button.
typeThe default behavior of the button. Possible values are:
Defines the value associated with the button's name when it's submitted with the form data. This value is passed to the server in params when the form is submitted using this button. When used with the close or request-close commands, the value attribute sets the returnValue of the <dialog> element being controlled.
A submit button with the attribute formaction set, but without an associated form does nothing. You have to set a form owner, either by wrapping it in a <form> or set the attribute form to the id of the form.
<button> elements are much easier to style than <input> elements. You can add inner HTML content (think <i>, <br>, or even <img>), and use ::after and ::before pseudo-elements for complex rendering.
If your buttons are not for submitting form data to a server, be sure to set their type attribute to button. Otherwise, they will try to submit form data and to load the (nonexistent) response, possibly destroying the current state of the document.
While <button type="button"> has no default behavior, event handlers can be scripted to trigger behaviors. An activated button can perform programmable actions using JavaScript, such as removing an item from a list.
By default, user agents style buttons as display: flow-root, which establishes a new block formatting context and centers the button's children both horizontally and vertically as long as they do not overflow. If the button is defined as a flex or grid container, the children will behave as flex or grid items. A button set to display: inline will be styled as if the value were set to display: inline-block.
Buttons that only display an icon do not have an accessible name. Accessible names provide information for assistive technology, such as screen readers, to access when they parse the document and generate an accessibility tree. Assistive technology then uses the accessibility tree to navigate and manipulate page content.
To give an icon button an accessible name, put text in the <button> element that concisely describes the button's functionality.
If you want to visually hide the button's text, an accessible way to do so is to use a combination of CSS properties to remove it visually from the screen, but keep it parsable by assistive technology.
However, it is worth noting that leaving the button text visible can help people who may not be familiar with the icon's meaning or understand the button's purpose. This is especially important for people who are not technologically sophisticated or who may have different cultural interpretations of the icon the button uses.
Interactive elements such as buttons should have an area large enough to be easy to activate. This helps a variety of people, including people with motor control issues and people using non-precise forms of input such as a stylus or fingers. A minimum interactive size of 44×44 CSS pixels is recommended.
Large amounts of interactive content — including buttons — placed in close visual proximity to each other should have space separating them. This spacing is beneficial for people who are experiencing motor control issues, who may accidentally activate the wrong interactive content.
Spacing may be created using CSS properties such as margin.
To describe the state of a button the correct ARIA attribute to use is aria-pressed and not aria-checked or aria-selected. To find out more read the information about the ARIA button role.
It is best not to override the default focus ring for elements that have focus. If the button styles are overridden, it is important to ensure that the focus state has enough contrast so that people experiencing low vision conditions can perceive it and people with cognitive differences will understand it.
The :focus-visible pseudo-class can be used to apply styles to an element that has :focus only when the user agent's heuristics determine that the focus should be highlighted, such as when a <button> receives keyboard focus. See :focus vs :focus-visible for more information.
Color contrast ratio is determined by comparing the luminosity of the button text and background color values to the background the button is placed on. To meet current Web Content Accessibility Guidelines (WCAG), a ratio of 4.5:1 is required for text content and 3:1 for large text. (Large text is defined as 18.66px and bold or larger, or 24px or larger.)
Whether clicking on a <button> or <input> button types causes it to (by default) become focused varies by browser and OS. Most browsers do give focus to a button being clicked, but Safari does not, by design.
This example creates a clickable button. The type="button" attribute ensures the button has no default behavior. You can make this button interactive using JavaScript or attributes such as command and commandfor.
The dialog in this example has two radio buttons that control whether or not the dialog can be closed. Select Yes or No, and then click Request to Close to try to close the dialog. If Yes is selected, the dialog closes; if No is selected, the dialog stays open and shows a message instead.
The Open Dialog button opens the <dialog> element using command="show-modal".
The Request to Close button has command="request-close", which targets the <dialog> element using the commandfor="mydialog" attribute. When it's clicked, it asks the <dialog> if it can be closed (unlike the command="close" attribute, which would close the <dialog> immediately). This checks if the <dialog> is cancelable using a cancel event.
When the event is cancelable, the value of the radio buttons is checked:
This example demonstrates how to use the button value attribute with close command to populate the value of a dialog's returnValue property.
When either the Cancel or Delete button is clicked, the dialog closes and sets its returnValue to the button's value attribute. The close event listener checks dialog.returnValue to determine which action the user chose and logs the result to the screen.
The HTML first defines a Delete Record button that uses the commandfor attribute to specify the dialog to be opened.
Within the dialog Cancel and Delete buttons use the commandfor attribute to indicate that they apply to the current dialog. They also set the command attribute to "close" and set the value attribute to "cancel" and "delete" respectively — the value of the selected button is automatically copied to the dialog returnValue when the button is clicked.
The code uses a close event listener to log the dialog's returnValue.
| Flow content, phrasing content, Interactive content, listed, labelable, and submittable form-associated element, palpable content. |
| Phrasing content but there must be no Interactive content. If the <button> is the first child of a customizable select element, then it may also contain zero or one <selectedcontent> element. |
| None, both the starting and ending tag are mandatory. |
| Any element that accepts phrasing content. |
| button |
| checkbox, combobox, link, menuitem, menuitemcheckbox, menuitemradio, option, radio, switch, tab |
| HTMLButtonElement |
| HTML # the-button-element |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Apr 24, 2026 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.