<button>
Creates interactive button elements
Standard HTMLQuick Answer
What is the HTML <button> tag?
The <button> tag creates clickable buttons for form submissions or JavaScript actions. The type attribute specifies behavior: "submit" (form submission), "reset" (form reset), or "button" (custom JavaScript). Buttons support text, images, and icons, offering more styling flexibility than <input type="button"> elements.
Definition and Usage
The <button> element establishes a clickable
button. Inside a <button> element you can put
text (and tags like <i>,
<b>, <strong>,
<br>, <img>, etc.). This is
not possible with a button created with the
<input> element.
Always specify the type attribute for a
<button> element to tell browsers what type of
button it is.
<button> element is
much easier to style than the <input> element.
You can add inner HTML content (think <i>,
<br>, or even <img>).
<button> element in an HTML form, different
browsers may submit different values. Use
<input> to create buttons in an HTML form.
Browser support and standards
Browser rendering and modern HTML conformance are different questions. Check the current element reference for the browsers and features your project needs; the older source version table is not a current compatibility guarantee.
Read the MDN element referenceAttributes
| Attribute | Value | Description |
|---|---|---|
| autofocus | autofocus | Specifies that a button should automatically get focus when the page loads |
| disabled | disabled | Specifies that a button should be disabled |
| form | form_id | Specifies which form the button belongs to |
| formaction | URL | Specifies where to send the form-data when a form is submitted (only for type="submit") |
| formenctype |
application/x-www-form-urlencoded multipart/form-data text/plain |
Specifies how form-data should be encoded before sending it to a server (only for type="submit") |
| formmethod | get post |
Specifies how to send the form-data (only for type="submit") |
| formnovalidate | formnovalidate | Specifies that the form-data should not be validated on submission (only for type="submit") |
| formtarget |
_blank _self _parent _top framename |
Specifies where to display the response after submitting the form (only for type="submit") |
| name | name | Specifies a name for the button |
| type | button reset submit |
Specifies the type of button |
| value | text | Specifies an initial value for the button |
Global Attributes
The <button> tag also supports all HTML global
attributes such as class, id,
style, etc.
Examples
Basic Button
A simple clickable button:
Example
<button>Click Me!</button>
Button with Type Attribute
Different button types:
Example
<button type="button">Click Me</button>
<button type="submit">Submit Form</button>
<button type="reset">Reset Form</button>
Disabled Button
A button that cannot be clicked:
Example
<button disabled>Disabled Button</button>
Button with Icons
Buttons can contain HTML content:
Example
The snippet includes CSS inside its HTML. Unavailable or blocked: image “icon.png”. Provide inline styles and scripts, embedded images or the missing HTML context. External requests and browser dialogs stay blocked.
<button
>
<img
src="icon.png"
alt="Icon"
style="width:16px; height:16px;"
/>
Save
</button
>
<button
>
<i class="fas fa-download"></i> Download
</button
>
Form Submit Button
Using button to submit a form:
Example
<form action="/submit" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" />
<button type="submit">Submit</button>
</form>
Button with JavaScript
Triggering JavaScript on click:
Example
The snippet includes JavaScript inside its HTML. Unavailable or blocked: browser dialog “alert()”. Provide inline styles and scripts, embedded images or the missing HTML context. External requests and browser dialogs stay blocked.
<button onclick="alert('Hello World!')">Click Me</button>
<script>
function myFunction() {
alert('Button was clicked!');
}
</script>
<button onclick="myFunction()">Click for Alert</button>
Styled Buttons
Custom button styling with CSS:
Example
<style>
.btn-primary {
background-color: #4CAF50;
color: white;
padding: 12px 24px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
.btn-primary:hover {
background-color: #45a049;
}
.btn-danger {
background-color: #f44336;
color: white;
padding: 12px 24px;
border: none;
border-radius: 4px;
cursor: pointer;
}
</style>
<button class="btn-primary">Primary Button</button>
<button class="btn-danger">Danger Button</button>
Button with Loading State
Showing loading state:
Example
<button id="loadBtn">Load Data</button>
<script>
document.getElementById('loadBtn').addEventListener('click', function() {
this.disabled = true;
this.innerHTML = 'Loading...';
// Simulate loading
setTimeout(() => {
this.disabled = false;
this.innerHTML = 'Load Data';
}, 2000);
});
</script>
Button with Form Attributes
Using formaction and formmethod:
Example
<form action="/default" method="get">
<label for="email">Email:</label>
<input type="email" id="email" name="email" />
<button type="submit">Submit (GET to /default)</button>
<button type="submit" formaction="/special" formmethod="post">
Submit (POST to /special)
</button>
</form>
Try it Yourself
Interactive Example
Button Types Comparison
Understanding the different button types:
| Type | Behavior | Use Case |
|---|---|---|
| submit | Submits the form data to the server | Form submission buttons (default type if not specified) |
| reset | Resets all form controls to their initial values | Clear form button (rarely used in modern web design) |
| button | Does nothing by default; used with JavaScript | Custom functionality with JavaScript event handlers |
Accessibility
- Always use descriptive text: Button text should clearly indicate what will happen when clicked
-
ARIA labels: Use
aria-labelfor icon-only buttons to provide context for screen readers - Keyboard navigation: Buttons are natively keyboard accessible (Space or Enter to activate)
- Focus indicators: Ensure buttons have visible focus states for keyboard users
-
Disabled state: Use
disabledattribute, not CSS, to properly disable buttons -
ARIA attributes: Use
aria-pressedfor toggle buttons,aria-expandedfor dropdown buttons -
Loading states: Use
aria-busy="true"when button triggers async operations
Example - Accessible Buttons
<!-- Icon button with ARIA label -->
<button
aria-label="Close dialog"
>
<span aria-hidden="true">×</span
>
</button
>
<!-- Toggle button -->
<button
aria-pressed="false"
onclick="this.setAttribute('aria-pressed', 'true')"
>
Mute
</button
>
<!-- Button with loading state -->
<button
aria-busy="false"
id="submitBtn"
>
Submit
</button
>
Best Practices
-
Always specify type: Always set the
typeattribute to avoid unexpected form submission -
Use semantic HTML: Use
<button>for actions,<a>for navigation - Descriptive text: Use clear, action-oriented text (e.g., "Save Changes" instead of "OK")
- Visual feedback: Provide hover and active states using CSS
- Disable during processing: Disable submit buttons during form processing to prevent double-submission
- Consistent styling: Maintain consistent button styles across your application
- Size and touch targets: Ensure buttons are large enough for touch interaction (minimum 44x44px)
- Loading indicators: Show loading state for async operations
<button> vs <input type="button">
Comparing <button> with
<input type="button">:
| Feature | <button> | <input type="button"> |
|---|---|---|
| Content | Can contain HTML (images, icons, formatting) | Plain text only (via value attribute) |
| Styling | Easier to style with CSS | More limited styling options |
| Default Type | submit (if not specified) | button (does nothing by default) |
| Flexibility | More flexible and powerful | Simpler, more limited |
| Use Case | Preferred for modern web development | Legacy forms, simple buttons |
<button> for better flexibility and modern web
development. It's easier to style and can contain rich content.
CSS Styling
Common button styling patterns:
Example
<style>
/* Base button styles */
button {
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: all 0.3s ease;
}
/* Primary button */
.btn-primary {
background-color: #007bff;
color: white;
}
.btn-primary:hover {
background-color: #0056b3;
}
.btn-primary:active {
transform: scale(0.98);
}
/* Disabled state */
button:disabled {
opacity: 0.6;
cursor: not-allowed;
}
/* Focus state for accessibility */
button:focus {
outline: 2px solid #007bff;
outline-offset: 2px;
}
</style>