Don't use <div>
tags to make clickable elements. Use <a>
or <button>
elements. This enables browsers with JavaScript disabled to interact with them as expected, in addition to a host of other benefits listed below. Even if your functionality requires JavaScript and there is no reasonable default behaviour you can assign to an <a>
or <button>
, use it anyways - it conveys "clickable" semantics.
In general, choose the tag that most closely describes the function of its content, not the appearance of its content, and avoid unnecessary <div>
tags lest your documents suffer from divitis.
Because "it requires JavaScript on the client" is usually rejected out of hand as a reason for doing anything, in 2023, here are a few examples of behavior you're breaking by choosing <div>
over a <button>
/<a>
tag. Some of this functionality can be restored with specific additional work, but that is often overlooked, and again, this is extra work to achieve feature-parity with what you get by default when using the correct <button>
or <a>
element:
- Your
<div>
cannot be navigated to with the keyboard or by many accessibility devices, making your site fundamentally inaccessible to people who cannot easily use a mouse, or people who simply prefer a keyboard
- Your
<div>
is not surfaced as an interactive element by screen-reader software, again, making it inaccessible by people with vision impairments
- Your
<div>
doesn't show where it will take the user in the status bar of the browser, <a>
does
- Your users cannot middle-click or ctrl-click your
<div>
to open the link in a new tab, or a new window, the way they can with <a>
- Search engines cannot (easily) follow your
<div>
from page to page to index your content
- It's generally a given that you will apply a significant amount of styling to make your
<div>
look like a button or anchor tag, but again: Extra work to achieve feature-parity.
In short, it unambiguously good to use <a>
or <button>
over <div>
: It costs you nothing, with no downside, and is important for a non-zero number of your users. Nobody has ever said "Oh good, this element is a div, not a button", however, lots of people say "Oh no, this element is a div when it should have been an anchor tag, now I can't use this site".