Bad code Bad code
<div tabindex="-1">
<div role="button">
<svg width="28" height="24"> … </svg>
</div>
</div>
Issues and how to fix them Issues and how to fix them
- Setting button semantics explicitly using the
role
attribute isn’t necessary, there’s an element for that (button
). - You don’t need the
tabindex
attribute if you use abutton
. HTML buttons are focusable by default. - A click event on a
div
triggers only on click. A click event on abutton
triggers on click and if the user presses the Enter or Space key. - There’s no text alternative for the icon.
Good code Good code
Unfortunately, there’s no native way of hiding content only visually.
The .sr-only
class makes sure that content is visually hidden but still accessible to screen reader users.
.sr-only {
position: absolute;
white-space: nowrap;
width: 1px;
height: 1px;
overflow: hidden;
border: 0;
padding: 0;
clip: rect(0 0 0 0);
clip-path: inset(50%);
margin: -1px;
}
<button>
<span class="sr-only">Send</span>
<svg width="28" height="24" aria-hidden="true"> … </svg>
</button>