Search code examples
htmlcsscss-mask

masking image with CSS


I made a design like this

enter image description here

How to mask the background with css?

I have tried code like this

.img-poster {
  display: block;
  max-width: 100%;
  -webkit-mask-image: url(https://cdn.pbrd.co/images/GYiCod1.png), url(https://cdn.pbrd.co/images/GYiCQsM.png);
  -webkit-mask-position: bottom center, center center;
  -webkit-mask-repeat: no-repeat, no-repeat;
}

.add {
  -webkit-mask-composite: add;
}
<section class="section poster-container">
  <img src="https://imagejournal.org/wp-content/uploads/bb-plugin/cache/23466317216_b99485ba14_o-panorama.jpg" alt="" class="img-poster add img-responsive">
</section>

The mask image I used are

https://i.sstatic.net/fg2k5.png

https://i.sstatic.net/zmylJ.png

Can you guys tell me what is wrong in my code? I know I can just import to png, but I tried to use css


Solution

  • You only need to consider one image which is the bottom part of your mask then use a simple gradient for the other part. You also don't need mask-composite. Simply adjust the size/position so that both mask don't overlap:

    .img-poster {
      display: block;
      max-width: 100%;
      -webkit-mask:  
         linear-gradient(#fff,#fff)              top,
         url(https://i.ibb.co/5WvbqgG/zmylJ.png) bottom;
      -webkit-mask-size:
         100% calc(100% - 30px),
         auto 30px;
      -webkit-mask-repeat: repeat-x;
      mask:  
         linear-gradient(#fff,#fff)              top,
         url(https://i.ibb.co/5WvbqgG/zmylJ.png) bottom;
      mask-size:
         100% calc(100% - 30px),
         auto 30px;
      mask-repeat: repeat-x;
    }
    <section class="section poster-container">
      <img src="https://imagejournal.org/wp-content/uploads/bb-plugin/cache/23466317216_b99485ba14_o-panorama.jpg" alt="" class="img-poster add img-responsive">
    </section>