I got a input which I styled with a :focus
and :hover
. When the input has :focus
, I don't want to have the :hover
style triggered when I hover over the input.
How am I supposed to style that?
My css is as follow:
.form-control:hover { border-color: #a9a9a9; } .form-control:focus { box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 2px rgba(102,175,233,.6); -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 2px rgba(102,175,233,.6); }
Typically, a user will first hover over an element, then bring it to focus, then activate it. So, the best way to order your pseudo-class styles are :hover then :focus then :active .
Hover: by putting your cursor over it. A hovered element is ready to be activated with a mouse or any mouse-emulating technology (such as eye and motion tracking). Focus: a focused element is ready to be activated with a keyboard or any keyboard-emulating technology (such as switch devices).
:focus is when an element is able to accept input - the cursor in a input box or a link that has been tabbed to. :active is when an element is being activated by a user - the time between when a user presses a mouse button and then releases it.
There's an specific CSS selector for this, the :not
selector. And it has good compatibility:
a:hover:not(:focus) { color: magenta; } a:focus:not(:hover) { color: cyan; }
<a href="example.com">Stackoverflow</a>
I also suggest you give preference to the focus
event, since it's somewhat more "static" than the hover state, with something like this:
a:hover:not(:focus) { color: magenta; } a:focus { color: cyan; }
<a href="example.com">Stackoverflow</a>
And for a backwards-compatible alternative:
a:hover { color: magenta; } a:focus { color: cyan; } a:focus:hover { color: cyan; }
<a href="example.com">Stackoverflow</a>
In simple words:
You have a rule for each state (magenta for hover
and cyan for focus
) and one for both, giving preference (visually) to the focus
state: cyan.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With