HTML <button> Tag

The <button> tag in HTML defines a clickable button, commonly used in forms or to trigger actions on a webpage. Buttons are essential for user interaction, allowing users to submit data, perform operations, or navigate within the site. They can contain text, images, or both, and can be styled using CSS to match the website's design.

HTML Buttons: Creating Interactive Elements

Syntax

<button type="button|submit|reset" name="buttonName" value="buttonValue" form="form_id" formaction="URL" formenctype="mime_type" formmethod="get|post" formnovalidate="formnovalidate" formtarget="_blank|_self|_parent|_top" autofocus disabled accesskey="character" class="className" contenteditable="true|false" dir="ltr|rtl" draggable="true|false|auto" hidden id="uniqueID" lang="language_code" spellcheck="true|false" style="CSS_properties" tabindex="number" title="text" onclick="script" oncontextmenu="script" ondblclick="script" onmousedown="script" onmouseup="script" onmouseenter="script" onmouseleave="script" onmousemove="script" onmouseover="script" onmouseout="script" onkeydown="script" onkeypress="script" onkeyup="script" onfocus="script" onblur="script">
  Button Text or Content
</button>

Attributes

Attribute Value Description
type button, submit, reset Specifies the type of the button. button for regular buttons, submit for submitting forms, and reset for resetting forms. Defaults to submit if not specified.
name text Specifies the name of the button. This is used when submitting form data.
value text Specifies the value of the button, sent with form data.
form form_id Specifies the ID of the form the button belongs to. This allows a button to be associated with a form outside of its containing form.
formaction URL Overrides the form's action attribute when the button is clicked (for submit buttons).
formenctype application/x-www-form-urlencoded, multipart/form-data, text/plain Specifies how the form data is encoded when submitted, overriding the form's enctype attribute. Only for submit buttons.
formmethod get, post Overrides the form's method attribute, specifying the HTTP method used for submission. Only for submit buttons.
formnovalidate formnovalidate When present, it bypasses form validation on submit. Only for submit buttons.
formtarget _blank, _self, _parent, _top Specifies where to display the response after submitting the form, overriding the form's target attribute. Only for submit buttons.
autofocus autofocus When present, the button gets focus when the page loads. Only one element can have the autofocus attribute on the page.
disabled disabled When present, the button is disabled and not clickable.
accesskey character Specifies a keyboard shortcut to activate the button.
class className Specifies one or more class names for the button, used for CSS styling.
contenteditable true, false Specifies whether the button content is editable by the user.
dir ltr, rtl Specifies the text direction for the content of the button.
draggable true, false, auto Specifies whether the button is draggable.
hidden hidden When present, the button is not visible.
id uniqueID Specifies a unique ID for the button, allowing it to be targeted by JavaScript or CSS.
lang language_code Specifies the language for the button content.
spellcheck true, false Specifies whether to enable spellcheck for the button's content.
style CSS_properties Defines inline CSS styles for the button.
tabindex number Specifies the tabbing order of the button.
title text Specifies extra information about the button, displayed as a tooltip.
onclick script Specifies JavaScript code to execute when the button is clicked.
oncontextmenu script Specifies JavaScript code to execute when the user right-clicks the button.
ondblclick script Specifies JavaScript code to execute when the button is double-clicked.
onmousedown script Specifies JavaScript code to execute when a mouse button is pressed down over the button.
onmouseup script Specifies JavaScript code to execute when a mouse button is released over the button.
onmouseenter script Specifies JavaScript code to execute when the mouse pointer enters the button.
onmouseleave script Specifies JavaScript code to execute when the mouse pointer leaves the button.
onmousemove script Specifies JavaScript code to execute when the mouse pointer is moving while over the button.
onmouseover script Specifies JavaScript code to execute when the mouse pointer is moved onto the button.
onmouseout script Specifies JavaScript code to execute when the mouse pointer is moved away from the button.
onkeydown script Specifies JavaScript code to execute when a key is pressed down while the button has focus.
onkeypress script Specifies JavaScript code to execute when a key is pressed down and released while the button has focus.
onkeyup script Specifies JavaScript code to execute when a key is released while the button has focus.
onfocus script Specifies JavaScript code to execute when the button gains focus.
onblur script Specifies JavaScript code to execute when the button loses focus.

Example

<button type="button">Click Me!</button>

More Examples

Basic Button Types

<button type="button">Regular Button</button>
<button type="submit">Submit Button</button>
<button type="reset">Reset Button</button>

This example demonstrates the three main types of buttons: a regular button, a form submission button, and a form reset button.

Button with Name and Value

<button type="submit" name="action" value="save">Save</button>

This button, when submitted as part of a form, will send the name "action" with the value "save" to the server.

Button within a Form

<form action="/submit" method="post">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username"><br><br>
  <button type="submit">Register</button>
</form>

This demonstrates a submit button used within a form, sending form data to /submit using a POST request.

Button with Styling and JavaScript

<button type="button" style="background-color: lightblue; padding: 10px;" onclick="alert('Button Clicked!')">Styled Button</button>

This button has inline styling and JavaScript to display an alert when clicked.

Button using other attributes:

<form id="myForm" action="/submit" method="post">
    <input type="text" name="data" value="some_value">
</form>

<button type="submit" form="myForm" formaction="/another_submit" formmethod="get" formtarget="_blank">Submit Form Elsewhere</button>

This button demonstrates how to use the form, formaction, formmethod and formtarget attributes to override form attributes and submits it to /another_submit on a new tab using GET method.

<button type="button" disabled>Disabled Button</button>
<button type="button" accesskey="a">Activate me with Alt+A</button>
<button type="button" title="Click me to do something">Hover me</button>

The first button will be disabled and non clickable, the second can be triggered using alt+A and the third shows a tooltip on hover.

Browser Support

The <button> tag is supported by all major browsers:

  • Chrome
  • Edge
  • Firefox
  • Safari
  • Opera

Notes and Tips

  • Use the correct type attribute to define the button's behavior. Using type="button" helps avoid unexpected behavior.
  • The <button> tag is more versatile than <input type="button"> because it can contain HTML elements like images or spans within its content.
  • When using submit buttons inside a form, ensure you provide a name attribute to handle it at the backend.
  • Always add a proper title attribute to make it accessible and SEO friendly.
  • Use CSS classes to style your buttons consistently throughout your site.
  • Make sure your buttons have enough padding to make them easier to click, especially on mobile devices.
  • Leverage JavaScript to create interactive and dynamic behaviors with buttons.
  • Test your buttons on different devices and browsers for a consistent user experience.