Skip to main content

Explore Web Accessibility (a11y)

Back to navigation

Use semantic button tags instead of divs for interactivity

Many times, when we have some requirement where we need to show beautiful buttons, we end up using div tags instead of button. And this could lead to highly inaccessible code if we do not think about assistive tech users.

HTML 5 has a semantic element called button which should be used for any click through action.

Alternatively, if we really need to use any non-semantic tag instead of button tag, we need to provide extra attributes and event handlers to make to accesible to assistive tech users.

Jump to code tips to make accessible buttons

Demo:

Inaccessible button 'Login'

Login to buy

Accessible button 2

Login to buy


How to create accessible buttons with divs (or any other non-semantic HTML)?

Please note that always try to use button tags to create buttons. But, for already existing websites and applications, if you have used div or span to create buttons, and if you find it expensive to use a button tag to fix the accessibility for those elements, you can use the steps as described in below list to make those buttons accessible.
  • tab-index: to make the button reachable by keyboard through tab key
  • keydown event listener: to enable triggering the button action from keyboard
  • role: to enable acreen readers understand that it is a button
  • aria-attributes if needed
    For example: If the button contains only icon or image without any text, we need to add aria-label so that screen readers and other assistive technologies can understand what exactly does that button do. Check demo for icon only button.

Sample code for buttons with label:

Demo for icon only buttons:

Toggle button for enabling dark/light mode for webpage or application

Sample code using aria attributes: