悬停多个子元素时如何设置父元素的背景样式?

How to style the background of the parent element when hovering more then one child element?

根据在 Stack overflow 中找到的to a trick,我可以像这样更改悬停子元素的父元素的背景:

parent sibling { }
parent sibling:hover { }
parent:hover sibling { }

但我需要更改悬停在不同子元素上的背景颜色。例如,将鼠标悬停在 Facebook 按钮上,背景变为蓝色,将鼠标悬停在 Google + 按钮上,背景变为红色。

我试过这个:

    <div class="share_box">
      <div class="white-container">

      <div class="facebook-sibling"/>
      <a class="facebook-child-button"/>

      <div class="twitter-sibling"/>
      <a class="twitter-child-button"/>

      <div class="googleplus-sibling"/>
      <a class="googleplus-child-button"/>

      </div>
  </div>

但是对于多个按钮它不起作用。我期望的结果类似于:

这是你想要的吗?

演示 1: http://jsfiddle.net/t73431y8/

演示 2: http://jsfiddle.net/t73431y8/2/

HTML:

<div class="PARENT">
<div class="RED">RED</div>
<div class="BLUE">BLUE</div>
<div class="GREEN">GREEN</div>   
</div>

CSS:

.PARENT{
    position: relative;
}

.RED{
    display: inline-block;
    margin: 5px;
    padding: 5px;
    color: #BB0000;
    background: #FFF;
}

.RED:hover:after{
    display: block;
    width: 100%;
    height: 100%;
    background: #BB0000;
    position: absolute;
    top: 0px;
    left: 0px;
    content: ' ';
    z-index: -1;
}

.BLUE{
    display: inline-block;
    margin: 5px;
    padding: 5px;
    color: #0000BB;
    background: #FFF;
}
.BLUE:hover:after{
    display: block;
    width: 100%;
    height: 100%;
    background: #0000BB;
    position: absolute;
    top: 0px;
    left: 0px;
    content: ' ';
    z-index: -1;
}
.GREEN{
    display: inline-block;
    margin: 5px;
    padding: 5px;
    color: #00BB00;
    background: #FFF;
}
.GREEN:hover:after{
    display: block;
    width: 100%;
    height: 100%;
    background: #00BB00;
    position: absolute;
    top: 0px;
    left: 0px;
    content: ' ';
    z-index: -1;
}

如果您设置父级 position: relative,它将包含任何 position: absolute 个子级。

在父元素的末尾创建一个新元素,然后position: absolute并调整它的位置和大小以填充父元素。

然后使用 z-index: -1 将其设置在其余内容(例如按钮)之后。

然后你可以使用 General Sibling Combinator (~) 到 select hovered 元素之后的新元素。

.facebook:hover ~ .background { background-color: rgb(50, 100, 150); }
.twitter:hover  ~ .background { background-color: rgb(50, 150, 250); }
.google:hover   ~ .background { background-color: rgb(250, 75, 50);  }

.share {
    position: relative;
}
.background {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
}                                                                                                                                                    /* Following styling for demo purposes only, not relevant */ .facebook:before { background-position:-46px -28px; width:101px; } .twitter:before { background-position:-151px -28px; width:90px; } .google:before { background-position:-245px -28px; width:94px; } .button:before { display:inline-block; content: ""; height:36px; background-image:url("http://i.stack.imgur.com/AXvMk.png"); border-radius: 3px; box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.2); } .button { display:inline-block; padding: 2px; } .white-container { padding: 10px 20px; font-size: 0; background: #fff; border-radius: 3px; } .background { background: #fff; } body { margin: 0 4px; border: 1px solid #aaa; border-top: 0px; box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.1) } .share { padding: 10px 15px; box-shadow: 0px 5px 5px -5px rgba(0,0,0,0.3) inset } body:before { content: ''; height: 4px; display: block; background: #fff; border-bottom: 1px solid #aaa } html { background: #efefef }
<div class="share">
    <div class="white-container">
      <a href="#" class="button facebook"></a>
      <a href="#" class="button twitter"></a>
      <a href="#" class="button google"></a>
      <div class="background"></div>
    </div>
</div>