Animate border-color property with CSS

The CSS border-color property can be animated to create dynamic color transitions on element borders. This animation effect is useful for hover states, attention-grabbing elements, or interactive UI components.

Syntax

selector {
    animation: animation-name duration timing-function iteration-count;
}

@keyframes animation-name {
    percentage {
        border-color: color-value;
    }
}

Example: Basic Border Color Animation

The following example animates the border color from gray to red and back −

<!DOCTYPE html>
<html>
<head>
<style>
    .animated-border {
        width: 300px;
        height: 150px;
        background-color: #f0f0f0;
        border: 5px solid gray;
        animation: colorChange 2s infinite alternate;
        display: flex;
        align-items: center;
        justify-content: center;
        font-family: Arial, sans-serif;
        margin: 20px auto;
    }
    
    @keyframes colorChange {
        0% {
            border-color: gray;
        }
        50% {
            border-color: red;
        }
        100% {
            border-color: blue;
        }
    }
</style>
</head>
<body>
    <h2>Border Color Animation</h2>
    <div class="animated-border">Animated Border</div>
</body>
</html>
A rectangular box with centered text appears, and its border color smoothly transitions from gray to red to blue and back repeatedly.

Example: Multiple Border Colors

This example demonstrates animating through multiple colors with different timing −

<!DOCTYPE html>
<html>
<head>
<style>
    .rainbow-border {
        width: 250px;
        height: 100px;
        background-color: white;
        border: 8px solid red;
        animation: rainbowBorder 3s infinite linear;
        margin: 30px auto;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: bold;
        color: #333;
    }
    
    @keyframes rainbowBorder {
        0% { border-color: red; }
        16.66% { border-color: orange; }
        33.33% { border-color: yellow; }
        50% { border-color: green; }
        66.66% { border-color: blue; }
        83.33% { border-color: indigo; }
        100% { border-color: violet; }
    }
</style>
</head>
<body>
    <h2>Rainbow Border Animation</h2>
    <div class="rainbow-border">Rainbow Effect</div>
</body>
</html>
A rounded rectangular box with bold text displays a continuously cycling rainbow border effect, smoothly transitioning through red, orange, yellow, green, blue, indigo, and violet colors.

Conclusion

Animating the border-color property creates engaging visual effects for web elements. Use keyframes to define color transitions and control timing with animation properties for smooth, eye-catching border animations.

Updated on: 2026-03-15T13:25:10+05:30

907 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements