Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css focus not working in safari and chrome

Tags:

css

focus

I got one strange problem which I never got before. Please see this code:

The css:

#btn{
    margin-left:150px;
    padding:10px;
    display:block;
}
#btn a{
    padding:5px 20px;
    background:green;
    color:#FFF;
    text-decoration:none;
    outline:none;
}
#btn a:hover{
    background:#933;    
}
#btn a:focus, #btn a:active{
    background:#CF0;
color:#000; 

}

Here the HTML

<div id="btn">
   <a href="#">Click here</a>
</div>

The focus and active css working well in firefox, but not in the chrome and safari.

like image 661
user687414 Avatar asked Apr 29 '11 13:04

user687414


People also ask

Does focus work on Safari?

Edit (8/29/2022)Safari has implemented :focus-visible in version 15.4.

How do I make focus-visible in CSS?

The :focus-visible pseudo-class applies while an element matches the :focus pseudo-class and the UA (User Agent) determines via heuristics that the focus should be made evident on the element. (Many browsers show a "focus ring" by default in this case.)

Which modern browsers do not show a focus indicator for a button that is clicked?

Buttons are not click focusable in Safari (though you can still focus them via keyboard with Option + Tab), as they don't receive focus on click they don't even match :focus , so they never show a focus ring on mouse interactions.

What is the difference between focus and focus-visible?

This state is usually indicated using the outline . Adding the :focus pseudo-class to an element will make it show a focus specific styles and disregard browsers heuristics. The :focus-visible , in contrast, applies custom styling only if it would be shown natively.


2 Answers

Yeah seems like little problem with focus in webkit. Not really a bug. Easily fixable in html. Just use tabindex.

     <a href="#" class="hide" tabindex="1">[hide]</a>
     <a href="#" class="show" tabindex="2">[show]</a>

ta da ...

like image 118
shobhonk Avatar answered Oct 07 '22 14:10

shobhonk


It's fixable, some additional code needed though...

<div id="btn">
    <a href="#" tabindex="1">Click here</a>
</div>

jsfiddle

I know it's ridiculous... You can read more here

Hope this helps

like image 35
calofanas Avatar answered Oct 07 '22 13:10

calofanas