49

I have two nested div's

<div class="parent">
    <div class="child"></div>
</div>

I want to change the background from .parent when I hover over .parent.

I want the background to turn normal again when I hover over .child.

For example, when I hover over the inner area I want the outer area to return to its original (gray) state:

.parent {
    width: 100px;
    height: 100px;
    padding: 50px;
    background: lightgray;
}

.parent:hover {
    background: lightblue;
}

.child {
    height: 100px;
    width: 100px;
    background: darkgray;
}

.child:hover {
    background: lightblue;
}
<div class="parent">
    <div class="child"></div>
</div>

I would like to keep this <div> structure and I do not want a JavaScript solution (I know the JavaScript solution but I want to keep it pure CSS).

This is not a duplicate of "how to style the parent when hovering over a child". I want to NOT style the parent when hovering over a child.

mhchem
  • 288
  • 1
  • 18
EaterOfCode
  • 2,202
  • 3
  • 20
  • 33

2 Answers2

29

Basically you can't : How to style the parent element when hovering a child element?

But a trick is to use a sibling element : http://jsfiddle.net/k3Zdt/8/

.parent {
  width: 100px;
  height: 100px;
  padding: 50px;
}

.child {
  height: 100px;
  width: 100px;
  background: #355E95;
  transition: background-color 1s;
  position: relative;
  top: -200px;
}

.child:hover {
  background: #000;
}

.sibling {
  position: relative;
  width: 100px;
  height: 100px;
  padding: 50px;
  top: -50px;
  left: -50px;
  background: #3D6AA2;
  transition: background-color 1s;    
}

.sibling:hover {
  background: #FFF;
}
<div class="parent">
    <div class="sibling"></div>
    <div class="child"></div>
</div>
Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
luxcem
  • 1,807
  • 22
  • 37
14

You can trick something ;)

Basically, use a :before pseudo-element for the child div, with its same size;

when you hover on the child div, enlarge the :before pseudo-element to cover the father div area; this will cause the father div hover effect to fall down, and then to come back to the original state. A precise combination of z-index is involved too.

Demo: http://jsfiddle.net/gFu8h/ Dark Magic(tm)

.parent {
    width: 100px;
    height: 100px;
    padding: 50px;
    transition: background-color 1s;
    background: #3D6AA2;    
    position: relative;
    z-index: 1;
}

.parent:hover{
    background: #FFF;    
}

.child {
    height: 100px;
    width: 100px;
    background: #355E95;
    transition: background-color 1s;
    position: relative;
}

.child:hover {    
    background: #000;
}

.child:before{
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    left: 0;        
    z-index: -1;
    transition: background-color 1s;
}

.child:hover:before{
    top: -50px;
    bottom: -50px;
    left: -50px;
    right: -50px;     
    background: #3D6AA2;    
}
<div class="parent">
    <div class="child"></div>
</div>
Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • 2
    +1 cool.. didn't thought about it. :) – Mr_Green Jul 29 '13 at 13:29
  • 2
    Thanks; this is only a proof-of-concept, it should be adjusted to be more dynamic and to handle the hover-out more properly, if needed (it restores the state only when going out of the father too (that is not the father anymore, because it is the transparent `:before` enlarged)), but at least... it is possible with pure CSS :) – Andrea Ligios Jul 29 '13 at 13:34