Reputation: 103
I'm trying to fix accessibility on my site and to manage the navigation with tab button, I'm using tabindex. I noticed that elements with tabindex have an outline, both on tab focus and on click. I would remove outlines on click (or drag) and leave it on tab focus.
Is there a solution (maybe in css) to resolve this problem?
Upvotes: 4
Views: 7564
Reputation: 5891
At the moment there is no easy way to achieve this without JavaScript detecting differences between focus on keyboard and mouse. However, there is a new CSS pusedo-class called :focus-visible (previously called :focus-ring
) that solves this problem. It allows you to style the focus indicator just for keyboard users while not having it apply to mouse users.
Currently it's not supported in any browser without turning on a feature flag, but there is a polyfill available that would allow you to use it.
Additional resource: A11ycast focus-ring
Upvotes: 5
Reputation: 18807
When you click on an element using your mouse, you will move both the pointing device focus and the keyboard focus.
The WCAG requires that the keyboard focus indicator be always visible on any keyboard operable element (no matter how it has been moved). By setting the tabindex
, you made it keyboard operable, so you must obey this rule, even if the element has been previously activated with the mouse, leading the keyboard focus to be moved there.
Also, many assistive technologies can focus an element without using the tab key (for instance, voice recoginition software, eye tracking devices, some screen readers).
Upvotes: 0
Reputation: 16251
this code will remove outline
only when you click on button by mouse
use mousedown
to remove outline
and mouseup
to back it
<button onmousedown="this.style.outlineColor='transparent'" onmouseup="this.style.outlineColor='blue'" tabindex="1">try me</button><br>
<br>
<button onmousedown="this.style.outlineColor='transparent'" onmouseup="this.style.outlineColor='blue'" tabindex="2">try me 2</button><br>
button:focus{
outline:none;
}
Upvotes: 0