Animating a gradient border in CSS

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Hello, amazing tutorial :)
This effect apparently does not work in Firefox, any hint about it?
Hi,
yeah unfortunately the @Property is only support in Chromium.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

A while ago, I made this cool animated Multi-colour text effect in CSS. I was re-looking this article and thought we could use some of these learnings to make a super cool animated border effect!
The end result for today will be this cool animated border gradient.
Gradient borders in CSS are tricky, as we need to leverage the border-image property for this. It's not a super well know property and comes with some caveats.
However, let's give it a go.
We'll first make a single div in our HTML setup.
<div></div>
Let's give this div a big width and height so we can have a good demo.
div {
width: 50vmin;
height: 50vmin;
border: 1rem solid;
}
This will create a div that is half the width of our viewport. And give this div a 1rem border.
The next thing we want to add is the actual border-image. We can pass an actual image or set SVGs or a gradient as this gets rendered as an image.
div {
width: 50vmin;
height: 50vmin;
border: 1rem solid;
border-image: linear-gradient(0deg, #12c2e9, #c471ed, #f64f59) 1;
}
We should now have our basic border setup, and it should look like this.

Pretty close already. Now we just need to animate this! Let's use some CSS variables to make the position animatable.
div {
--angle: 0deg;
width: 50vmin;
height: 50vmin;
border: 1rem solid;
border-image: linear-gradient(var(--angle), #12c2e9, #c471ed, #f64f59) 1;
}
This introduces a new --angle variable. This is nothing that CSS knows about but our own variable.
We can then add a basic animation query as well.
animation: 5s rotate linear infinite;
And the rotate animation will look like this:
@keyframes rotate {
to {
--angle: 360deg;
}
}
However, nothing will happen! The value will change, but since it's not a know CSS variable, we need to define it as a changeable property.
@property --angle {
syntax: '<angle>';
initial-value: 0deg;
inherits: false;
}
And now we got our working animated border in CSS!
For browser support, we have to technically look at multiple properties. Still, this one below should give you an excellent example of how good the support is.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter