The <button>
represents a clickable button, which can be used in forms or anywhere in a document that needs simple, standard button functionality. By default, HTML buttons are typically presented in a style similar to that of the host platform the user agent is running on, but you can change the appearance of the button using CSS.
Content categories | Flow content, phrasing content, Interactive content, listed, labelable, and submittable form-associated element, palpable content. |
---|---|
Permitted content | Phrasing content but there must be no Interactive content |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parents | Any element that accepts phrasing content. |
Permitted ARIA roles |
checkbox , link , menuitem , menuitemcheckbox , menuitemradio , radio , switch , tab
|
DOM interface | HTMLButtonElement |
This element's attributes include the global attributes.
autofocus
HTML5
autocomplete
<button>
is nonstandard and Firefox-specific. By default, unlike other browsers, Firefox persists the dynamic disabled state of a <button>
across page loads. Setting the value of this attribute to off
(i.e. autocomplete="off"
) disables this feature. See bug 654072.disabled
This Boolean attribute indicates that the user cannot interact with the button. If this attribute is not specified, the button inherits its setting from the containing element, for example <fieldset>
; if there is no containing element with the disabled attribute set, then the button is enabled.
Firefox will, unlike other browsers, by default, persist the dynamic disabled state of a <button>
across page loads. Use the autocomplete
attribute to control this feature.
form
HTML5
<form>
element in the same document. If this attribute is not specified, the <button>
element will be associated to an ancestor <form>
element, if one exists. This attribute enables you to associate <button>
elements to <form>
elements anywhere within a document, not just as descendants of <form>
elements.formaction
HTML5
action
attribute of the button's form owner.formenctype
HTML5
application/x-www-form-urlencoded
: The default value if the attribute is not specified.multipart/form-data
: Use this value if you are using an <input>
element with the type
attribute set to file
.text/plain
If this attribute is specified, it overrides the enctype
attribute of the button's form owner.
formmethod
HTML5
post
: The data from the form are included in the body of the form and sent to the server.get
: The data from the form are appended to the form attribute URI, with a '?' as a separator, and the resulting URI is sent to the server. Use this method when the form has no side-effects and contains only ASCII characters.If specified, this attribute overrides the method
attribute of the button's form owner.
formnovalidate
HTML5
novalidate
attribute of the button's form owner.formtarget
HTML5
target
attribute of the button's form owner. The following keywords have special meanings: _self
: Load the response into the same browsing context as the current one. This value is the default if the attribute is not specified._blank
: Load the response into a new unnamed browsing context._parent
: Load the response into the parent browsing context of the current one. If there is no parent, this option behaves the same way as _self
._top
: Load the response into the top-level browsing context (that is, the browsing context that is an ancestor of the current one, and has no parent). If there is no parent, this option behaves the same way as _self
.name
type
submit
: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.reset
: The button resets all the controls to their initial values.button
: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.value
<button>
elements are much easier to style than <input>
elements. You can add inner HTML content (think <em>
, <strong>
or even <img>
), and make use of ::after
and ::before
pseudo-element to achieve complex rendering while <input>
only accepts a text value attribute.
If your buttons are not to submit 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.
IE7 has a bug where when submitting a form with <button type="submit" name="myButton" value="foo">Click me</button>
, the POST
data sent will result in myButton=Click me
instead of myButton=foo
.
IE6 has an even worse bug where submitting a form through a button will submit ALL buttons of the form, with the same bug as IE7.
This bug has been fixed in IE8.
Firefox will, unlike other browsers, by default, persist the dynamic disabled state of a <button>
across page loads. Setting the value of the autocomplete
attribute to off
disables this feature. See bug 654072.
Firefox <35 for Android sets a default background-image
gradient on all buttons (see bug 763671). This can be disabled using background-image: none
.
<button name="button">Click me</button>
Buttons that only use an icon to represent functionality do not have an accessible name. Accessible names provide a programmatic hook 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, supply a string of text for the <button>
element that concisely describes the button's functionality.
<button name="favorite" type="button"> <svg aria-hidden="true" viewBox="0 0 10 10"><path d="m7.4 8.8-2.4-1.3-2.4 1.3.46-2.7-2-1.9 2.7-.39 1.2-2.5 1.2 2.5 2.7.39-1.9 1.9z"/></svg> Add to favorites </button>
If you want to visually hide the button's text, an accessible way to do so is to use a combination of properties to remove it visually from the screen but keep it parseable by assistive technology.
However, it is worth noting that leaving the button text visually apparent can aid people who may not be familiar with the icon's meaning or understand the button's purpose. This is especially relevant for people who are not as technologically sophisticated, or who may have different cultural interpretations for the imagery the icon button uses.
Large amounts of interactive content—including buttons—placed in close visual proximity to each other should have space inserted to separate 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
.
Firefox will add a small dotted border on a focused button. This border is declared through CSS, in the browser stylesheet, but you can override it if necessary to add your own focused style using button
.::-moz-focus-inner
{ }
If overridden, it is important to ensure that the state change when focus is moved to the button is high enough that people experiencing low vision conditions will be able to perceive it.
Color contrast ratio is determined by comparing the luminosity of the button text and background color values compared to the background the button is placed on. In order to meet current Web Content Accessibility Guidelines (WCAG), a ratio of 4.5:1 is required for text content and 3:1 for larger text such as headings. Large text is defined as 18.66px and bold
or larger, or 24px or larger.
Whether clicking on a <button>
causes it to (by default) become focused varies by browser and OS. The results for <input>
of type="button"
and type="submit"
were the same.
Desktop Browsers | Windows 8.1 | OS X 10.X |
---|---|---|
Firefox | Yes - Firefox 30.0 | No (even with a tabindex ) Firefox 63 |
Chrome | Yes - Chrome 35 | Yes - Chrome 65 |
Safari | N/A | No (even with a tabindex ) Safari 12 |
Internet Explorer | Yes - Internet Explorer 11 | N/A |
Presto | Yes - Opera 12 | Yes - Opera 12 |
Mobile Browsers | iOS 7.1.2 | Android 4.4.4 |
---|---|---|
Safari Mobile | No (even with a tabindex ) | N/A |
Chrome 35 | No (even with a tabindex ) | Yes |
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of '<button>' in that specification. | Living Standard | |
HTML5 The definition of '<button>' in that specification. | Recommendation | |
HTML 4.01 Specification The definition of '<button>' in that specification. | Recommendation |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | Yes | Yes | Yes | Yes |
autofocus |
5 | Yes | 4 | 10 | 9.6 | 5 |
autocomplete
|
No | No | Yes | No | No | No |
disabled |
Yes | Yes | Yes | Yes | Yes | Yes |
form |
Yes | 16 | Yes | No | Yes | Yes |
formaction |
9 | Yes | 4 | 10 | ? | ? |
formenctype |
9 | Yes | 4 | 10 | 10.6 | ? |
formmethod |
9 | Yes | 4 | 10 | ? | ? |
formnovalidate |
Yes | Yes | Yes | Yes | Yes | Yes |
formtarget |
Yes | Yes | Yes | Yes | Yes | Yes |
name |
Yes | Yes | Yes | Yes | Yes | Yes |
type |
Yes | Yes | Yes | Yes | Yes | Yes |
value |
Yes | Yes | Yes | Yes | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
autofocus |
? | ? | ? | 4 | ? | ? | ? |
autocomplete
|
No | No | No | Yes | No | No | No |
disabled |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
form |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
formaction |
? | ? | Yes | 4 | ? | ? | ? |
formenctype |
? | ? | Yes | 4 | ? | ? | ? |
formmethod |
? | ? | Yes | 4 | ? | ? | ? |
formnovalidate |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
formtarget |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
name |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
type |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
value |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
Other elements that are used for creating forms: <form>
, <datalist>
, <fieldset>
, <input>
,<keygen>
, <label>
, <legend>
, <meter>
, <optgroup>
, <option>
, <output>
, <progress>
, <select>
, <textarea>
.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button