Ditch the Div-Button: Why Semantic Buttons Win (and When to Reach for ARIA)


Informative article

We’ve all seen it (or perhaps even written it): a div or span element, perhaps with a click handler, meticulously styled to look exactly like a button. While visually it might pass, beneath the surface lies an accessibility nightmare and a missed opportunity for robust, maintainable code.

In this article, we’re going to champion the humble <button> element. We’ll start by stripping away all its default browser styling, then carefully add back the visual cues that scream “I’m clickable!” – all while retaining its inherent semantic power. Finally, we’ll explore the rare but crucial scenarios where a div or span does need to become interactive, and how ARIA steps in to save the day.

The Problem with “Div-Buttons”

When you use a div or span and attach a JavaScript click event, you’re essentially creating a custom interactive control from scratch. This means you lose:

  • Keyboard Navigability: Users can’t Tab to it.
  • Screen Reader Annunciation: Screen readers won’t announce it as a “button,” nor will they inform the user of its state.
  • Built-in Accessibility Features: Things like the disabled attribute, form submission behavior, and default click handling are gone.
  • Browser Consistency: Each browser styles native buttons slightly differently, which, while sometimes annoying, provides a baseline of recognition for users.

Starting with a Blank Canvas: The Unstyled Button

Let’s begin by taking a standard <button> and mercilessly stripping away all its default browser styling. This allows us to appreciate its raw semantic value and build our visual design from the ground up, much like a div.

Here’s the CSS to achieve this:

button {
  /* Remove default background, border, and padding */
  background: none;
  border: none;
  padding: 0;

  /* Inherit font styles from parent, don't use system defaults */
  font: inherit;
  color: inherit; /* Ensure text color is inherited */
  
  /* Remove browser-specific "appearance" */
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;

  /* Ensure the text alignment is inherited, not centered by default */
  text-align: inherit;

  /* Keep the pointer cursor for interaction */
  cursor: pointer;

  /* IMPORTANT: While removing the outline helps styling,
     you MUST add custom focus styles for accessibility! */
  outline: none; 
}

Now, if you render a <button> with this CSS, it will look indistinguishable from a div containing text.

What you see visually are two pieces of text. But semantically, one is a robust, interactive control, and the other is just text. The magic of the <button> remains: it’s still keyboard focusable and announced correctly by screen readers.

Styling It Back: Adding Visual Cues to Semantic Power

Now that our button looks like a div, let’s add back the styling that makes it visually obvious it’s a button. This isn’t just for aesthetics; it’s a core accessibility requirement. We need to satisfy two major principles: users must be able to identify the component, and they must be able to operate it.

To “Look Like a Button” (Identification)

Simply having text on a page isn’t enough. A user needs to visually identify it as an interactive control. This relates directly to WCAG 1.4.11 Non-text Contrast (AA), which requires that the visual boundaries of user interface components (like our button) have a contrast ratio of at least 3:1 against their surroundings. By adding a background-color or a border, we are creating that essential boundary.

To “Act Like a Button” (Operation)

When we removed the default outline, we removed the browser’s built-in focus indicator. WCAG 2.4.7 Focus Visible (AA) requires that any keyboard-operable interface has a visible focus indicator. Therefore, it’s not optional; we must add our own clear and highly-visible focus style to show keyboard users where they are on the page.

With those requirements in mind, let’s add our styles back:

/* Our base unstyled button from before */
button {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: inherit;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  text-align: inherit;
  cursor: pointer;
  outline: none; /* Remember: this MUST be replaced */
}

/* Add our custom button styling */
.my-custom-button {
  background-color: #007bff; /* A nice blue background */
  color: white; /* White text for contrast */
  padding: 8px 16px; /* Some internal spacing */
  border-radius: 4px; /* Slightly rounded corners */
  transition: background-color 0.2s ease-in-out; /* Smooth hover effect */
  display: inline-block; /* Treat it like a block, but inline flow */

  /* This 'background-color' creates the boundary, 
     helping to meet WCAG 1.4.11 Non-text Contrast */
}

.my-custom-button:hover {
  background-color: #0056b3; /* Darker blue on hover */
}

/* This is our non-negotiable replacement for the default outline, 
   meeting WCAG 2.4.7 Focus Visible */
.my-custom-button:focus-visible {
  outline: 3px solid #0056b3; /* A clear, thick outline */
  outline-offset: 2px;
}

And now, apply it to our semantic button:

<button class="my-custom-button">Click Me Semantically!</button>

The result? You have a beautifully styled button that looks exactly how you want but now explicitly meets accessibility criteria for both identification (Non-text Contrast) and operation (Focus Visible), all while retaining the free benefits of a native HTML <button>.

The Benefits of Semantic Buttons

  • Accessibility Out-of-the-Box: This is the big one. Screen readers understand buttons. Keyboard users can interact with them. It significantly lowers the barrier for inclusive design.
  • Robustness: Buttons have native states (:hover, :active, :focus, :disabled) that are easy to style and understand.
  • Less JavaScript: You don’t need complex JavaScript to manage focus, keyboard events (like Enter or Spacebar for activation), or the disabled state. The browser handles it.
  • Developer Experience: It’s clearer, more concise, and easier to maintain. When you see <button>, you immediately know its purpose.
  • Future-Proofing: Browsers and assistive technologies evolve. Relying on native elements means your components are more likely to work correctly as new standards emerge.

When a Div or Span Does Need ARIA

While the <button> element should be your default for anything that performs an action, there are legitimate scenarios where you might use a div or span for interactivity. This usually happens when:

  • Custom Widgets: You’re building a highly custom UI component that doesn’t map directly to a native HTML element (e.g., a complex drag-and-drop interface, a tab panel, a custom dropdown with advanced features, a rich text editor).
  • Non-Actionable “Clicks”: Sometimes a click isn’t an “action” in the traditional sense, but rather a way to reveal more content or navigate in a non-standard way within an existing content block.

In these situations, you lose the browser’s built-in semantics, and it becomes your responsibility to reintroduce them using WAI-ARIA (Web Accessibility Initiative - Accessible Rich Internet Applications) attributes.

For example, if you have a div that functions like a button (but for some very specific design reason cannot be a <button>):

<div role="button" tabindex="0" aria-label="Add new item">
  +
</div>

Here’s what each ARIA attribute does:

  • role="button": Tells assistive technologies that this div should be treated as a button.
  • tabindex="0": Makes the div focusable via keyboard (Tab key).
  • aria-label="Add new item": Provides an accessible name for the button, especially important if its visual content (+ in this case) isn’t descriptive enough.

You would also need to manually add JavaScript to handle both click events and keydown events for Enter and Spacebar to fully emulate a native button’s behavior. As you can see, it’s significantly more work to make a div behave like a button than to just use a <button> in the first place!

Conclusion

For 99% of interactive elements that trigger an action, the <button> element is your best friend. Embrace its semantic power, style it to your heart’s content, and provide an accessible experience by default. Reserve div and span with ARIA for the truly custom, complex widgets where native HTML simply doesn’t offer a suitable alternative. Your users, your fellow developers, and your future self will thank you.