Definition
The <button> element creates a clickable button and is often used to submit forms and other button functionality. To add functionality when the button is clicked, you will need to associate it with an activation behavior, such as submit, reset, or button.
Example
<button type="button">Log Out</button>Usage
- You must tell browsers the type of button by specifying a
typeattribute when using a<button>element. - You can add text, and phrasing content tags, such as
<b>,<em>,<img>,<q>,<svg>, and<strong>to<button>elements. - If you fail to include a Button element, the web page will fail HTML validation.
Attributes
The Button element supports the Global Attributes, along with the following:
autofocusThe
autofocusattribute specifies that the<button>should be auto-focused when the page document loads. You can only use this on one element per page.autocompleteThe
autocompleteattribute is used to disable a feature unique to Firefox where Firefox remembers the state of input elements when you refresh the page. To disable this feature, set theautocompletevalue to"off".autocomplete="off"disabledThe
disabledattribute prevents the user from interacting with the button. It turns it off. Because Firefox persists the disabled state of a button across page loads, you will need to turnautocompleteoff, as specified above.formThe
formattribute must be identical to theidattribute value of a<form>element that you wish to associate this button to. The<button>element can be anywhere in the document, not just in the<form>element.formactionThe
formactionattribute specifies the URL that processes the information submitted by the form. This overrides theactionattribute of the<form>element.formenctypeThe
formenctypeattribute specifies the method of encoding of information sent to a server for forms withtype=submit. Possible values include:application/x-www-form-urlencodedmultipart/form-datatext/plain
If specified, it overrides the
enctypeattribute of the<form>element.formmethodThe
formmethodattribute specifies the HTTP method used to submit a form where thetypeattribute value is notbutton.Possible values include:
get- This is where the submitted data is included in the HTTP request body.post- This is where the submitted data is appended to a formsactionURL with a?. This is often used for search forms.
If specified, it overrides the
methodattribute of the<form>element.formnovalidateThe
formnovalidateattribute is a Boolean attribute. It specifies whether to validate the form when submitted.If specified, it overrides the
novalidateattribute of the<form>element.formtargetThe
formtargetattribute specifies where to display the response after submitting the form where thetype="submit".Values can include:
_self_blank_parent_self_top- Frame, tab, or window name.
If specified, it overrides the
targetattribute of the<form>element.nameThe
nameattribute specifies the name of the button. It is submitted to the server along with thevalueas part of the form data.typeThe
typeattribute specifies the behavior of the<button>when activated.Values can include:
submit- The button submits the form data to the server. This is the default value if the attribute is not specified, empty, or invalid.reset- This resets all controls to the default values.button- This has no default behavior. It is usually used with JavaScript.
valueThe
valueattribute specifies a value for the button’snameattribute, and is passed to the server when the form is submitted.
Best Practices
- Do not solely use an icon to represent a button. You should include descriptive text that describes the button’s functionality to ensure the button is accessible for assistive technology, such as screen readers.
- If you must have not descriptive text on the button, you should still include it, but hide it using CSS. However, the preferred way is to keep the descriptive text on the button, such as “Sign up”, or “Log out”.
- Buttons should be large enough to be easy to active them, whether with a mouse cursor, stylus, or fingers. A minimum size of 44x44 CSS pixels is recommended.
- Buttons should be sufficiently spaced from each other. This is especially important for users with motor control issues who may inadvertently tap the screen when trying to scroll.
Specification
Browser Support
Desktop
| Chrome | Edge | Firefox | IE | Opera | Safari |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
Mobile
| Android Webview | Chrome Android | Firefox Android | Opera Android | iOS Safari | Samsung Internet |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
