Standard Button

Learn the difference between a real button and a fake one. The <button> element is the foundation of accessible interactivity.

beginner
2.1.1 Keyboard (Level A)4.1.2 Name, Role, Value (Level A)
❌

The Bad (Inaccessible)

<div class="btn" onclick="submitForm()">
  Submit
</div>
βœ…

Accessibility-Ready Code

<button type="submit" class="btn-primary">
  Submit
</button>

The Standard

The <button> element is the single most important interactive element in HTML. Using it correctly grants you free keyboard support, focus management, and screen reader clarity.

WCAG Criteria

  • 2.1.1 Keyboard (Level A): All functionality must be operable via keyboard.
  • 4.1.2 Name, Role, Value (Level A): The element’s name, role, and state must be programmatically determinable.

❌ The Bad (Inaccessible)

What’s Wrong?

  1. No Keyboard Access: You cannot Tab to a <div>.
  2. No Role Announced: A screen reader just says β€œSubmit” with no context. Is it a link? A heading?
  3. No Enter/Space Support: Pressing Enter does nothing.

βœ… The Good (Accessibility-Ready Code)

Why This Works

  1. Native Keyboard: The browser automatically handles Tab, Enter, and Space.
  2. Clear Role: Screen readers announce: β€œSubmit, button”.
  3. Focus Ring: The browser provides a default :focus outline automatically.

Accessibility Checklist

  • Use a <button> element for all click actions.
  • Include a visible text label OR an aria-label for icon-only buttons.
  • Ensure a clear :focus-visible style is always present.
  • Do NOT use <div> or <span> with onclick for buttons.
πŸ“ Perfect Implementation Reference
<!-- Native button -->
<button type="button" class="btn">
  Save Changes
</button>

Technical Deep Dive

The Accessibility Tree

Role: button
Name: "Submit"
State: focusable

Focus Flow

  1. Tab Key: Captures focusin. Highlighted via :focus-visible.
  2. Enter / Space: Triggers click event natively.
πŸ§ͺ

Interactive Behavioral Lab

πŸ’»

Interactive Sandbox

LIVE PREVIEW

πŸ”¬ Technical Internals

Understand how this component is processed by the browser and Assistive Technology (AT). This section bridges the gap between visual code and the hidden logic that powers accessibility.

🌲 Accessibility Tree

The data structure used by screen readers to "see" your page. It translates HTML roles and attributes into standardized objects.

βš™οΈ Event Logic

Expected behavioral standards for keyboard navigation and state transitions. Crucial for users who don't use a mouse.

  • Focus: Highlights via :focus-visible
  • Activation: Responds to Enter and Space
  • Role: Identified as Button Pattern

🌐 Browser & Screen Reader Compatibility

Browser
Screen Reader
Status
🍎 Safari
VoiceOver
βœ“ Safe
πŸͺŸ Chrome
NVDA
βœ“ Safe
πŸͺŸ Edge
JAWS
βœ“ Safe
🦊 Firefox
NVDA
βœ“ Safe
πŸ“± iOS Safari
VoiceOver
βœ“ Safe
πŸ€– Chrome Android
TalkBack
βœ“ Safe