Skip to content

Instantly share code, notes, and snippets.

@nikku
Last active August 31, 2021 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikku/8bc426f82074562f76e97d9b0a522e45 to your computer and use it in GitHub Desktop.
Save nikku/8bc426f82074562f76e97d9b0a522e45 to your computer and use it in GitHub Desktop.
Use semantic HTML elements - A short pitch

Use Semantic HTML Elements

TLDR

Understand the semantic elements that HTML offers and use them in favor of divs

Clickable <div />? Try not to do it, if it clearly is a UI control.

Consider: Not every element that is clickable must be a UI control but every click action needs to be accessible from a UI control.

The Case of Buttons

<!-- NO UI CONTROL -->
<div onClick={ (event) => alert('foo') }>
</div>
<!-- actual UI controls -->
<button onClick={ (event) => alert('foo') }>
</button>

<a></a>

<input type="button" />

What do we gain from actual buttons

  • Browser is aware
  • Keyboard navigation / trigger / outlines out of the box
  • out of the box disabled states
  • improved screen reader support (already without a11y hints)

We could add all of this to <div> using custom styling and aria hints, or we simply use semantic HTML elements that offer it out of the box.-

There is much more semantic HTML elements

<section>
<li>
<code>
<header>
<nav>
<p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment