cssanimationgradientgradientstop

How do I animate a CSS gradient stop with a smooth transition to transparent?


I have the code bellow. How do I transition the gradient stop smoothly? Its just abruptly changing from one to another.

Most examples of gradient animations I have seem use the gradient position, but I believe changing the gradient stop should be possible too.

.test {
    display: inline-block;
    width: 300px;
    height: 300px;
    border-radius: 100%;
    background: conic-gradient(red, red);
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center center;
    background-size: auto;
    -webkit-mask:radial-gradient(circle, transparent 50%, white calc(50% + 1px));

    animation:
        rotate
        4.5s
        ease-out
        0s
        infinite
        none
        running;
}

@keyframes rotate {
    0% {
        background-image: conic-gradient(red, red);
    }

    30% {
        background-image: conic-gradient(red 70%, transparent);
    }

    70% {
        background-image: conic-gradient(red 30%, transparent, transparent, transparent);
    }

    100% {
        background-image: conic-gradient(red, transparent);
    }
}
<div class="test"></div>


Solution

  • This as of Dec 03 2020 only works on Chrome or Edge 95+

    One can animate the gradient using CSS @property.

    @property --opacity {
      syntax: '<percentage>';
      initial-value: 100%;
      inherits: false;
    }
    
    .test {
        display: inline-block;
        position: absolute;
        border-radius: 100%;
        background-image: conic-gradient(
            red var(--opacity),
            red 10%,
            rgba(255, 0, 0, var(--opacity)),
            transparent,
            transparent
        );
        will-change: transform, background-image;
        width: 200px;
        height: 200px;
        mask:radial-gradient(circle, transparent 47%, white calc(47% + 1px));
        -webkit-mask:radial-gradient(circle, transparent 47%, white calc(47% + 1px));
    
        animation:
            conic-gradient
                4.5s
                ease-out
                0s
                infinite
                none
                running;
    }
    
    @keyframes conic-gradient {
        50% {
            --opacity: 0%;
        }
    
        85% {
            --opacity: 100%;
        }
    }
    <div class="test"></div>