.parent {
background-color: #cde;
width: 100%;
position: relative;
height: 100px;
}
.parent:hover {
background-color: black;
}
.child {
float: right;
position: absolute;
left: 50%;
top: 50%;
padding: 10px;
background-color: #ff0000;
}
.child:hover {
background-color: yellow;
}
<div class="parent">
<span class="child">
Click
</span>
</div>
答案 0 :(得分:3)
一个技巧是在悬停上使用子元素创建另一个图层,以模拟父背景的不变。
以下是box-shadow
(On hover of child, change background color of parent container (CSS only))
.parent {
background-color: #cde;
width: 100%;
height: 100px;
position: relative;
overflow:hidden;
}
.parent:hover {
background-color: black;
}
.child {
position: absolute;
left: 50%;
top: 50%;
padding: 10px;
background-color: #ff0000;
}
.child:hover {
background-color: yellow;
box-shadow:0 0 0 1000px #cde;
}
<div class="parent">
<span class="child">
Click
</span>
</div>