悬停效果不起作用:将鼠标悬停在<a> anchor tag

时间:2015-12-29 12:23:01

标签: html css html5 css3

I'm trying to change the div color by hovering over an anchor tag but when I am but nothing is happening, here is my code below and jsfiddel link https://jsfiddle.net/rhulkashyap/udvzanqz/上时不会更改Div颜色。

HTML &amp;的 CSS

&#13;
&#13;
.hover-me{
  margin-bottom:50px;
  display:block;
}
.change{
  width:200px;
  height:200px;
  background:#00ACC1;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  font-size:20px;
  color:#fff;
}

.hover-me:hover .change{
  background:#00796B;
}
&#13;
<a href="#" class="hover-me">Change Div color by hovering me</a>

<div class="change">Hello Universe</div>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:5)

您需要使用sibling selector

&#13;
&#13;
.hover-me{
  margin-bottom:50px;
  display:block;
}
.change{
  width:200px;
  height:200px;
  background:#00ACC1;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  font-size:20px;
  color:#fff;
}

.hover-me:hover ~ .change{
  background:#00796B;
}
&#13;
<a href="#" class="hover-me">Change Div color by hovering me</a>
<div class="change">Hello Universe</div>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

这是完美的,但你想要:

.hover-me:hover + .change{
      background:#00796B;
    }

这里有jsFiddle演示:https://jsfiddle.net/udvzanqz/2/

答案 2 :(得分:1)

只需按照以下方式更改您的CSS: -

.hover-me:hover+.change{
    background:#00796B;
}

它可能对你有帮助。

答案 3 :(得分:1)

&#13;
&#13;
.hover-me:hover ~ .change{
  background:#00796B;
}
&#13;
&#13;
&#13;