为什么我的img悬停不起作用?

时间:2016-12-17 17:00:54

标签: html css image hover opacity

我试图将其设置为opacity: .04;,然而当我将其悬停在opacity:1;时,我的img悬停功能无效。我不确定为什么会这样。有人可以帮我理解我的问题所在吗?

我的HTML:

.container2 {
  margin: 0px;
  padding: 0px;
}
.intro {
  width: 100%;
  position: relative;
  overflow: hidden;
  margin: 0;
  padding-top: 0;
}
.pic1 {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
}
.intro-img {
  display: block;
  margin: 0 auto;
  min-width: 100%;
  opacity: 0.4;
}
.pic1 img:hover {
  opacity: 1;
}
.wrapper {
  height: 200px;
  min-height: 200px;
  position: relative;
  padding-bottom: 0;
}
.blue {
  background: #22428e;
  color: #fff;
}
.green {
  background: #de0e00;
  color: #fff;
}
#colors-content {
  height: 200px;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  width: 330px;
  padding: 0 25px;
  margin-left: 3em;
}
h1 {
  text-transform: uppercase;
  font: 700 32px/35px'Gotham SSm A', 'Gotham SSm B', Arial, Helvetica, sans-serif;
  margin: 0 0 34px;
  padding-top: 28px;
}
p {
  margin-top: -10px;
}
a {
  text-decoration: none;
  color: white;
}
a:hover {
  color: black;
}
@media screen and (max-width: 454px) {
  .wrapper {
    min-height: 100px;
    position: relative;
  }
  #colors-content {
    height: 200px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 100%;
    padding: 0 25px;
    margin-left: -4px;
  }
  #pic1 {
    display: none;
  }
}
<div class="container2">
  <div class="intro">
    <div class="pic1">
      <img class="intro-img" src="https://s23.postimg.org/94clk2prf/resources_gender_spectrum2.png">
    </div>
    <!-- End pic1 -->
    <div class="wrapper">
      <article class="blue" id="colors-content">
        <div>
          <a href="">
            <h1> Gender
          Spectrum.com </h1> 
          </a>
          <p>I'm gone so i'me gone so long hah haha aha im bout it fuck it aiash hth as jasd ashd as ahsdjas r ajsgda uaju ashd ujawh uu ajshdhuiae</p>
        </div>
      </article>
      <!-- End of article -->
    </div>
    <!-- End wrapper -->
  </div>
  <!-- End intro -->
</div>
<! -- End of container2 -->

8 个答案:

答案 0 :(得分:2)

您的代码中有两种可能性,如果您想通过仅将鼠标悬停在该背景上来制作背景图像的不透明度,那么这里有您的代码:

&#13;
&#13;
.container2 {
  margin: 0px;
  padding: 0px;
}
.intro {
width: 100%;
position: relative;
overflow: hidden;
  margin: 0;
  padding-top: 0;
}
#pic1 {
    position: absolute;
    top: 0;
    width: 100%;
    height: 100%;
}
.intro-img {
      display: block;
    margin: 0 auto;
    min-width: 100%;
  opacity: 0.4;
}
#pic1 img:hover{
  opacity: 1;
}
.wrapper {
  height: 200px;
    min-height: 200px;
    position: relative;
  padding-bottom: 0;
    margin-left: 3em;
    width: 330px;
}
.blue {
   background: #22428e;
    color: #fff;
}
.green{
     background: #de0e00;
    color: #fff;
}
#colors-content {
    height: 200px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 330px;
    padding: 0 25px;
}
h1 {
      text-transform: uppercase;
    font: 700 32px/35px 'Gotham SSm A','Gotham SSm B',Arial,Helvetica,sans-serif;
    margin: 0 0 34px;
  padding-top: 28px;
}
p {
  margin-top: -10px;
}
a {
  text-decoration: none;
  color: white;
}
a:hover {
  color: black;
}
@media screen and (max-width: 454px) { 
  .wrapper {
    min-height: 100px;
    position: relative;
}
#colors-content {
  height: 200px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 100%;
    padding: 0 25px;
margin-left: -4px;
}
  #pic1 {
    display: none;
}
}
&#13;
<div class="container2">
<div class="intro">
  <div id="pic1">
     <img class="intro-img" src="https://s23.postimg.org/94clk2prf/resources_gender_spectrum2.png">
    </div> <!-- End pic1 -->
<div class="wrapper">
<article class="blue" id="colors-content">
  <div>
    <a href=""> <h1> Gender
      Spectrum.com </h1> </a>
    <p> I'm gone so i'me gone so long hah haha aha im bout it fuck it aiash  hth as jasd ashd as ahsdjas r ajsgda  uaju ashd ujawh uu ajshdhuiae</p>
  </div>
</article><!-- End of article -->
</div><!-- End wrapper -->
  </div> <!-- End intro -->
  </div> <! -- End of container2 -->
&#13;
&#13;
&#13;

如果您想将鼠标悬停在前蓝色包装上并将鼠标悬停在背景上,则将背景不透明度设为1,则此处为代码:

&#13;
&#13;
.container2 {
  margin: 0px;
  padding: 0px;
}
.wrapper:hover + div#pic1 img.intro-img {
    opacity: 1;
}
.intro {
width: 100%;
position: relative;
overflow: hidden;
  margin: 0;
  padding-top: 0;
}
#pic1 {
    position: absolute;
    top: 0;
    width: 100%;
    height: 100%;
}
.intro-img {
      display: block;
    margin: 0 auto;
    min-width: 100%;
  opacity: 0.4;
}
#pic1 img:hover{
  opacity: 1;
}
.wrapper {
  height: 200px;
    min-height: 200px;
    position: relative;
  padding-bottom: 0;
    margin-left: 3em;
    width: 330px;
    z-index: 9;
}
.blue {
   background: #22428e;
    color: #fff;
}
.green{
     background: #de0e00;
    color: #fff;
}
#colors-content {
    height: 200px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 330px;
    padding: 0 25px;
}
h1 {
      text-transform: uppercase;
    font: 700 32px/35px 'Gotham SSm A','Gotham SSm B',Arial,Helvetica,sans-serif;
    margin: 0 0 34px;
  padding-top: 28px;
}
p {
  margin-top: -10px;
}
a {
  text-decoration: none;
  color: white;
}
a:hover {
  color: black;
}
@media screen and (max-width: 454px) { 
  .wrapper {
    min-height: 100px;
    position: relative;
}
#colors-content {
  height: 200px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 100%;
    padding: 0 25px;
margin-left: -4px;
}
  #pic1 {
    display: none;
}
}
&#13;
<div class="container2">
<div class="intro">
<div class="wrapper">
<article class="blue" id="colors-content">
  <div>
    <a href=""> <h1> Gender
      Spectrum.com </h1> </a>
    <p> I'm gone so i'me gone so long hah haha aha im bout it fuck it aiash  hth as jasd ashd as ahsdjas r ajsgda  uaju ashd ujawh uu ajshdhuiae</p>
  </div>
</article><!-- End of article -->
</div><!-- End wrapper -->
  <div id="pic1">
     <img class="intro-img" src="https://s23.postimg.org/94clk2prf/resources_gender_spectrum2.png">
    </div> <!-- End pic1 -->

  </div> <!-- End intro -->
  </div> <! -- End of container2 -->
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您的.wrapper元素涵盖您的图片。这就是为什么鼠标事件不会触发它背后的元素。

我给了.wrapper { margin-top: 200px; },所以一旦修复,你就会发现它有效。

.container2 {
  margin: 0;
  padding: 0;
}
.intro {
  width: 100%;
  position: relative;
  overflow: hidden;
  margin: 0;
  padding-top: 0;
}
.pic1 {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
}
.intro-img {
  display: block;
  margin: 0 auto;
  min-width: 100%;
  opacity: 0.4;
}
.pic1 img:hover {
  opacity: 1;
}
.wrapper {
  height: 200px;
  min-height: 200px;
  position: relative;
  padding-bottom: 0;
  margin-top: 200px;
}
.blue {
  background: #22428e;
  color: #fff;
}
.green {
  background: #de0e00;
  color: #fff;
}
#colors-content {
  height: 200px;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  width: 330px;
  padding: 0 25px;
  margin-left: 3em;
}
h1 {
  text-transform: uppercase;
  font: 700 32px/35px'Gotham SSm A', 'Gotham SSm B', Arial, Helvetica, sans-serif;
  margin: 0 0 34px;
  padding-top: 28px;
}
p {
  margin-top: -10px;
}
a {
  text-decoration: none;
  color: white;
}
a:hover {
  color: black;
}
@media screen and (max-width: 454px) {
  .wrapper {
    min-height: 100px;
    position: relative;
  }
  #colors-content {
    height: 200px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 100%;
    padding: 0 25px;
    margin-left: -4px;
  }
  #pic1 {
    display: none;
  }
}
<div class="container2">
  <div class="intro">
    <div class="pic1">
      <img class="intro-img" src="https://s23.postimg.org/94clk2prf/resources_gender_spectrum2.png">
    </div>
    <!-- End pic1 -->
    <div class="wrapper">
      <article class="blue" id="colors-content">
        <div>
          <a href="">
            <h1> Gender
          Spectrum.com </h1> 
          </a>
          <p>I'm gone so i'me gone so long hah haha aha im bout it fuck it aiash hth as jasd ashd as ahsdjas r ajsgda uaju ashd ujawh uu ajshdhuiae</p>
        </div>
      </article>
      <!-- End of article -->
    </div>
    <!-- End wrapper -->
  </div>
  <!-- End intro -->
</div>
<! -- End of container2 -->

答案 2 :(得分:0)

我将:hover选择器移到.intro-img而不是

.container2 {
  margin: 0px;
  padding: 0px;
}
.intro {
  width: 100%;
  position: relative;
  overflow: hidden;
  margin: 0;
  padding-top: 0;
}
#pic1 {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
}
.intro-img {
  display: block;
  margin: 0 auto;
  min-width: 100%;
  opacity: 0.4;
}
.intro-img:hover {
  opacity: 1;
}
.wrapper {
  height: 200px;
  min-height: 200px;
  position: relative;
  padding-bottom: 0;
}
.blue {
  background: #22428e;
  color: #fff;
}
.g
<div class="container2">
  <div class="intro">
    <div class="pic1">
      <img class="intro-img" src="https://s23.postimg.org/94clk2prf/resources_gender_spectrum2.png">
    </div>
    <!-- End pic1 -->
    <div class="wrapper">
      <article class="blue" id="colors-content">
        <div>
          <a href="">
            <h1> Gender
      Spectrum.com </h1> 
          </a>
          <p>I'm gone so i'me gone so long hah haha aha im bout it fuck it aiash hth as jasd ashd as ahsdjas r ajsgda uaju ashd ujawh uu ajshdhuiae</p>
        </div>
      </article>
      <!-- End of article -->
    </div>
    <!-- End wrapper -->
  </div>
  <!-- End intro -->
</div>
<! -- End of container2 -->

答案 3 :(得分:0)

在CSS中调整您的方法。 您正在使用.intro-img类设置不透明度,因此您需要使用相同的类。

img.intro-img:hover {
    opacity: 1;
}

答案 4 :(得分:0)

我将您的代码复制并粘贴到&#34; jsfiddle&#34;并改变了悬停的CSS;它现在在鼠标悬停时变得坚固。

.intro-img:hover{
  opacity: 1;
}

答案 5 :(得分:0)

它没有用,因为你用的是#而不是。在css选择器上

<div class="pic1">

意味着您必须

.pic1 img:hover{ opacity: 1; }

答案 6 :(得分:0)

你的pic1不是#pic1使用.pic1

的ID

答案 7 :(得分:0)

&#39;#PIC1&#39;在CSS中选择一个带有&#39; id&#39;的html元素值为#pic;&#39;但是&#39; pic1&#39;是一个班级&#39;第一个html页面中的值......

相关问题