DEV Community

D7460N
D7460N

Posted on • Edited on

2

How to animate or transition multiple linear-gradient backgrounds from a single element

Trying to emulate this Codepen but with a single link element and the animated underline wraps with the text. https://codepen.io/dragontheory/pen/JjorrKL

Text with animated underline

One way to have an underline wrap with the text is to use background linear gradients. Like this solid line at this Codepen...

https://codepen.io/dragontheory/pen/XWJydQE

Underline that wraps with text.

Then add three identical background linear-gradients colored the same as the background.

Not sure if backgroundColor or currentBackgroundColor (like currentColor) - is a valid property...

Three identical 6x1 background linear gradient shapes:

linear-gradient(backgroundColor, backgroundColor) left bottom / 6px 1px no-repeat,
linear-gradient(backgroundColor, backgroundColor) left bottom / 6px 1px no-repeat,
linear-gradient(backgroundColor, backgroundColor) left bottom / 6px 1px no-repeat;
Enter fullscreen mode Exit fullscreen mode

Then animate or transition them on top of the solid underline (from left bottom to right bottom), giving the illusion of broken lines.
And then stagger them (see first Codepen above).

  /* Staggered delay */
  animation: break01 2s infinite,
             break02 2s 0.5s infinite,
             break03 2s 1s infinite;

}
@-webkit-keyframes "break01" {
  from { background-position: left bottom; }
  to { background-position: right bottom; }
}
@-webkit-keyframes "break02" {
  from { background-position: left bottom; }
  to { background-position: right bottom; }
}
@-webkit-keyframes "break03" {
  from { background-position: left bottom; }
  to { background-position: right bottom; }
}
Enter fullscreen mode Exit fullscreen mode

So the question is how to animate or transition multiple linear-gradient backgrounds from a single element?

Hope that makes sense.

No idea if this is the best way. Open to ideas.

Anyone?

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (1)

Collapse
 
vallek profile image
Vallek • Edited

You need to write positions for each gradient in each @keyframe as comma-separated values for background-position. And then change them individually for different animations. Like this:

to {background-position: right bottom, left bottom, left bottom;}
Enter fullscreen mode Exit fullscreen mode

PS I know it's an old question, but I got here from google so maybe it will help someone.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay