Home >Web Front-end >CSS Tutorial >How to Achieve Smooth Gradient Animations in CSS?

How to Achieve Smooth Gradient Animations in CSS?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-11 00:52:09842browse

How to Achieve Smooth Gradient Animations in CSS?

Implementing Smooth Gradient Animation with CSS

Question: Unnatural Gradient Animation

In the realm of CSS gradients, there's an age-old challenge that plagues many developers—the jerky, abrupt movement of gradients during animation. This jarring effect arises when the gradient abruptly changes its position at each animation step.

Consider the following example:

@keyframes gra {
  0% {
    background: linear-gradient(135deg, #ff670f 0%, #ff670f 21%, #ffffff 56%, #0eea57 88%);
  }
  50% {
    background: linear-gradient(135deg, #ff670f 0%, #ff670f 10%, #ffffff 40%, #0eea57 60%);
  }
  100% {
    background: linear-gradient(135deg, #ff670f 0%, #ff670f 5%, #ffffff 10%, #0eea57 40%);
  }
}

In this code, the gradient's position shifts suddenly between the three frames, resulting in a jerky animation. The goal is to achieve a seamless and smooth transition, mirroring the natural flow of color progression.

Answer: Gradient Animation Mastery

To unlock the secret of smooth gradient animation, we introduce a different approach. Instead of directly modifying the gradient's position, we shift its size. This subtle change leads to a striking improvement in the animation's fluidity:

#gradient {
  height: 300px;
  width: 300px;
  border: 1px solid black;
  font-size: 30px;
  background: linear-gradient(130deg, #ff7e00, #ffffff, #5cff00);
  background-size: 200% 200%;

  -webkit-animation: Animation 5s ease infinite;
  -moz-animation: Animation 5s ease infinite;
  animation: Animation 5s ease infinite;
}

@-webkit-keyframes Animation {
  0% {
    background-position: 10% 0%;
  }
  50% {
    background-position: 91% 100%;
  }
  100% {
    background-position: 10% 0%;
  }
}
@-moz-keyframes Animation {
  0% {
    background-position: 10% 0%;
  }
  50% {
    background-position: 91% 100%;
  }
  100% {
    background-position: 10% 0%;
  }
}
@keyframes Animation {
  0% {
    background-position: 10% 0%;
  }
  50% {
    background-position: 91% 100%;
  }
  100% {
    background-position: 10% 0%;
  }
}

In this improved code, we now manipulate the background-size property. The animation starts with a small gradient size, gradually increasing it until the gradient covers the entire element. This creates an effect where the colors appear to flow seamlessly across the element.

By employing this technique, we've achieved a graceful and captivating gradient animation, bringing your designs to life with a touch of elegance and motion.

The above is the detailed content of How to Achieve Smooth Gradient Animations in CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn