albert.walickihire me
← all writing
css3 min read

Pure CSS animated gradient colour button is easier than you think!

Let's learn how to create an animated gradient color button with animated borders and text! One single CSS property will handle all animations

Albert Walicki · frontend developer in Gdańsk
CSS button with gradient border

Most buttons are boring. Solid, standard borders, most of them are not aligned correctly. In this case, let’s learn how to create a gradient colour button with animated borders and text! One single CSS property will handle all animations.

Outline button - Easy, straight and boring

In the first place, let’s start with a basic outline button with hover can be created like this:

<a href="/" title="Hello button" class="btn">Hello</a>
Preview
Hello

Gradient button and text

One step further - adding gradient borders and text.

To achieve that, we need to do a few things:

<a class="btn" href="/" title="Hello">Hello</a>
Preview
Hello

Awesome! You have created a gradient border button with gradient text! Now we will bring it to life with extra CSS.

Final boss - animated gradient button

In CSS we can’t transition gradients. It would be awesome to see smooth animation with CSS like this:

.gradient {
  background-image: linear-gradient(red, blue);
  transition: background-image 0.5s linear;
}

.gradient:hover {
  background-image: linear-gradient(green, orange);
}

But it won’t work. It immediately changes to the other one without transition. There are a few hacks to do it, but my favourite is to animate background-position.

Firstly, we need to add two properties to our button:

  • background-size: 200% auto;
  • background-position: left center;

Then on hover:

  • background-position: right center;

In this case, I added a gradient starting with white colour. It enhances the impression of an animated border.

<a class="btn" href="/" title="Hello">Hello</a>
Preview
Hello

And that’s it!

You can play with the final button Here.

cssguide
read next
work with me

Got something that needs building?

Frontend builds, full-stack features in Django, design-system work. Available for work.

See my workGet in touch