4

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?

goltornate
  • 103
  • 1
  • 10

3 Answers3

5

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

Steven Lambert
  • 5,571
  • 2
  • 29
  • 46
0

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>

if you want on click Tab (in keyboard)
button:focus{
outline:none;
}
לבני מלכה
  • 15,925
  • 2
  • 23
  • 47
0

This is a no go.

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).

Community
  • 1
  • 1
Adam
  • 17,838
  • 32
  • 54
  • as long as the ":focus" styling works, you should be ok with wcag 2.4.7. mouse focus isn't required under wcag. i think the OP was only trying to remove mouse focus and leave keyboard focus alone. with voice recog (eg, dragon), the ":focus" should still work. – slugolicious May 22 '18 at 19:19