3

Using css i try to convert image from colour to black and White. But its not happen in the top of the another div.

My code shown below

.home-center {
  display: flex;
  justify-content: center;
  flex-direction: column;
  text-align: center;
  color: #fff;
}

.home-center-text {
  position: absolute;
  width: 100%;
  z-index: 1;
}

.home-center img {
  -webkit-filter: grayscale(100%);
  -moz-filter: grayscale(100%);
  -ms-filter: grayscale(100%);
  filter: grayscale(100%);
  filter: gray;
}

.home-center img:hover {
  -webkit-filter: none;
  -moz-filter: none;
  -ms-filter: none;
  filter: none;
}
<div class="col-xs-12 col-sm-6">
  <div class="home-center">
    <div class="home-center-text">
      <h2>our film</h2>
      <span>See our work</span>
    </div>
    <img src="http://reussis.com/demo/studio/images/Home_Our_Film.jpg" class="img-responsive">

  </div>
</div>

Sample test link https://jsfiddle.net/rijo/29eo2kkL/1/

Gerard
  • 15,418
  • 5
  • 30
  • 52
Rijo
  • 2,963
  • 5
  • 35
  • 62

3 Answers3

4

Apply :hover not to image, but to wrapper:

.home-center {
  display: flex;
  justify-content: center;
  flex-direction: column;
  text-align: center;
  color: #fff;
}

.home-center-text {
  position: absolute;
  width: 100%;
  z-index: 1;
}

.home-center img {
  -webkit-filter: grayscale(100%);
  -moz-filter: grayscale(100%);
  -ms-filter: grayscale(100%);
  filter: grayscale(100%);
  filter: gray;
}

.home-center:hover img {
  -webkit-filter: none;
  -moz-filter: none;
  -ms-filter: none;
  filter: none;
}
<div class="col-xs-12 col-sm-6">
  <div class="home-center">
    <div class="home-center-text">
      <h2>our film</h2>
      <span>See our work</span>
    </div>
    <img src="http://reussis.com/demo/studio/images/Home_Our_Film.jpg" class="img-responsive">

  </div>
</div>
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • Can you explain why this happens? It should work on img too, right? Or is it about the display property? – Abhimanyu May 13 '22 at 20:50
0

Use :hover on wrapper no img :

.home-center:hover img{
  -webkit-filter: none;
  -moz-filter: none;
  -ms-filter: none;
  filter: none;
} 

.home-center {
  display: flex;
  justify-content: center;
  flex-direction: column;
  text-align: center;
  color: #fff;
}

.home-center-text {
  position: absolute;
  width: 100%;
  z-index: 1;
}

.home-center img {
  -webkit-filter: grayscale(100%);
  -moz-filter: grayscale(100%);
  -ms-filter: grayscale(100%);
  filter: grayscale(100%);
  filter: gray;
}

.home-center:hover img{
  -webkit-filter: none;
  -moz-filter: none;
  -ms-filter: none;
  filter: none;
}
<div class="col-xs-12 col-sm-6">
  <div class="home-center">
    <div class="home-center-text">
      <h2>our film</h2>
      <span>See our work</span>
    </div>
    <img src="http://reussis.com/demo/studio/images/Home_Our_Film.jpg" class="img-responsive">

  </div>
</div>
Ehsan
  • 12,655
  • 3
  • 25
  • 44
0

You can trigger effects on another element based on hover of another element. Like this.

.home-center-text:hover ~ .home-center-img 
{  /* Do stuff here */ }

.home-center {
  display: flex;
  justify-content: center;
  flex-direction: column;
  text-align: center;
  color: #fff;
}

.home-center-text {
  position: absolute;
  width: 100%;
  z-index: 1;
}

.home-center-img {
  -webkit-filter: grayscale(100%);
  -moz-filter: grayscale(100%);
  -ms-filter: grayscale(100%);
  filter: grayscale(100%);
  filter: gray;
}

.home-center-img:hover {
  -webkit-filter: none;
  -moz-filter: none;
  -ms-filter: none;
  filter: none;
}

.home-center-text:hover ~ .home-center-img {
  -webkit-filter: none;
  -moz-filter: none;
  -ms-filter: none;
  filter: none;
}
<div class="col-xs-12 col-sm-6">
  <div class="home-center">
    <div class="home-center-text">
      <h2>our film</h2>
      <span>See our work</span>
    </div>
    <img src="http://reussis.com/demo/studio/images/Home_Our_Film.jpg" class="img-responsive home-center-img">

  </div>
</div>

Based on this answer.

Rithwik
  • 1,128
  • 1
  • 9
  • 28