Test365i
365i.co.uk
Back to all guides
CSS

A spinning gradient border with CSS @property

A spinning gradient border is a coloured ring that turns slowly around a button or panel. This guide builds one with CSS @property and a single class, the same technique running on 20 buttons and panels across this site, with no extra markup and no JavaScript.

15 minutesCustom codeElementor

Works on WordPress 7.2, Elementor 4.4.2 and Elementor Pro 4.4.2; animates in Chrome and Edge 85+, Safari 16.4+ and Firefox 128+

Editorial integrity: read our editorial standards.

Live demo, running the code from this guide on a bare page with no theme styles.
  1. Register the angle with @property

    An ordinary custom property is only text to the browser, so it has no idea what lies between 0deg and 360deg. Registering it with @property tells the browser the value is an angle, and that is what makes it animatable.

    Where the CSS goes: on this site it lives in one Elementor Pro Custom Code snippet (Elementor > Custom Code, location <head>, wrapped in a <style> tag). Without Elementor Pro, paste it into Appearance > Customize > Additional CSS, or into your child theme's style.css.

    An unregistered custom property jumps from 0deg to 360deg; a property registered as an angle moves through every value
    Registering the property is what gives the browser values in between to draw.
    spinning-border.cssCSS
    @property --edge-angle {
    	syntax: '<angle>';
    	inherits: false;
    	initial-value: 0deg;
    }

    The MDN reference puts the purpose of the at-rule in one line:

    The @property CSS at-rule is used to explicitly define CSS custom properties

    MDN Web Docs, @property reference

    "Explicitly" is the important word. Until a property is defined with a syntax, the browser treats it as a string, and a string cannot be part-way between two values. Defining it as an <angle> is the whole trick; everything after this step is ordinary CSS.

  2. Paint the gradient on the border box

    The element gets two backgrounds. The first is a solid fill clipped to the padding-box, the area inside the border. The second is a conic-gradient() clipped to the border-box, so only the 2px ring round the edge shows it. The transparent border makes room for that ring, and because it is a real border, border-radius rounds it with no extra work.

    Match the solid fill to the colour behind the element. Here it is the panel colour #131322 from the Test365i design system. The gradient starts from var(--edge-angle), which is the value the next step turns.

    A conic-gradient on the border box plus a solid fill on the padding box leaves a 2px gradient ring
    Two backgrounds, two boxes: the fill covers everything except the 2px border.
    spinning-border.cssCSS
    .spin-edge {
    	border: 2px solid transparent;
    	background:
    		linear-gradient(#131322, #131322) padding-box,
    		conic-gradient(from var(--edge-angle), #7453FE, #35D0FF, #F93CBB, #7453FE) border-box;
    	animation: edge-spin 4s linear infinite;
    }
    After step 2: the gradient ring is in place, standing still because nothing turns the angle yet.
  3. Turn the angle with @keyframes

    One keyframe is enough. The animation line in step 2 runs it every four seconds, at a steady speed, for ever. Because the angle is registered, the browser draws every position between 0 and 360 degrees instead of jumping from start to end.

    When I built this site, Elementor 4 did not keep animation properties inside a global class, so this keyframe sits in the same Custom Code snippet as step 1. Keep the two together and the class itself stays a plain style.

    spinning-border.cssCSS
    @keyframes edge-spin {
    	to { --edge-angle: 360deg; }
    }
    After step 3: the angle turns once every four seconds, so the edge spins.

    Una Kravets of the Chrome team described the gap this fills when she introduced the feature on web.dev:

    There's no way to smoothly animate (or interpolate) between gradient values

    Una Kravets, web.dev, "@property: giving superpowers to CSS variables"

    That is why the gradient itself never animates here. Its colours stay fixed and only the registered angle moves, which the browser can interpolate. Older versions of this effect rotated an extra, oversized layer behind the button and hid the overflow; registering the angle removes that layer completely.

  4. Add the spin-edge class in Elementor 4

    Select the button or container, open the Style tab and add spin-edge as a class. Elementor 4 prints a global class under the name you give it, so the CSS above finds the element with no extra selector. On this site the same class gives 20 buttons and panels across seven pages their moving edge, including the frames in the Elementor 4 element gallery.

    Outside Elementor, add the class to any element in your HTML:

    The spin-edge class on a button and on a div block
    One class turns any button or panel into a spinning edge.
    index.htmlHTML
    <a class="spin-edge" href="/contact/">Talk to us</a>
  5. Stop the spin for people who prefer less motion

    Some visitors ask their device to reduce motion. This media query stops the turn for them and leaves a still gradient edge, which still looks finished. It is the same rule that calms the animated aurora background on this site.

    spinning-border.cssCSS
    @media (prefers-reduced-motion: reduce) {
    	.spin-edge { animation: none; }
    }
    The finished border. With Reduce motion on in your device settings, the edge holds still.

What the spinning gradient border does not do

Browsers without @property show the gradient standing still. Chrome and Edge 85, Safari 16.4 and Firefox 128 and later all animate it.

The padding-box fill must be a solid colour, so the edge suits elements on a plain background, not a button sitting over a photo.

Animating a custom property repaints the element on every frame. It is fine for a handful of buttons and panels, as on this site, but I would not put it on every card in a long grid.

All the spinning gradient border code

Each file below is complete. Copy it, or download it under the name shown.

spinning-border.cssCSS
@property --edge-angle {
	syntax: '<angle>';
	inherits: false;
	initial-value: 0deg;
}

.spin-edge {
	border: 2px solid transparent;
	background:
		linear-gradient(#131322, #131322) padding-box,
		conic-gradient(from var(--edge-angle), #7453FE, #35D0FF, #F93CBB, #7453FE) border-box;
	animation: edge-spin 4s linear infinite;
}

@keyframes edge-spin {
	to { --edge-angle: 360deg; }
}

@media (prefers-reduced-motion: reduce) {
	.spin-edge { animation: none; }
}
index.htmlHTML
<a class="spin-edge" href="/contact/">Talk to us</a>

Spinning gradient border questions

Does a spinning gradient border need JavaScript?

No. The angle is registered with @property and turned by a CSS keyframe, so it runs with JavaScript switched off.

Which browsers support CSS @property?

Chrome and Edge from version 85, Safari from 16.4 and Firefox from 128. Older browsers ignore the registration and show the same gradient border standing still, so nothing breaks.

Can I use this in Elementor without Elementor Pro?

Yes. Elementor Pro gives you the Custom Code screen used on this site, but the same CSS works in Appearance > Customize > Additional CSS or a child theme stylesheet. In Elementor you only add the class.

How do I change the colours or the speed?

Change the colour stops inside conic-gradient(), keeping the first and last the same so the loop has no seam. Change 4s in the animation line: a higher number turns more slowly.

Will it work on a card or a panel, not just a button?

Yes. The class works on any element with a solid background. Give panels a larger border-radius, as the demo does.

Does the animation affect Core Web Vitals?

It does not move anything, so it adds no layout shift. It repaints the element each frame, so keep it to a few accents rather than dozens of elements.

Can I add a glow as well?

Yes. A soft box-shadow in one of the gradient colours sits well behind the ring. Box Shadow Generator, made by Mark McNeece of 365i, builds one you can copy.

Sources

  1. MDN Web Docs: @property
  2. MDN Web Docs: conic-gradient()
  3. Una Kravets, web.dev: @property, giving superpowers to CSS variables
  4. Bramus Van Damme, Bram.us: Animating a CSS gradient border

More build guides

The latest guides, newest first.

Elementor

A clickable card grid with the Elementor 4 Collection loop

The Elementor 4 Collection loop repeats one design for every post a query finds. On this site it lists the build guides, which are a JetEngine post type, in the element gallery, the Lab and the mega menu, all from one card component. This guide builds that grid, makes each whole card clickable and hides fields that come back empty.

25 minutes Get the guide
Elementor

Style the Elementor 4 Carousel element: arrows, dots and loop

The Elementor 4 Carousel element gives you slides, arrows and dots as separate atomic parts. Out of the box on this site, the arrows sat on top of the slide titles, the active dot was black on a dark panel and Loop did nothing. This guide fixes all three, with the CSS the element gallery now runs.

20 minutes Get the guide
Elementor

The Elementor 4 Accordion element, built on native details

The Elementor 4 Accordion element is built on the browser's own details and summary elements, so it opens and closes with no script. This guide sets it up the way the element gallery on this site does, styles it with classes, and explains the FAQ schema switch and what Google now does with it.

15 minutes Get the guide