Live Preview
A styled clickable button:

By the end of this tutorial, you’ll create accessible, styled buttons for actions and forms.
Write valid <button> elements with explicit type attributes.
Master button, submit, and reset behaviors.
Handle clicks with addEventListener for interactive actions.
Submit and reset forms with the correct button types.
Style buttons with classes for consistent, polished UI.
Apply accessibility and semantic HTML best practices.
<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">.
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.
Wrap button label text between opening and closing <button> tags. Always specify type:
<!-- Basic button syntax -->
<button type="button">Click Me</button>type explicitly—avoid accidental form submission.aria-label for icon-only buttons).disabled to prevent interaction when an action is unavailable.<button
type="button"
class="btn-primary"
id="save-btn"
>
Save Changes
</button>| Use Case | Code Snippet | Result |
|---|---|---|
| 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-only | aria-label="Close" | Accessible label required |
Key <button> attributes for beginners:
type Essentialbutton (generic click), submit (send form), or reset (clear form). Always set explicitly.
type="button"name / value FormsSent with form data on submit. Useful when multiple submit buttons exist in one form.
name="action" value="save"disabled StatePrevents clicks and removes from keyboard focus. Use when action is temporarily unavailable.
<button disabled>class / id GlobalHook for CSS styling and JavaScript event listeners.
class="btn-primary" id="save-btn"form OptionalAssociate button with a form by id, even when placed outside the form element.
form="my-form"aria-label A11yRequired for icon-only buttons with no visible text label.
aria-label="Close dialog"Prefer addEventListener over inline onclick in production code for maintainable JavaScript.
Real-world button patterns with copy-ready code, live previews, and hands-on practice.
A styled clickable button:
A simple button with type="button" so it does not accidentally submit a form.
<button type="button">Click Me</button>Buttons trigger actions, submit forms, open modals, toggle menus, and run JavaScript—anywhere users need to perform an on-page action.
Run code when the button is clicked using addEventListener.
<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>Inside a <form>, use type="submit" to send data to the server.
<form action="/submit" method="post">
<input type="text" name="username" placeholder="Username">
<button type="submit">Submit</button>
</form>Add the disabled attribute to prevent clicks until the action is available.
<button type="button" disabled>Not Available</button>
<button type="button">Continue</button>Apply a CSS class for consistent button styling across your site.
<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>Create consistent, accessible button styles with CSS classes:
default Browser styling:hover Hover feedback:focus-visible Keyboard focus:disabled Inactive state/* 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
Make buttons usable for everyone:
button for proper semantics and keyboard behavior.disabled attribute, not just gray CSS styling.aria-label when there is no visible text.Set type and visible label text inside the element.
Default or custom CSS styles the button. It receives keyboard focus.
Click or keyboard triggers submit, reset, or a JavaScript event handler.
Form submits, fields reset, or custom JavaScript runs based on type and event listeners.
The <button> element is supported in all modern browsers and Internet Explorer.
From legacy Internet Explorer to the latest Chromium builds — the button element is one of the most universally supported interactive HTML controls.
Bottom line: Ship interactive controls with confidence. The <button> element is safe to use in every production environment today.
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.
type="button" outside formsbutton over div for clicksbutton for page navigation (use a)type unset inside forms (defaults to submit)<button>Bookmark these before you ship — they’ll make every interactive control accessible and reliable.
<button> creates interactive controls for actions and form submission.
Always specify type—defaults to submit inside forms.
button, submit, and reset control behavior.
Buttons can contain icons, spans, and other HTML inside.
AdvantageUse disabled to block interaction when action is unavailable.
Supported in every browser including IE. No polyfills required.
Compatibilitybutton 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.addEventListener in production for cleaner, maintainable JavaScript. Inline onclick is fine for small learning examples.button inside a form defaults to type="submit". Without setting type="button", clicking it will submit the form unexpectedly.Practice basic, JavaScript, and form submit buttons in the Try It editor.
6 people found this page helpful