Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When input has :focus, dont trigger :hover styles

Tags:

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); } 
like image 901
guidsen Avatar asked Jul 24 '14 01:07

guidsen


People also ask

How do you use hover and focus?

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 .

Is focus and hover the same?

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

What is difference between focus and active?

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


1 Answers

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.

like image 71
arielnmz Avatar answered Sep 21 '22 19:09

arielnmz