12

I have a Burger Menu Button which is selectable via TAB. When I click on it and the menu opens up the burger has this blue outline to make it clear that it is focused. I don't want to remove that blue outline, because it helps vision impaired people and for tab selection it is also great, but is there a smart way to remove the blue outline only when someone clicks on it via mouse. Just asthetics...

Thanks for your answer.

cheers

Merc
  • 4,241
  • 8
  • 52
  • 81
  • The best solution for this problem is from guys from Facebook, see https://stackoverflow.com/questions/31402576/enable-focus-only-on-keyboard-use-or-tab-press#answer-51093815 – Inna Feb 22 '20 at 15:22

5 Answers5

2

As you pointed out, the blue outline is here for accessibility reasons.

If you click on the element, the keyboard focus will also move to that element.

So users have to know that the keyboard focus has been moved to that element.

Some people with disabilities may want to jump to a particular tab using the mouse, but then use their keyboard for easiness reasons.

Adam
  • 17,838
  • 32
  • 54
1

js:

$('#element').click(function(){
   $(this).addClass('mouse');
});
$('#element').blur(function(){
  if($(this).hasClass('mouse'){
     $(this).removeClass('mouse');
  }
});

css:

.mouse{
  outline: none;
}
Kuba Wojtach
  • 541
  • 3
  • 10
  • 1
    Doesn't the jquery click function include keyboard clicks as well? If so, mousedown/up might be better than click/blur. – stringy Mar 07 '17 at 04:08
0

If I understood correctly the question, try:

.myButton:active {outline: none;}

Ninjabin
  • 97
  • 5
  • 1
    But this would remove the outline when the user tabs to it too right? Which is not the desired behaviour. – Craig Aug 12 '21 at 07:52
0

Here's a simple solution, in plain Javascript, that works back to IE 10.

This answer is similar to @kuba's answer. Add/remove a class using JS to detect a mouse click or a tab button press.

javascript:

var htmlElement = document.querySelector('html');

document.addEventListener('click', function(){
  htmlElement.classList.add('clicking');
});

document.addEventListener('keyup', function(e){
  if (e.keyCode === 9) {
    htmlElement.classList.remove('clicking');
  }
});

Then turn off outline on :focus when the clicking class exists

CSS:

html.clicking .targetElement:focus {
   outline: none;
}

/* 
  or you can try dealing with all visibly focusable elements from the start.  I'm not sure if this is all of them, but it's good starting point.
*/

html.clicking a:focus,
html.clicking button:focus,
html.clicking input:focus,
html.clicking textarea:focus {
  outline: none;
}

Browser Compatibility:

querySelector IE 8+
element.classList IE 10+

jQuery alternative if you need to support browsers older than IE10.

$(document).on('click', function(){
  $('html').addClass('clicking');
});

$(document).on('keyup', function(){
  if (e.keyCode === 9) {
    $('html').removeClass('clicking');
  }
});
Francisc0
  • 968
  • 1
  • 14
  • 28
-1

Well you may want to do it this way:

div:active, div:focus{
  outline: none;
  border: none;
}

and maybe:

*{
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0) !important;
}
Beni Sinca
  • 378
  • 4
  • 15