<button>
Definition
The HTML button
element is used to create a clickable button on a web page. It can be used to submit forms, trigger events, or perform other actions.
Examples
<form>
<label for="submit-button">Submit Button:</label>
<button id="submit-button" type="submit">Submit</button>
<label for="reset-button">Reset Button:</label>
<button id="reset-button" type="reset">Reset</button>
<label for="custom-button">Custom Button:</label>
<button id="custom-button" type="button">Click me!</button>
</form>
In the above example, three button
elements are used within a form
element. The first button has a type
attribute set to submit
, which will submit the form when clicked. The second button has a type
attribute set to reset
, which will reset the form to its default values when clicked. The third button has a type
attribute set to button
, which means it will not perform any default action when clicked. Instead, it can be used to trigger a custom JavaScript function. Each button
element also has a label
element associated with it to provide a descriptive label for the button.
Attributes
Attribute | Description | Deprecated |
---|---|---|
autofocus | Specifies that the button should automatically be focused | No |
disabled | Specifies that the button should be disabled | No |
name | Specifies a name for the button, which can be used to identify the button and its associated data when the form is submitted | No |
type | Specifies the type of button, such as submit or reset | No |
value | Specifies a value for the button, which can be used to identify the button and its associated data when the form is submitted | No |
form | Specifies the form the button belongs to | No |
formaction | Specifies the URL to be used for the form submission | No |
formenctype | Specifies the encoding type for the form data | No |
formmethod | Specifies the HTTP method to use for the form submission | No |
formnovalidate | Specifies that the form should not be validated | No |
formtarget | Specifies where to display the response after form submission | No |
Best Practices
Best practices for using the HTML button
element include the following:
- Use the
button
element only for clickable buttons that trigger an action, such as submitting a form or performing a JavaScript function. - Use the
input
element with thetype="button"
attribute for buttons that do not perform an action, such as a reset button that resets a form to its default values. - Use the
button
element'stype
attribute to specify the type of button, such assubmit
for a button that submits a form orreset
for a button that resets a form. - Use the
button
element'sname
andvalue
attributes to specify a name and value for the button, which can be used to identify the button and its associated data when the form is submitted. - Use the
button
element'sdisabled
attribute to disable the button, preventing it from being clicked, if necessary.
Accessibility Considerations
Accessibility considerations for the HTML button
element may include:
- Use the
label
element to provide a descriptive label for the button, which can be read by screen readers and other assistive technologies. - Use the
button
element'saria-label
attribute to provide a label for the button if the button's text is not descriptive or if the button has no text at all. - Avoid using images as the only content of a
button
element, as they may not be easily understood by assistive technologies.
Browser Compatibility
Chrome | Firefox | Safari | Internet Explorer | Microsoft Edge | Opera |
---|---|---|---|---|---|
Yes | Yes | Yes | Yes | Yes | Yes |