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');
}
});