Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between parent *:hover {} and parent:hover * {}?

I'm wondering what is the difference between CSS selectors. If I'm changing this:

.parent *:hover {}

to this:

.parent:hover * {}

in the subsequent code:

#child {
  width: 100%;
  height: 100%;
}
.parent {
  background-color: green;
  width: 100px;
  height: 100px;
}

.parent:hover * { 
  background-color: red;
}
<html>
  <head>
    <title>HTML Sample</title>
  </head>
  <body>
    <div class="parent">
      <div id="child"></div>
    </div>
  </body>
</html>

Nothing changes and hover effect remains same. Am I missing smth here?

like image 882
gokareless Avatar asked Jan 03 '23 07:01

gokareless


2 Answers

  • .parent *:hover {} means: target all descendants of any element with class="parent", while in a hover-state" (that is, while the descendant is hovered)

  • .parent:hover * {} means target all descendants of any element with class="parent", while the element with class="parent" is in a hover-state (note that if a descendant element is hovered, so is the .parent, even if they don't form a single cohesive visual unit (e.g. position: absolute is being used).

If your <div class="parent"> has multiple children then the first rule (.parent *:hover {}) will only affect a single descendant while that specific descendant is hovered - so if the user mouse-overs another element then the style-rule would change.

The second rule is such that provided that .parent is mouse-hovered, then all descendants will have their styles changed. This is not a good style rule because the descendant selector will select everything under .parent (e.g. every single <span>, <a>, <p>, etc). You probably should use a more specific selector.

like image 58
Dai Avatar answered Jan 14 '23 13:01

Dai


Easy. Please hover to the parrent and then to the child.

#child {
  width: 100%;
  height: 100%;
}
.parent {
  background-color: green;
  width: 100px;
  height: 100px;
  padding: 100px;
}

.parent:hover * { 
  background-color: red;
}

.parent *:hover { 
  background-color: blue;
}
<html>
  <head>
    <title>HTML Sample</title>
  </head>
  <body>
    <div class="parent">
      <div id="child"></div>
    </div>
  </body>
</html>
like image 30
Taras Gordienko Avatar answered Jan 14 '23 13:01

Taras Gordienko