Search code examples
cssanimationcss-animations

Background-position not working with CSS animation and linear gradient


I'm attempting to use CSS3 (gradients and animations) to make an animation similar to the iphone slide to unlock gradient. However, for some reason, the background-position only works when I use static pixel numbers and not when I use percentages. Ideally, I'd be able to apply this class to any element, and have the animation work, without needing to code for specific widths. Can somebody point me in the right direction?

JSFiddle: https://jsfiddle.net/ekLamtbL/

.slideToUnlock {
  background: linear-gradient(-90deg, black 0%, gray 40%, white 45%, gray 55%, black 60%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  color: transparent;
    -webkit-animation: slidetounlock 5s infinite;
    animation: slidetounlock 5s infinite;
    -webkit-animation-direction: normal;
    animation-direction: normal;
}

@-webkit-keyframes slidetounlock {
  0% {
    background-position: -100% 0;
  }
  100% {
    background-position: 100% 0;
  }
}

Solution

  • Added your code

    background-size: 250% 250%;
    

    Example

    .slideToUnlock {
      background: linear-gradient(-90deg, black 0%, gray 40%, white 45%, gray 55%, black 60%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
      color: transparent;
      -webkit-animation: slidetounlock 5s infinite;
      animation: slidetounlock 5s infinite;
      -webkit-animation-direction: normal;
      animation-direction: normal;
      background-size: 250% 250%;
    }
    

    https://jsfiddle.net/ekLamtbL/2/