HTML <button> Tag

Beginner
⏱️ 7 min read
📚 Updated: Jun 2026
🎯 5 Examples
Forms & Controls

What You’ll Learn

By the end of this tutorial, you’ll create accessible, styled buttons for actions and forms.

01

Core Syntax

Write valid <button> elements with explicit type attributes.

02

type Attribute

Master button, submit, and reset behaviors.

03

JavaScript Events

Handle clicks with addEventListener for interactive actions.

04

Form Integration

Submit and reset forms with the correct button types.

05

CSS Styling

Style buttons with classes for consistent, polished UI.

06

Production Tips

Apply accessibility and semantic HTML best practices.

What Is the <button> Tag?

The button element (<button>) is an interactive control that users can click, tap, or activate with the keyboard. It can contain text, images, or other HTML inside it—unlike <input type="button">.

💡
Actions, not navigation

Use <button> for on-page actions (submit, toggle, open modal). Use <a> for navigation to other pages or URLs.

Inside a <form>, a button without an explicit type defaults to submit. Always set type="button" for generic click handlers outside form submission.

📝 Syntax

Wrap button label text between opening and closing <button> tags. Always specify type:

syntax.html
<!-- Basic button syntax -->
<button type="button">Click Me</button>

Syntax Rules

  • Always set type explicitly—avoid accidental form submission.
  • Put descriptive label text inside the button (or use aria-label for icon-only buttons).
  • Buttons can contain rich HTML: icons, spans, and multiple lines of text.
  • Use disabled to prevent interaction when an action is unavailable.
full-example.html
<button
  type="button"
  class="btn-primary"
  id="save-btn"
>
  Save Changes
</button>

⚡ Quick Reference

Use CaseCode SnippetResult
Generic click<button type="button">Click</button>
Form submit<button type="submit">Submit</button>
Form reset<button type="reset">Reset</button>Clears form fields
Disabled<button disabled>...</button>
With icon/text<button><span>Save</span></button>Rich HTML inside
Icon-onlyaria-label="Close"Accessible label required

⚖️ <button> vs <input>

Both can submit forms, but they differ in content and semantics:

Feature<button><input type="submit">
ContentRich HTML (icons, spans)Plain text via value only
Element typeOpening + closing tagsVoid (self-closing)
SemanticsPreferred for interactive controlsLegacy/simple submit labels
NavigationNot for links—use <a>Not for links—use <a>

🧰 Attributes

Key <button> attributes for beginners:

type Essential

button (generic click), submit (send form), or reset (clear form). Always set explicitly.

type="button"
name / value Forms

Sent with form data on submit. Useful when multiple submit buttons exist in one form.

name="action" value="save"
disabled State

Prevents clicks and removes from keyboard focus. Use when action is temporarily unavailable.

<button disabled>
class / id Global

Hook for CSS styling and JavaScript event listeners.

class="btn-primary" id="save-btn"
form Optional

Associate button with a form by id, even when placed outside the form element.

form="my-form"
aria-label A11y

Required for icon-only buttons with no visible text label.

aria-label="Close dialog"

Prefer addEventListener over inline onclick in production code for maintainable JavaScript.

Examples Gallery

Real-world button patterns with copy-ready code, live previews, and hands-on practice.

Live Preview

A styled clickable button:

Basic Button

A simple button with type="button" so it does not accidentally submit a form.

basic-button.html
<button type="button">Click Me</button>
Try It Yourself

📚 Common Use Cases

Buttons trigger actions, submit forms, open modals, toggle menus, and run JavaScript—anywhere users need to perform an on-page action.

JavaScript Click Handler

Run code when the button is clicked using addEventListener.

javascript-button.html
<button type="button" id="greet-btn">Say Hello</button>
<p id="message"></p>

<script>
  document.getElementById("greet-btn")
    .addEventListener("click", function () {
      document.getElementById("message").textContent = "Hello!";
    });
</script>
Try It Yourself

Form Submit Button

Inside a <form>, use type="submit" to send data to the server.

form-submit.html
<form action="/submit" method="post">
  <input type="text" name="username" placeholder="Username">
  <button type="submit">Submit</button>
</form>
Try It Yourself

Disabled Button

Add the disabled attribute to prevent clicks until the action is available.

disabled-button.html
<button type="button" disabled>Not Available</button>
<button type="button">Continue</button>
Try It Yourself

Styled Button with CSS

Apply a CSS class for consistent button styling across your site.

styled-button.html
<style>
  .btn-primary {
    background: #16a34a;
    color: #fff;
    padding: 0.6rem 1.25rem;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
  }
</style>
<button type="button" class="btn-primary">Get Started</button>
Try It Yourself

Styling <button> with CSS

Create consistent, accessible button styles with CSS classes:

default Browser styling
:hover Hover feedback
:focus-visible Keyboard focus
:disabled Inactive state
button-styles.css
/* Primary button */
.btn-primary {
  background: #2563eb;
  color: #fff;
  padding: 0.6rem 1.25rem;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary:hover {
  background: #1d4ed8;
}

.btn-primary:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
}

Live styled primary button

♿ Accessibility

Make buttons usable for everyone:

  • Clear labels — use descriptive text like “Save changes”, not just “Click here”.
  • Keyboard support — buttons are focusable and activatable with Enter/Space by default.
  • Don’t use div for buttons — use button for proper semantics and keyboard behavior.
  • Disabled state — use disabled attribute, not just gray CSS styling.
  • Icon-only buttons — add aria-label when there is no visible text.

🧠 How <button> Works

1

Author adds button markup

Set type and visible label text inside the element.

Markup
2

Browser renders clickable control

Default or custom CSS styles the button. It receives keyboard focus.

Display
3

User activates the button

Click or keyboard triggers submit, reset, or a JavaScript event handler.

Interaction
=

Action performed

Form submits, fields reset, or custom JavaScript runs based on type and event listeners.

Universal Browser Support

The <button> element is supported in all modern browsers and Internet Explorer.

Baseline · Since HTML 2

Works everywhere your users are

From legacy Internet Explorer to the latest Chromium builds — the button element is one of the most universally supported interactive HTML controls.

100% Core tag support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
<button> tag 100% supported

Bottom line: Ship interactive controls with confidence. The <button> element is safe to use in every production environment today.

Conclusion

The <button> tag is essential for interactive web pages. Always set type explicitly, use semantic labels, and prefer addEventListener for JavaScript.

Style with CSS classes for a polished, accessible user experience—and use <a> for navigation, not buttons.

💡 Best Practices

✅ Do

  • Always set type="button" outside forms
  • Use clear, action-oriented button labels
  • Prefer button over div for clicks
  • Style buttons with CSS classes consistently

❌ Don’t

  • Use button for page navigation (use a)
  • Leave type unset inside forms (defaults to submit)
  • Rely only on color to convey button purpose
  • Use vague labels like “Click here”

Key Takeaways

Knowledge Unlocked

Six truths every developer should know about <button>

Bookmark these before you ship — they’ll make every interactive control accessible and reliable.

6
Core concepts
02

Set type Explicitly

Always specify type—defaults to submit inside forms.

Essential
📝 03

Three type Values

button, submit, and reset control behavior.

Reference
🛠 04

Rich HTML Content

Buttons can contain icons, spans, and other HTML inside.

Advantage
🚫 05

disabled Attribute

Use disabled to block interaction when action is unavailable.

State
🌐 06

Universal Support

Supported in every browser including IE. No polyfills required.

Compatibility

❓ Frequently Asked Questions

It creates a clickable control for actions, form submission, or resetting form fields.
button for generic clicks, submit to send form data, and reset to clear form fields.
button can contain HTML like icons. input buttons only show plain text from the value attribute.
Use addEventListener in production for cleaner, maintainable JavaScript. Inline onclick is fine for small learning examples.
A button inside a form defaults to type="submit". Without setting type="button", clicking it will submit the form unexpectedly.

Build Interactive Buttons

Practice basic, JavaScript, and form submit buttons in the Try It editor.

Try button tag →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful