How to make a gradient transition in CSS

CSS doesn't support transition for background gradient. It will instead change the whole gradient suddenly. But what if you could? I wanna show you how I worked around the limitation of CSS with this nice little trick.

2 min read

November 9, 2020

sunset by the beach

The problem

Have you ever been asked by a designer if you could make a transition from one background gradient to another? Something like that should work, right?

.gradient {
  background: linear-gradient(red, blue);
  transition: background 0.2s ease-in

.invert {
  background: linear-gradient(blue, red);

Well, I did try it when this was part of a design I had to implement this week. Sadly, it doesn't work. The page will instead change suddenly from one gradient to another 😞. And it's pretty understandable. It would be a quite complex feature to take all the pixels of the gradient and compute the transition to another color.

The workaround

This might not work for every issue but for simple gradients, you can clearly use this little hack to get pretty neat visual effects.

The idea is to use a transparent gradient as an overlay and use the simple background-color transition. To obtain a very similar effect.

<div class="container" id="container">
  <div class="gradient"></div>
.container {
  background: red;
  transition: background 1s ease-in;
  width: 400px;
  height: 400px;

.gradient {
  background: linear-gradient(white, transparent);
  width: 100%;
  height: 100%;

So, now we have this color gradient from red to white. And if we change the .container color we'll have the 1s ease in transition.

Show me the code

Okay, here's a better example!

See the Pen

Color Gradient</a> by Marc-Antoine Ferland (@maferland)

Hey there 👋

I hoped you like this little sample of my brain 🧠. If you have any questions, please reach out 🙏

Marc-Antoine Ferland


Hello, I'm Marc-Antoine. I absolutely love crafting elegant solutions to a wide array of problems. I'm fascinated by theory-crafting and I will love challenging the status quo. I love efficient and people-driven cultures. Work should adapt to your lifestyle, not the other way around. I'm currently a Senior Frontend Engineer at Capdesk 💻✌️