HTML Buttons

HTML Buttons

Master all button types in HTML - from submit and reset buttons to custom interactive buttons. Learn proper usage, accessibility, styling techniques, and advanced button patterns.

How do you create a button in HTML?

Use <button>. It's focusable, works with Enter and Space, and screen readers announce it as a button. Set type explicitly: submit sends a form (the default inside a form), button does nothing until you add JavaScript, and reset clears the form. Use <a> instead when the action is navigation to another page.

<button type="button" onclick="openMenu()">Open menu</button>
<button type="submit">Save changes</button>
<button type="button" aria-label="Close"><svg aria-hidden="true">…</svg></button>
<button disabled>Processing…</button>

Button Types Overview

HTML provides several ways to create buttons, each with specific purposes and behaviors. Understanding the differences between button types is crucial for creating accessible and functional web forms.

Button Element Types

  • submit - Submits form data
  • reset - Resets form to defaults
  • button - Generic clickable button

Input Button Types

  • input submit - Submit with value attribute
  • input reset - Reset with value attribute
  • input button - Generic input button
  • input image - Image-based submit button

Button vs Input Elements

While both <button> and <input> can create clickable buttons, they have important differences in functionality, styling options, and use cases.

<button> Element

Advantages:

  • • Can contain HTML content (images, icons, etc.)
  • • More flexible styling options
  • • Better semantic meaning
  • • Default type is "submit" inside forms
  • • Easier to style with CSS

Considerations:

  • • Slightly more complex HTML structure
  • • Need to explicitly set type attribute

📝 <input> Buttons

Advantages:

  • • Simple, self-closing tag
  • • Consistent with other input elements
  • • Type defines behavior clearly
  • • Image input for graphical buttons

Limitations:

  • • Can only contain text (via value attribute)
  • • Limited styling options
  • • Cannot contain child elements
  • • Less semantic than button element

When to Use Which?

Use <button> for:

  • • Buttons with icons or complex content
  • • Custom-styled interactive buttons
  • • Modern web applications
  • • Better accessibility requirements

Use <input> for:

  • • Simple text-only buttons
  • • Image-based submit buttons
  • • Legacy compatibility
  • • Minimal styling requirements

Complete Button Reference

Submit Buttons

Submit buttons trigger form submission when clicked. They're the primary action button in forms.

Button Element

<button type="submit">Submit</button>

Default type inside forms. Can contain HTML.

Input Element

<input type="submit" value="Submit">

Text from value attribute. Self-closing.

Submit Button Features:

  • • Triggers form submission to action URL
  • • Can be disabled to prevent submission
  • • Can override form attributes (formaction, formmethod, etc.)
  • • Receives focus when Enter is pressed in form fields

🔄 Reset Buttons

Reset buttons restore form fields to their initial values. Use with caution as they can be destructive.

Reset Behavior

  • • Clears user input
  • • Restores default values
  • • Unchecks checkboxes/radios
  • • Resets select options

⚠️ Best Practices

  • • Consider if really needed
  • • Place away from submit
  • • Add confirmation dialog
  • • Use clear labeling

🔘 Generic Buttons

Generic buttons (type="button") have no default behavior and are perfect for custom JavaScript functionality.

Common Use Cases:

  • • Toggle visibility of content
  • • Open/close modal dialogs
  • • Trigger AJAX requests
  • • Custom form validation
  • • Navigation between steps
  • • Dynamic content loading
  • • Interactive UI elements
  • • Custom animations/effects

🖼️ Image Buttons

Image buttons (input type="image") submit forms and send click coordinates along with form data.

Special Features:

  • • Click coordinates: Sends x,y position where user clicked
  • • Required attributes: src, alt (for accessibility)
  • • Form submission: Acts like submit button
  • • Styling: CSS can modify appearance
Data sent:button.x=123&button.y=45 (where button is the name attribute)

Interactive Button Demo

Explore all button types with interactive examples, custom styling, and JavaScript functionality. See how different button types behave in various scenarios.

Code Editor

<form onsubmit="event.preventDefault(); log('form submitted')">
  <input name="q" placeholder="Search">
  <button type="submit">Submit (default)</button>
  <button type="button" onclick="log('button clicked, form not submitted')">type=button</button>
  <button type="reset">Reset</button>
  <button type="button" disabled>Disabled</button>
</form>
<p id="log"></p>

Preview

Advanced Button Features

🔗 Form Association

Buttons can be associated with forms outside their container using the form attribute.

<button form="myform">External Submit</button>

⚙️ Form Override Attributes

Submit buttons can override form attributes:

  • • formaction - Override action URL
  • • formmethod - Override HTTP method
  • • formenctype - Override encoding
  • • formtarget - Override target
  • • formnovalidate - Skip validation

♿ Accessibility Features

  • • aria-label: Descriptive labels
  • • aria-pressed: Toggle button state
  • • aria-expanded: Dropdown/collapsible states
  • • disabled: Prevent interaction
  • • tabindex: Control tab order
  • • role: Override button semantics

Custom Data Attributes

Use data attributes for JavaScript functionality:

<button data-action="delete" data-id="123">Delete</button>

Button Best Practices

Design & UX Best Practices

  • • Make buttons look clickable
  • • Use consistent button styling
  • • Provide visual feedback on interaction
  • • Use clear, action-oriented labels
  • • Ensure sufficient touch target size (44px+)
  • • Group related buttons together

♿ Accessibility Best Practices

  • • Always provide descriptive labels
  • • Ensure keyboard navigation works
  • • Use proper focus indicators
  • • Test with screen readers
  • • Provide alternative text for image buttons
  • • Use ARIA attributes appropriately

Common Mistakes

  • • Using divs or spans as buttons
  • • Forgetting disabled states
  • • Poor button placement
  • • Unclear button labels
  • • Missing form associations
  • • Inconsistent styling patterns

Performance Tips

  • • Avoid inline JavaScript in HTML
  • • Use event delegation for many buttons
  • • Optimize image buttons for web
  • • Implement loading states
  • • Debounce rapid button clicks
  • • Use CSS transitions for smooth effects

Button Mastery Complete!

Outstanding! You now understand all HTML button types and their advanced features. Ready to dive into form validation techniques?

HTML Buttons — step by step

1 / 4
1

Use the button element for interactive actions

The button element is the correct choice for clickable actions that trigger JavaScript or form submission. Avoid using div or span with click handlers — buttons are keyboard-accessible and convey semantics to assistive technologies.

1 of 4
💡type='button' Prevents Accidental Form Submission
Any button inside a form element without an explicit type defaults to type='submit'. If you add a button for a non-submit action — like clearing a field or toggling a panel — always set type='button' explicitly. Otherwise clicking it will unexpectedly submit the form.

🎯 Quick Check

What is the default type of a button element placed inside a form?

HTML Buttons: summary

Key points

  • A <button> inside a form without type="button" submits the form; this is a common source of bugs.
  • Icon-only buttons need an aria-label.
  • Don't remove the focus outline without providing a visible replacement.

Frequently asked questions

›Should I use a button or a link?

Use a link (<a href>) when it goes to another URL. Use a button when it performs an action on the current page, such as opening a menu, submitting or toggling.

›Why does my button reload the page?

It's inside a <form> and defaults to type="submit". Add type="button".