skip to Main Content

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 ?

2

Answers


  1. You can try this border animation.

    body {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
    }
    
    *, *::before, *::after {
        box-sizing: border-box;
    }
    
    @keyframes rotate {
        100% {
            transform: rotate(1turn);
        }
    }
    
    .container {
        position: relative;
        z-index: 0;
        width: 300px;
        height: 300px;
        border-radius: 10px;
        overflow: hidden;
        padding: 2rem;
    }   
    .container::before {
            content: '';
            position: absolute;
            z-index: -2;
            left: -50%;
            top: -50%;
            width: 200%;
            height: 200%;
            background-color: blue;
            background-repeat: no-repeat;
            background-size: 50% 50%, 50% 50%;
            background-position: 0 0, 100% 0, 100% 100%, 0 100%;
            background-image: linear-gradient(red, maroon);
            animation: rotate 4s linear infinite;
    }
        
    .container::after {
            content: '';
            position: absolute;
            z-index: -1;
            left: 6px;
            top: 6px;
            width: calc(100% - 12px);
            height: calc(100% - 12px);
            background: white;
            border-radius: 5px;
    }
    <div class="container">Content Here</div>
    Login or Signup to reply.
  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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search