Search code examples
csscss-animationslinear-gradientsbackground-position

How to make a CSS Shimmer Effect on Image?


I have the following shimmer effect working very well when used on a p element but it doesn't work on any div nor an img. So, what modifications should I make to make sure the shimmer effect plays on any element.

The snippet is below

 .shimmer {
      display: inline-block;
      color:grey;
      background: #acacac -webkit-gradient(linear, 100% 0, 0 0, from(#acacac), color-stop(0.5, #ffffff), to(#acacac));
      background-position: -50rem top; /*50px*/
      background-repeat: no-repeat;
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      -webkit-animation-name: shimmer;
      -webkit-animation-duration: 2.2s;
      -webkit-animation-iteration-count: infinite;
      -webkit-background-size: 50rem 100%; /*50px*/
      font-size: 90px;
    }
    
    @-webkit-keyframes shimmer {
        0% {
            background-position: -50rem top; /*50px*/
        }
        70% {
            background-position: 12.5rem top; /*200px*/
        }
        100% {
            background-position: 12.5rem top; /*200px*/
        }
    }
<div>
<p class="shimmer">Shimmering Text</p>

<div>
<img src="https://i.sstatic.net/MeQxk.png" width=100 height=100 alt="Image Should Shimmer.Unfortunately not working "/>
</div>
</div>


Solution

  • Use mask

    .shimmer {
      color: grey;
      display: inline-block;
      mask: linear-gradient(-60deg, #000 30%, #0005, #000 70%) right/350% 100%;
      animation: shimmer 2.5s infinite;
      font-size: 50px;
      max-width: 200px;
    }
    
    @keyframes shimmer {
      100% {
        mask-position: left
      }
    }
    <p class="shimmer">Shimmering Text</p>
    <img src="https://i.sstatic.net/MeQxk.png" class="shimmer" />