I like your original idea with using overflow: hidden, but to make it work I had to include an extra wrapper div.

  • The outer wrapper defines a padding which serves as the display area for the gradient border
  • The inner div is just the content box with a black background

.loading-box-container {
  --size: 200px;
  --radius: 10px;
  position: relative;
  width: var(--size);
  height: var(--size);
  padding: var(--radius);
  border-radius: var(--radius);
  overflow: hidden;
}

.loading-box {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  background: #000;
  border-radius: var(--radius);
}

.loading-box-container::before {
  content: '';
  width: 150%; /* The upscaling allows the box to fill its container even when rotated */
  height: 150%;
  position: absolute;
  top: -25%; left: -25%;
  background: conic-gradient(#0000ff00, #ff0000ff);
  animation: rotate-border 5s linear infinite;
}

@keyframes rotate-border {
    to {
        transform: rotate(360deg);
    }
}
<div class="loading-box-container">
  <div class="loading-box">
    <p>Loading</p>
  </div>
</div>

An alternative: Using @property

There's a much more elegant solution using @property, but unfortunately it only works on Chrome. I'm including here in case one day it becomes more universally supported or support for other browsers isn't important for your use case.

The conic-gradient function has a parameter that allows you to specify at what angle the gradient starts. If we can animate just that parameter, perhaps using a CSS variable, then we can animate the border with just a single div and without actually rotating anything.

Unfortunately, without some hinting the browser doesn't know how to transition a CSS variable. Therefore, we use @property to indicate the variable is an angle, telling the browser how to transition it.

@property --rotation {
  syntax: '<angle>';
  initial-value: 0deg;
  inherits: false;
}

.loading-box {
  --size: 200px;
  --radius: 10px;
  position: relative;
  width: var(--size);
  height: var(--size);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  background: #000;
  border-radius: var(--radius);
  margin: var(--radius);
}

.loading-box::before {
  --rotation: 0deg;
  content: '';
  width: calc(100% + 2 * var(--radius));
  height: calc(100% + 2 * var(--radius));
  border-radius: var(--radius);
  position: absolute;
  top: calc(-1 * var(--radius)); left: calc(-1 * var(--radius));
  background: conic-gradient(from var(--rotation), #0000ff00, #ff0000ff);
  animation: rotate-border 5s linear infinite;
  z-index: -1;
}

@keyframes rotate-border {
    to {
        --rotation: 360deg;
    }
}
<div class="loading-box">
  <p>Loading</p>
</div>

CanIUse for @property indicates this will only work in Chrome and Edge as of this post.

Answer from Auroratide on Stack Overflow
🌐
Ibelick
ibelick.com › blog › create-animated-gradient-borders-with-css
Creating an animated gradient border with CSS
May 2, 2023 - .box { display: flex; justify-content: center; align-items: center; padding: 12px; height: 400px; width: 400px; border: 3px solid #0000; border-radius: 12px; background: linear-gradient(#131219, #131219) padding-box, linear-gradient( var(--angle), #070707, #687aff ) border-box; animation: 8s rotate linear infinite; } @keyframes rotate { to { --angle: 360deg; } } @property --angle { syntax: "<angle>"; initial-value: 0deg; inherits: false; } To achieve our desired effect, we'll use the following CSS properties:
🌐
CSS-Tricks
css-tricks.com › animating-a-css-gradient-border
Animating a CSS Gradient Border | CSS-Tricks
February 8, 2021 - div { --angle: 0deg; /* … */ border-image: linear-gradient(var(--angle), green, yellow) 1; animation: 10s rotate linear infinite; } @keyframes rotate { to { --angle: 360deg; } }
Discussions

Creating an animated gradient border with CSS
Looks amazing, we'll done OP. I'm certainly going to use this in my portfolio. More on reddit.com
🌐 r/reactjs
12
26
May 2, 2023
html - CSS animated gradient border on a DIV - Stack Overflow
I'm trying to create a loading DIV that has a border that looks like an indeterminate progress ring spinner. I'm pretty close based on one of the examples on https://css-tricks.com/gradient-borders... More on stackoverflow.com
🌐 stackoverflow.com
Moving Rainbow Border Around Div
You can get pretty close with a few pseudo elements and some clever gradients: https://codepen.io/Chester/full/QPoyjN You could also probably do this using some sort of animated svg path. More on reddit.com
🌐 r/css
20
125
April 28, 2019
How is this wavy effect created (between the gradient header and next section)?
I found the source: http://pukka.io/ It appears to be an SVG, but the css implementation of the moving waves by u/moc_tidder is pretty cool. Edit: /u/ not @ More on reddit.com
🌐 r/web_design
61
580
September 27, 2016
🌐
CodePen
codepen.io › mike-schultz › pen › NgQvGO
Animated CSS Gradient Border
@import url('https://fonts.googleapis.com/css?family=Raleway:200'); html, body { height: 100%; } body { display: flex; justify-content: center; align-items: center; height: 100%; background: #1D1F20; } #box { display: flex; align-items: center; justify-content: center; width: 400px; height: 200px; color: white; font-family: 'Raleway'; font-size: 2.5rem; } .gradient-border { --borderWidth: 3px; background: #1D1F20; position: relative; border-radius: var(--borderWidth); } .gradient-border:after { content: ''; position: absolute; top: calc(-1 * var(--borderWidth)); left: calc(-1 * var(--borderWid
🌐
CodeTV
codetv.dev › blog › animated-css-gradient-border
Animated CSS gradient borders (no JavaScript, no hacks)
Update the conic-gradient() to use the custom property and add the animation — but let’s start out the animation as paused. We only want to set the animation-play-state to running when the element is hovered.
🌐
Theowoo
theowoo.github.io › agbg
Animated Gradient Border Generator
Animated Gradient Border Generator · Examples: 0 1 2 3 4 5 · Background · Rectangle · Circle · Width · Height · Border Width · Border Radius · Two Tones
🌐
Reddit
reddit.com › r/reactjs › creating an animated gradient border with css
r/reactjs on Reddit: Creating an animated gradient border with CSS
May 2, 2023 -

Hey everyone! I just published a new article on creating an animated gradient border using CSS. I've also included a version for Tailwind CSS users (with React and TypeScript too!).

I'm planning on writing more articles about design, UI, software, and other topics in the future. If you have any suggestions, feedback, or any cool ways you've used this effect in your projects please feel free to share! I'd love to hear your thoughts.

https://www.julienthibeaut.xyz/blog/create-animated-gradient-borders-with-css

🌐
Bejamas
bejamas.com › hub › guides › css-animated-gradient-border
Finding the best way to create animated gradient borders in CSS - Bejamas
November 18, 2024 - .card { isolation: isolate; } ... black); mask-clip: content-box, border-box; mask-composite: exclude; } We can use CSS Houdini to animate a gradient border....
Find elsewhere
Top answer
1 of 2
2

I like your original idea with using overflow: hidden, but to make it work I had to include an extra wrapper div.

  • The outer wrapper defines a padding which serves as the display area for the gradient border
  • The inner div is just the content box with a black background

.loading-box-container {
  --size: 200px;
  --radius: 10px;
  position: relative;
  width: var(--size);
  height: var(--size);
  padding: var(--radius);
  border-radius: var(--radius);
  overflow: hidden;
}

.loading-box {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  background: #000;
  border-radius: var(--radius);
}

.loading-box-container::before {
  content: '';
  width: 150%; /* The upscaling allows the box to fill its container even when rotated */
  height: 150%;
  position: absolute;
  top: -25%; left: -25%;
  background: conic-gradient(#0000ff00, #ff0000ff);
  animation: rotate-border 5s linear infinite;
}

@keyframes rotate-border {
    to {
        transform: rotate(360deg);
    }
}
<div class="loading-box-container">
  <div class="loading-box">
    <p>Loading</p>
  </div>
</div>

An alternative: Using @property

There's a much more elegant solution using @property, but unfortunately it only works on Chrome. I'm including here in case one day it becomes more universally supported or support for other browsers isn't important for your use case.

The conic-gradient function has a parameter that allows you to specify at what angle the gradient starts. If we can animate just that parameter, perhaps using a CSS variable, then we can animate the border with just a single div and without actually rotating anything.

Unfortunately, without some hinting the browser doesn't know how to transition a CSS variable. Therefore, we use @property to indicate the variable is an angle, telling the browser how to transition it.

@property --rotation {
  syntax: '<angle>';
  initial-value: 0deg;
  inherits: false;
}

.loading-box {
  --size: 200px;
  --radius: 10px;
  position: relative;
  width: var(--size);
  height: var(--size);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  background: #000;
  border-radius: var(--radius);
  margin: var(--radius);
}

.loading-box::before {
  --rotation: 0deg;
  content: '';
  width: calc(100% + 2 * var(--radius));
  height: calc(100% + 2 * var(--radius));
  border-radius: var(--radius);
  position: absolute;
  top: calc(-1 * var(--radius)); left: calc(-1 * var(--radius));
  background: conic-gradient(from var(--rotation), #0000ff00, #ff0000ff);
  animation: rotate-border 5s linear infinite;
  z-index: -1;
}

@keyframes rotate-border {
    to {
        --rotation: 360deg;
    }
}
<div class="loading-box">
  <p>Loading</p>
</div>

CanIUse for @property indicates this will only work in Chrome and Edge as of this post.

2 of 2
0

Hi is this what you are looking for?

What I did was I added a new div which will be the "mask" as well as a container div for both the mask and the loadingBox.

I then sized the mask to be a little larger than your visible area, make it a transparent background, and then gave it a large outline the same color as your background to effectively mask out a border. I then fiddled with z-indexs of the mask, the loadingbox and the before. I also added some actual borders on mask to box it out into a nice shape.

Take a look:

.gradient-box {
  
  display: flex;
  align-items: center;
  width: 90%;
  margin: auto;
  max-width: 22em;

  position: relative;
  padding: 30% 2em;
  box-sizing: border-box;

  border: 5px solid blue;
  color: #FFF;
  background: #000;
  background-clip: padding-box; /* !importanté */
  border: solid 5px transparent; /* !importanté */
  border-radius: 1em;

}

.gradient-box:before {
    content: '';
    position: absolute;
    top: 0; right: 0; bottom: 0; left: 0;
    z-index: -3;
    margin: -35px; /* !importanté */
    border-radius: inherit; /* !importanté */
    background: conic-gradient(#0000ff00, #ff0000ff);
    -webkit-animation: rotate-border 5s linear infinite;
    -moz-animation: rotate-border 5s linear infinite;
    -o-animation: rotate-border 5s linear infinite;
    animation: rotate-border 3s linear infinite;
}

@keyframes rotate-border {
    to {
        transform: rotate(360deg);
    }
}

html { height: 100%; background: #000; display: flex; }
body { margin: auto; }

.mask {
  position: absolute;
   box-sizing: border-box;
background-color: transparent; 
 outline: 65px solid black;
height: 100%;
  width: 100%;
  border: 2px solid black;
  border-left: 7px solid black;
  border-right: 7px solid black;
  z-index: -1;
}

.container {
  position: relative;
}
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Loading DIV Test</title>
</head>
<body>
<div class="container">
<div class="mask"></div>
<div id="loadingBox" class="gradient-box">
<p>Loading.</p>
</div>
  </div>
</body>

🌐
Bram.us
bram.us › 2021 › 01 › 29 › animating-a-css-gradient-border
Animating a CSS Gradient Border – Bram.us
While the effect here looks quite nice it won’t play nice with more than two colors. Take this rainbow animated gradient border for example: See the Pen CSS Rainbow Gradient Border (Animated, Attempt 1) by Bramus (@bramus) on CodePen.
🌐
Cruip
cruip.com › home › animated gradient borders with tailwind css
Animated Gradient Borders with Tailwind CSS - Cruip
March 12, 2024 - Note: this animation is currently only supported in Chrome and Chromium-based browsers. Firefox does not yet support the CSS @property rule, but it is expected to be integrated soon, as it is already available in the nightly development version. <div class="w-full max-w-[422px] [background:linear-gradient(45deg,#172033,theme(colors.slate.800)_50%,#172033)_padding-box,conic-gradient(from_var(--border-angle),theme(colors.slate.600/.48)_80%,_theme(colors.indigo.500)_86%,_theme(colors.indigo.300)_90%,_theme(colors.indigo.500)_94%,_theme(colors.slate.600/.48))_border-box] rounded-2xl border border-
🌐
FreeFrontend
freefrontend.com › css-border-animations
57 CSS Border Animations
1 week ago - A modern CSS border animation featuring a unique zigzag effect created with rotated square containers and linear gradients.
🌐
Daily Dev Tips
daily-dev-tips.com › posts › animating-a-gradient-border-in-css
Animating a gradient border in CSS
December 2, 2021 - 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, which should look like this. Pretty close already. Now we need to animate this! Let’s use some CSS variables to make the position animatable.
🌐
DEV Community
dev.to › dailydevtips1 › animating-a-gradient-border-in-css-1dho
Animating a gradient border in CSS - DEV Community
December 2, 2021 - 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.
🌐
CSS-Tricks
css-tricks.com › gradient-borders-in-css
Gradient Borders in CSS | CSS-Tricks
September 30, 2022 - Thanks for writing this! I used the first option recently when a student wanted to have a gradient border animated. Worked out pretty well: https://codepen.io/jupago/ Did not know about border-image-slice. Might also be fun to try clip-path. That 24ways article on Clip Paths got me wondering if that will eventually be the the next best thing.
🌐
YouTube
youtube.com › kevin powell
Create an animated, glowing, gradient border with CSS - YouTube
Inspired by a codepen by Gayane Gasparyan - https://codepen.io/gayane-gasparyan/pen/jOmaBQK - I took my own attempt to create a rotating gradient border, inc
Published   March 16, 2023
🌐
Medium
medium.com › @johnnyfekete › animated-gradient-border-in-css-80c32a69563f
Animated gradient border in CSS. Learn how you can enhance your UI with… | by Johnny Fekete | Medium
October 22, 2023 - CSS border properties don’t support background images, nor animation. However, by adding an :after pseudo-element and using `clip-path`, we can achieve the same effect as having the border.
🌐
Winterwind
winterwind.com › tutorials › css › 1
Animated Gradient Border
First, add a simple button with transparent border then add a linear-gradient background property to its 'after' pseudo element. Second, add an infinite ease animation to the button's 'after' pseudo element.
🌐
Webbae
webbae.net › posts › ultimate-guide-to-animated-border-gradients
Ultimate Guide to Animated Border Gradients | Web Bae
This is one of the main reasons I still really like the GSAP solution. However, for 1px borders, this works pretty damn well I gotta say! The animation is simple: we gradually increase the angle of the gradient using keyframes: ... And that’s it for the CSS approach!
🌐
CodePen
codepen.io › shshaw › pen › RwJwJJx
Animated Border Gradient
var(--main-bg) padding-box, // border-box extends this background to the border space var(--gradient-border) border-box, // Duplicate main background to fill in behind the gradient border. You can remove this if you want the border to extend "outside" the box background. var(--main-bg) border-box; background-position: center center; animation: bg-spin 3s linear infinite; @keyframes bg-spin { to { --border-angle: 1turn; } } &:hover { animation-play-state: paused; } } @property --border-angle { syntax: "<angle>"; inherits: true; initial-value: 0turn; }
🌐
Guissmo
guissmo.com › blog › animated-gradient-border-using-css
Making an animated gradient border using CSS | Jared Asuncion
In any case, we don’t have a pure CSS solution yet as of time of writing. So we add the following: To work around that we add a setInterval script. const startTime = Date.now(); setInterval(() => { const DURATION = 2000; const NEW_VALUE = `${((Date.now() - startTime) % DURATION) / DURATION}turn`; const COMPONENT = Array.from( document.getElementsByClassName("gradient-border") ).forEach((component) => { component.style.setProperty("--angle", NEW_VALUE); }); }, 20);