0

I want the border animation to follow the border-radius of div. However, the animation doesn't follow the border radius.

.container {
  height: 150px;
  width: 150px;
  border: 1px solid black;
  border-radius: 20px;
  background: yellow;
  border-image: conic-gradient(from var(--angle), var(--c2), var(--c1) 0.1turn, var(--c1) 0.15turn, var(--c2) 0.25turn) 30;
  animation: borderRotate var(--d) linear infinite forwards;
}

@keyframes borderRotate {
  100% {
    --angle: 420deg;
  }
}

@property --angle {
  syntax: '<angle>';
  initial-value: 90deg;
  inherits: true;
}

 :root {
  --d: 2500ms;
  --angle: 90deg;
  --c1: red;
  --c2: blue;
}
<div class="container"></div>

How do i achieve the animation with border radius ?

hatched
  • 795
  • 2
  • 9
  • 34
  • use one method from the duplicate to create your gradient with radius then keep the same animation your are using – Temani Afif Jul 17 '23 at 09:04

1 Answers1

2

Is this what you are trying to achieve? Using pseudo elements, you offset the ::before element by 50% while also making it 200% of its original size. Then overlay an ::after pseudo element.

body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

@keyframes rotate {
  100% {
    transform: rotate(1turn);
  }
}

.container {
  position: relative;
  z-index: 0;
  width: 130px;
  height: 130px;
  border-radius: 20px;
  padding: 10px;
  overflow: hidden;
}

.container::before {
  content: '';
  position: absolute;
  z-index: -2;
  left: -50%;
  top: -50%;
  width: 200%;
  height: 200%;
  background-image: conic-gradient(red, yellow, green, blue, black);
  animation: rotate 4s linear infinite;
}

.container::after {
  content: '';
  position: absolute;
  z-index: -1;
  left: 5px;
  top: 5px;
  width: calc(100% - 10px);
  height: calc(100% - 10px);
  background: yellow;
  border-radius: 15px;
}
<div class="container"></div>
George Chond
  • 977
  • 1
  • 3
  • 12