Why does my img hover not work?

2019-03-04 07:20发布

问题:

I am trying to get to be set to an opacity: .04;, and when hovered over to be opacity:1; however, my img hover is not functioning. I am unsure of why this is. Can someone help me understand where my problem is?

my 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 -->

回答1:

Your .wrapper element is covering your image. This why mouse events do not trigger on the elements behind it.

I gave .wrapper { margin-top: 200px; } so you see it works as soon as that is fixed.

.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:

There are two possibilities in your code that if you want to make opacity 1 of background image by only hover mouse on that background there here is your code:

.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;
}
}
<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 -->

if you want to make background opacity to 1 once you hover your mouse on the front blue wrapper plus hover the mouse on background then here is the code:

.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;
}
}
<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 -->



回答3:

I moved the :hover selector to .intro-img instead

.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 -->



回答4:

Adjust your approach in your CSS. You are setting the opacity using the .intro-img class, so you will need to use the same class.

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


回答5:

I copied and pasted your code into a "jsfiddle" and changed the CSS for the hover; it now goes solid upon mouseover.

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


回答6:

It did not work because you used # instead of . on the css selector

<div class="pic1">

means you have to do

.pic1 img:hover{ opacity: 1; }



回答7:

Your pic1 is a class not an ID insead of #pic1 use .pic1



回答8:

'#pic1' in CSS select an html element with 'id' value of 'pic1', but 'pic1' is a 'class' value in your first html page...