Since there's no parent selector in CSS, you could use a workaround and apply a red outline to the .div2
when hovered and set an overflow: hidden
to .div1
, e.g.
.div1 {
...
overflow: hidden;
}
.div2 {
...
}
.div1:hover{
...
}
.div2:hover {
...
outline: 999em red solid ;
}
Example: http://jsfiddle.net/924zuneu/
With this approach when you hover .div2
the outer div still applies a yellow background, but the outline will cover it.
Another approach is to style a pseudoelement of .div2
, e.g.
.div1 {
...
position: relative;
z-index: 1;
}
.div2 {
...
}
.div1:hover {
...
}
.div2:hover{
...
}
.div2:hover:before {
content : "";
background : red;
position : absolute;
z-index : -1;
pointer-events: none;
top: 0; left: 0; right: 0; bottom: 0;
}
Here pointer-events
is needed otherwise when you leave the .div2
you wouldn't see the hover effect for .div1
Example: http://jsfiddle.net/kr4453bj/3/
Result (for both methods)
when .div1
is hovered

when .div2
is hovered
