Skip to content

Instantly share code, notes, and snippets.

@aneudy1702
Last active March 30, 2024 02:35
Show Gist options
  • Save aneudy1702/1b7d367ed5da0650b6da00b83b95837d to your computer and use it in GitHub Desktop.
Save aneudy1702/1b7d367ed5da0650b6da00b83b95837d to your computer and use it in GitHub Desktop.
Step into the world of semantic HTML with this guide! Discover the key to unlocking accessible, interactive web elements without relying on <div>s. Perfect for engineers ready to tackle hands-on coding challenges and elevate their understanding of accessibility's role in modern web development.

Disclaimer: This write-up is the first part of a valuable two-part series. While it provides comprehensive resources and requirements to understand the impact of semantic HTML on accessibility, it does not include code solutions. Part 2, which is equally important, will delve into the coding aspect, offering detailed solutions. This version is tailored for workshop use, encouraging hands-on learning and problem-solving.


Don't <div /> Your <button />s

image: a humorous depiction of someone mistakenly using divs as buttons

Here's the 🏃 TL:DR 🏃 version for the speed readers:

🚫🚫 div[role="button"] 🚫🚫

The div element, as per its HTML Standard definition, is non-interactive and therefore inaccessible. This can have significant implications for the usability and inclusivity of your code. For interactive elements, it's crucial to opt for an interactive HTML element instead.

// 😱😱😱 - Avoid at all costs
<div
    onClick={handleClick}
    className="btn"
    role="button"
    tabIndex="0"
/>

💫💫 Let the <button /> fulfill its destiny 💫💫 or as MDN puts it,

The <button> HTML element is an interactive entity activated by various user inputs. Once activated, it performs actions like submitting forms or opening dialogs.

// 💅💅💅 - Fully accessible and semantic
<button onClick={handleClick}>Click me</button>

By all means, this is meant to serve as a guiding principle and thought exercise in hopes of preventing you from ending up like this engineer on Stack Overflow:

image: meme depicting the confusion of nesting clickable divs

We all have been there; we just need to make that div.card component clickable. This post is not intended to have you turn everything into buttons. By the end of this post, we hope you are better equipped to respond to and wrestle with all the accessibility challenges we face. More importantly, correct the habit that often places us in those challenging situations. Remember, you're not alone in this struggle.

Intro

Let's be honest—we've all been there. The div is often our go-to when in doubt about which HTML element to use. It's versatile, easy to style with CSS, and seems to do it all. But have you ever stopped to consider the cost of semantics and accessibility?

Indeed, one could muscle through accessibility requirements using divs. Many WAI-ARIA Roles examples on MDN apply to div elements. Even MDN's button role description mentions divs but cautions against their risks. For example:

<div id="saveChanges" tabindex="0" role="button" aria-pressed="false">Save</div>

Even the W3C outlines a non-semantic approach that's safer, yet engineers frequently opt for potentially risky practices, risking business-damaging accessibility consequences.

The <button> vs <div> debate is not just theoretical—it has real-world implications. 20% of internet users access it through assistive technology. 20% is a lot of people. If the TL;DR didn't convince you to use a button for button-like actions, perhaps you're here for the memes. Welcome!

image: meme highlighting the absurdity of overcomplicating HTML

What Are We Solving, Then?

Imagine a scenario where a Product Owner requests a list of clickable product cards, with about 20% of traffic coming from assistive device users. Each card should:

  • Be linked and clickable
  • Contain more than one link
  • Be semantically structured for assistive tech
  • Be selectable like regular links
  • Support right-click and keyboard shortcuts
  • Have focusable elements for tab navigation

The Code

image: illustrative meme of coding best practices

Retro

Reflecting on this journey from divs to buttons, it's clear that semantic HTML isn't just about following standards—it's about embracing accessibility, maintainability, and inclusivity. As we refactor our way through the codebase, let's remember the power of a well-placed <button> and the broader impact of our coding choices.


Sources:

  • W3Docs, CSS Tricks, Scott O'Hara's Blog, Gale Blog, Medium articles, and the W3C and HTML5 specifications provide a wealth of information on the importance of semantic HTML and alternatives to using div elements for interactive purposes.
@aneudy1702
Copy link
Author

This was me the morning I started writing this up

image

@aneudy1702
Copy link
Author

aneudy1702 commented Mar 29, 2024

Recommended Readings for Participants:

  • W3Docs on Making Clickable Divs: A foundational guide that transforms a <div> into a clickable link. This article sets the stage for understanding the practical aspects of manipulating <div> elements. Read more on W3Docs.

  • CSS-Tricks on Clickable Divs: Offers insights into creating clickable <div> elements. A subsequent piece from CSS-Tricks revisits this topic, advocating for a more semantic HTML approach while still leveraging JavaScript, showcasing the evolution of best practices. Explore Clickable Divs on CSS-Tricks and the journey towards semantic HTML.

  • Divisiveness by Scott O'Hara: Delve into the discussions surrounding the use of <div> elements and their impact on accessibility and semantic HTML. Scott O'Hara provides a thoughtful analysis of when and how to use <div>s appropriately. Read Scott O'Hara’s blog.

  • Why Semantic HTML Matters: This article from Gale Blog highlights the importance of semantic HTML in enhancing web accessibility. It offers compelling reasons to prioritize semantic elements in your web projects. Discover Why Semantic Matters.

  • Alternatives to the Div Tag: Zac Heisey explores seven alternatives to the <div> tag, encouraging developers to consider semantic elements that better describe content and support accessibility. Explore Alternatives on Medium.

  • The Div Element Definition: For a deeper understanding of the <div> element and its intended use in HTML5, refer to this detailed definition by the W3C. It’s crucial for grasping the core functions of <div> in web development. Learn more at W3C.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment