2

I am trying to create a pulsating circular background with smooth edges. For the circle with smooth edges I am using this CSS code:

background: radial-gradient(black, black, transparent, transparent);

Using my code below works well to animate the background-color. However, as soon as I replace the background-color with this radial-gradient background the animation jumps and is no longer smooth. The behavior is consistent over multiple Browsers. This is a minimal working example of the issue I am having:

.global {
  background: lightskyblue;
}

.silver {
  // background: radial-gradient(black, black, transparent, transparent);
  animation: pulse 3s infinite;
}

@keyframes pulse {
  0%,
  100% {
    // background-color: black;
    background: radial-gradient(black, transparent, transparent, transparent);
  }
  50% {
    // background-color: white;
    background: radial-gradient(black, black, transparent, transparent);
  }
}
<body class="global">
  <img src="pngwave.png" alt="test" class="silver" />
</body>

I have found this Stackoverflow question which is similar but did not help me solve my problem.

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
JRsz
  • 2,891
  • 4
  • 28
  • 44

1 Answers1

3

You need to animate the background-size

.box {
  width: 200px;
  height: 200px;
  background: radial-gradient(farthest-side,black, transparent) center no-repeat;
  animation:pulse 2s linear infinite alternate;
}
@keyframes pulse{
  from {
    background-size:50% 50%;
  }
  to {
    background-size:100% 100%;
  }
}
<div class="box"></div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • Actually I have another very related problem: Is it possible to do this with the actual background-color? The reason is that the element I am working with has a background image and background replaces the background-image while background-color works well with a background-image. – JRsz Apr 21 '20 at 21:27
  • @JRsz use multiple background: https://jsfiddle.net/xr3h52od/ – Temani Afif Apr 21 '20 at 21:44