How to properly set text caption on image with image on hover

798 Views Asked by At

i've been trying to use angular masonry. I've been trying to do something like when hovering over an image, the image will darken, and the caption will be more exposed (high-contrast) over the image to be able to read it properly. However, when i try to hover over the image, the caption, or in my case i call it header, within my image disappears (or apparently also affected by darkening the image).

To fully understand what i mean, please see this Sample Code on StackBlitz

I've been trying to do this in purely css so that in my angular code would've handle only those data, and screen transition (routerlinks). I leave styling to CSS and other Libraries (i.e Angular Masonry)

3

There are 3 best solutions below

5
user184994 On BEST ANSWER

The problem is that the .feed-header is sitting behind the filter, so you need to set a z-index on that class:

.masonry-item
.feed-container
.feed-header {
  position: absolute;
  width: 100%;
  padding: 10px;
  align-items: center;
  display: flex;
  z-index: 9;
}

Also, if you want to make the text contrast, you may want to set it to white on hover (to contrast with the dark filter). You can do so using:

.masonry-item
.feed-container:hover
.feed-header {
  color: white;
}

Here is a StackBlitz demo

0
AudioBubble On

This stackblitz should work

All I did was putting the header before the image and adding this as SASS to your CSS file :

ngxMasonryItem {
  .feed-text {
    color: white;
    z-index: 9999;
  }
  &:hover {
    img {
      filter:  blur(2px) brightness(50%)
    }
  }
}

(An removing the previous filter you used)

in HTML, declaration order is important, and so is z-index.

2
harkesh kumar On

Try This Demo

CSS file

.box {  

    cursor: pointer;  
    height: 468px;  
    float: left;  
    margin: 5px;  
    position: relative;  
    overflow: hidden;  
    width: 468px;  

}  

 .box img {  
    position: absolute;  
    left: 0;  
    -webkit-transition: all 300ms ease-out;  
    -moz-transition: all 300ms ease-out;  
    -o-transition: all 300ms ease-out;  
    -ms-transition: all 300ms ease-out;  
    transition: all 300ms ease-out;  
}  


.box .caption {  
    font-family: Tahoma;
    font-size: .5em;
    background-color: rgba(0,0,0,0.8);  
    position: absolute;  
    color: #fff;  
    z-index: 100;  
    -webkit-transition: all 300ms ease-out;  
    -moz-transition: all 300ms ease-out;  
    -o-transition: all 300ms ease-out;  
    -ms-transition: all 300ms ease-out;  
    transition: all 300ms ease-out;  
    left: 0;  
    opacity: 0;  
    width: 450px;  
    height: 450px;  
    text-align: left;  
    padding: 15px;
}  



 .box:hover .fade-caption {  
    opacity: 1;  
} 

HTML File

<div id="mainwrapper"> 

<!-- Image Caption 3 -->  
     <!-- Image Caption 3 -->  
    <div class="box">  
        <img id="image-3" src="https://www.w3schools.com/howto/img_avatar.png"/>  
        <span class="caption fade-caption">  
        <h3>Fade Caption</h3>  
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit,sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>  
        </span>  
    </div>

   <div class="box">  
        <img id="image-3" src="https://www.w3schools.com/howto/img_avatar.png"/>  
        <span class="caption fade-caption">  
        <h3>Fade Caption</h3>  
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit,sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>  
        </span>  
    </div>


</div>