Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the hover pseudo-class override the active pseudo-class

Tags:

The title basically says it all.

Suppose I have an element which I want to change color on :hover, but while clicked, I want it to switch back to its original color. So, I've tried this:

a:link, a:visited, a:active {     background: red; } a:hover {     background: green; } 

As it turns out, this doesn't work. After a lot of head-scratching, I realized that the :hover state was overriding the :active state. This was easily solved by this:

a:link, a:visited {     background: green; } a:hover {     background: red; } a:active {     background: green; } 

(I could combine the 1st rule with the 3rd one).

Here's the fiddle: http://jsfiddle.net/V5FUy/


My question: is this the expected behavior? As far as I understand this, the :active state should always override the :hover state, since the :active state will almost always be accompanied with the :hover state.

like image 917
Joseph Silber Avatar asked Sep 22 '11 00:09

Joseph Silber


People also ask

What does the pseudo-class hover do?

A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s). For example, :hover can be used to change a button's color when the user's pointer hovers over it.

Can you hover a pseudo element?

To clarify, you CAN NOT give :hover to a pseudo element. There's no such thing as ::after:hover in CSS as of 2018.

Which state should always come after hover?

Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective! a:active MUST come after a:hover in the CSS definition in order to be effective! Pseudo-class names are not case-sensitive.

Which of the following pseudo-class becomes active when a mouse moves over an element such as a link or an image?

The :hover pseudo-class, also called the “pointer hover pseudo-class”, applies when a pointing device interacts with an element without necessarily activating it. A typical example of this is when a mouse 🐭 hovers over an element.


1 Answers

yes this is expected behavior,

lets take a look at another example. just adding two classes,

<ul> <li class="item first">item</li> <li class="item">item</li> <li class="item">item</li> <li class="item">item</li> <li class="item last">item</li> </ul> 

here the class first also comes together with the class item. but if we declare our css in the wrong order that would not give the wanted behavior

.first { background: blue; } .item { background: red; } 

as you can see, the last matching selector will be used. it is the same as your example, no mather what is more logic, the 2 pseudo-classes are concidered equal, thus the same rules apply the last matching defenition wins.

edit

pseudoclasses are equals, it is the one defined last that wins! here is a jsFiddle that proves my point :link defined after :hover, :link wins (test) so, your statement of :hover overriding :link is wrong, its just the same like with :active, its all about the order.

like image 182
Sander Avatar answered Sep 28 '22 17:09

Sander