Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › HTML Snippets › Reference › <button>

HTML Reference

<button> — The Button Element

The accessible, content-rich alternative to <input type="button">. The right way to make anything clickable.

Last updated: October 2026

Definition: The <button> element creates a clickable button. Unlike <input>, it can contain HTML content — text, icons, and inline elements. Every clickable action that isn't navigation should be a button.

🎬 Live Example

Three button types — submit, reset, and button. Try them inside a form.

💡 Only "Submit" submits the form. "Reset" clears the field. "Custom" runs JavaScript without touching the form.

📝

Basic Syntax

<button type="button">Click me</button>

The default type is submit — always set type explicitly to avoid accidental form submission.

⚙️ Attributes Reference

Attribute Value Purpose
typesubmit / reset / buttonThe button's behavior. Default is submit.
disabledbooleanPrevents clicks and focus.
nametextSent as the key when the button submits a form.
valuetextSent as the value when the button submits a form.
formform idAssociate the button with a form elsewhere on the page.
formactionURLOverride the form's action for this button.
formmethodget / postOverride the form's method for this button.
formenctypeencodingOverride the form's enctype for this button.
formnovalidatebooleanSkip validation for this button's submission.
autofocusbooleanFocus on page load.
1

The Three Button Types

<!-- Submit: sends the form (default type) -->
<button type="submit">Save</button>

<!-- Reset: clears form fields to initial values -->
<button type="reset">Clear Form</button>

<!-- Button: does nothing without JavaScript -->
<button type="button" onclick="doSomething()">Custom Action</button>

Always set type="button" for buttons that aren't meant to submit. Otherwise, clicking the button inside a form will submit it — a bug that's easy to miss.

2

Button with Icon and Text

Unlike <input>, buttons can contain any inline HTML.

<button type="button">
    <svg width="16" height="16" viewBox="0 0 24 24">...</svg>
    Download PDF
</button>

<button type="button">
    <img src="/icons/trash.svg" alt="">
    Delete Item
</button>

<button type="button">
    ⭐ Add to Favorites
</button>

Use a plain emoji or SVG for the icon. Add an aria-label if the button has no visible text.

3

Disabled Button

<button type="submit" disabled>
    Submitting...
</button>

A disabled button:

  • Cannot be clicked.
  • Cannot receive keyboard focus.
  • Is not submitted with the form.
  • Is visually dimmed by default.

💡 Accessibility tip: Disabled buttons aren't focusable, so screen reader users can't find them to understand why they're disabled. Consider using aria-disabled="true" with JavaScript instead, so users can still focus and read the button.

4

Multiple Submit Buttons with Different Actions

Use formaction to override the form's action for one button.

<form action="/save-draft" method="POST">
    <input type="text" name="title">
    <textarea name="body"></textarea>

    <!-- Save as draft — uses the form's action -->
    <button type="submit">Save Draft</button>

    <!-- Publish — overrides the action -->
    <button type="submit" formaction="/publish">Publish</button>

    <!-- Preview — different action, no validation -->
    <button type="submit" formaction="/preview" formnovalidate>
        Preview
    </button>
</form>
5

button vs a vs input type=button

Use When Example
<a> Navigation — going to another page "Read more", "View profile"
<button> Action — doing something on this page "Submit", "Save", "Delete", "Open modal"
<input type="button"> Legacy — avoid for new code Older forms

💡 Golden rule: If it changes the page, it's a link (<a>). If it does something, it's a button (<button>).

🎯 Button Type Cheatsheet

Type Action Common Use
submitSubmits the formLogin, signup, contact forms
resetClears form fieldsLong forms with "Clear" option
buttonNo default actionCustom actions with JavaScript

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the HTML <button> tag?

The <button> element creates a clickable button. Unlike <input type=button>, it can contain HTML content — text, images, icons, and other inline elements. It is the accessible choice for any clickable action.

What is the difference between button types?

type=submit (default) submits a form. type=reset resets form fields to their initial values. type=button does nothing — used with JavaScript for custom actions.

Why does my button submit the form unexpectedly?

The default type is submit. If you add a <button> inside a form without setting type=button, it will submit the form when clicked. Always set type explicitly.

Should I use <button> or <a> for links?

Use <a> for navigation (going to another page or URL). Use <button> for actions (submit, delete, open modal, toggle). The distinction matters for accessibility, SEO, and browser behavior.

Can I put a div inside a button?

No. Buttons can only contain inline content — text, <span>, <img>, <strong>, <em>. Block elements like <div> or <p> are invalid inside a button. Use a <span> with display: block via CSS if you need block behavior.

🎯 What's Next?

← All HTML Reference