Skip to main content

Command Palette

Search for a command to run...

Moving Gradient animation with CSS ๐ŸŽจ

#2 CSS Tips and Tricks

Published
โ€ข2 min read
Moving Gradient animation with CSS ๐ŸŽจ
S

Hi folks! I am a ๐Ÿ‘จโ€๐Ÿ’ป Full-Stack Developer, occasional Designer, and Blogger facilitating the world with User Experience ๐Ÿง as a Design Thinker ๐Ÿ’ญ and User-Centric Developer ๐Ÿ’ฏ and while also exploring โ˜๏ธ Cloud

Working ๐Ÿ’ผ @HackerRank as a Software Development Engineer 2

๐Ÿค“ I have a keen interest in ๐Ÿค collaborating with others and empowering others to build digital solutions that solve real-world ๐ŸŒ problems. I'm a Creative Technologist who believes that the merger between Design Thinking and Digital Technologies will lead to the building of user-centric solutions that are impactful toward the betterment of business & society.

Simple animations can bring a huge difference to your websites. Let's build something simple and unique.

Complete page background animation with CSS moving gradient

The above Codepen example shows a moving gradient animation on the whole body of the website.

Code snippet for the animation effect.

HTML ๐Ÿ‘‡

<body>
  <h1>
     Animation effect on "body"
  </h1>
</body>

CSS ๐Ÿ‘‡

body {
  background: -webkit-linear-gradient(
    -70deg,
    #a2facf,
    #64acff
  );
  background-size: 400% 400%;
  animation: gradient 5s ease infinite;
}

@keyframes gradient {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

So, What is happening ??

The background size here is 400% 400% and the background is moving. Thus, the animation is happening.

Frame 2.png

Frame 3.png

A lot of cool stuff can be coded with this type of animation.

1. ๐ŸŒˆ Rainbow Effect

The above Codepen example shows a ๐ŸŒˆ rainbow effect.

This is similar to the animation that we saw in the previous example but with 7 colors.

2. Loader for cards and simple components.

The above Codepen example shows a loader ๐ŸŒ with the same gradient animation.

3. Implementing the same effect on a button or cards

The above Codepen example shows ๐ŸŒˆ the rainbow effect on a button.

4. On Hover the same animation effect

Animation on hovering on the card

Configurable parameters

  • Changing the background-size
  • Changing the time-duration
  • Changing the degree of the linear gradient.

Now, put on your creative ๐Ÿค  hats and make an interesting animation effect with moving gradients.

Drop the website link in the comments for which you would be doing it or have done it.

Show your love by Sharing the blog. ๐Ÿค—

You can find me on the web ๐ŸŒ

R

This is a great write-up, thanks for sharing. I particularly appreciate the variety of CodePen examples given.

Could you have a look at Ian Forrest's CSS Gradient Animator and let us know what you think of it?

Also, how does changing the linear gradient degree impact the animation?

3
S

Thanks a lot. ๐Ÿค— Rishav

The CSS Gradient Animator is a great tool. Thanks for sharing getting code snippet by directly setting up the varials. It will be a helpful tool. โœŒ

Check out this example to see how it affects the animation.

In a simple explanation, the value of deg in linear-gradient defines the deg between the line between two gradients and the horizontal line.

You can check this for a detailed description. ๐Ÿ‘‡ https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient()#composition_of_a_linear_gradient

1