How to Create Border Animation using CSS ?

The CSS border animation using hover is used to create border animation when we hover over a text. The concepts that we are going to use are before , after and hover selectors. It is highly recommend to go through all these selectors before moving any further in this article.

Approach: The approach of this animation is to divide the animation into two parts. The top and right will be done at one time using before, bottom and left are done at one time using after selector.

HTML Code: We have created HTML file and create a div in it with h1 inside div. Below is the code for the same.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0" />
    <title>GeeksforGeeks</title>
</head>
  
<body>
    <div class="geeks">
        <h1>GeeksforGeeks</h1>
    </div>
</body>
  
</html>

chevron_right


CSS Code: The CSS for this animation is a little bit tricky so let’s try to understand it step by step.



Step 1: The first thing we have done is provide basic background and align our text at center. Below is the code for the same.

filter_none

edit
close

play_arrow

link
brightness_4
code

body {
    margin: 0;
    padding: 0;
    background: green;
}
  
.geeks {
    left: 40%;
    top: 40%;
    position: absolute;
    width: 300px;
    text-align: center;
}
  
h1 {
    position: relative;
}

chevron_right


Step 2: The second step is creating top and right border.

  • The first thing is to create a border with a transparent background.
  • Then animate it over hover giving it a linear animation and an identifier name as animate.
  • Now using keyframes we will animate the border. Make sure to apply color to only the top and right side of the border. First, we have increased the width for top border animation and the height for right border animation.
filter_none

edit
close

play_arrow

link
brightness_4
code

.geeks::before {
    content: "";
    position: absolute;
    top: -2px;
    left: -2px;
    width: 0;
    height: 0;
    background: transparent;
    border: 2px solid transparent;
}
 
.geeks:hover::before {
    animation: animate 1s linear forwards;
}
 
@keyframes animate {
    0% {
        width: 0;
        height: 0;
        border-top-color: black;
        border-right-color: transparent;
        border-bottom-color: transparent;
        border-left-color: transparent;
    }
 
    50% {
        width: 100%;
        height: 0;
        border-top-color: black;
        border-right-color: black;
        border-bottom-color: transparent;
        border-left-color: transparent;
    }
 
    100% {
        width: 100%;
        height: 100%;
        border-top-color: black;
        border-right-color: black;
        border-bottom-color: transparent;
        border-left-color: transparent;
    }
}

chevron_right


Step 3: Repeat the step-2 with after selector. Some key points to remember during this step are:

  • Make sure to make top and right transparent and left and bottom as coloured.
  • For left the height will increase and for bottom width will increase.
  • Make sure to use a different name for keyframes identifier in this step.
filter_none

edit
close

play_arrow

link
brightness_4
code

.geeks::after {
    content: "";
    position: absolute;
    top: -2px;
    left: -2px;
    width: 0;
    height: 0;
    background: transparent;
    border: 2px solid transparent;
}
 
.geeks:hover::after {
    animation: animates 1s linear forwards;
}
 
@keyframes animates {
    0% {
        width: 0;
        height: 0;
        border-top-color: transparent;
        border-right-color: transparent;
        border-bottom-color: transparent;
        border-left-color: black;
    }
 
    50% {
        width: 0;
        height: 100%;
        border-top-color: transparent;
        border-right-color: transparent;
        border-bottom-color: black;
        border-left-color: black;
    }
 
    100% {
        width: 100%;
        height: 100%;
        border-top-color: transparent;
        border-right-color: transparent;
        border-bottom-color: black;
        border-left-color: black;
    }
}

chevron_right


Complete CSS Code:

filter_none

edit
close

play_arrow

link
brightness_4
code

<style>
    body {
        margin: 0;
        padding: 0;
        background: green;
    }
  
    .geeks {
        left: 40%;
        top: 40%;
        position: absolute;
  
        width: 300px;
        text-align: center;
    }
  
    h1 {
        position: relative;
    }
  
    .geeks::before {
        content: "";
        position: absolute;
        top: -2px;
        left: -2px;
        width: 0;
        height: 0;
        background: transparent;
        border: 2px solid transparent;
    }
  
    .geeks:hover::before {
        animation: animate 1s linear forwards;
    }
  
    @keyframes animate {
        0% {
            width: 0;
            height: 0;
            border-top-color: black;
            border-right-color: transparent;
            border-bottom-color: transparent;
            border-left-color: transparent;
        }
  
        50% {
            width: 100%;
            height: 0;
            border-top-color: black;
            border-right-color: black;
            border-bottom-color: transparent;
            border-left-color: transparent;
        }
  
        100% {
            width: 100%;
            height: 100%;
            border-top-color: black;
            border-right-color: black;
            border-bottom-color: transparent;
            border-left-color: transparent;
        }
    }
  
    .geeks::after {
        content: "";
        position: absolute;
        top: -2px;
        left: -2px;
        width: 0;
        height: 0;
        background: transparent;
        border: 2px solid transparent;
    }
  
    .geeks:hover::after {
        animation: animates 1s linear forwards;
    }
  
    @keyframes animates {
        0% {
            width: 0;
            height: 0;
            border-top-color: transparent;
            border-right-color: transparent;
            border-bottom-color: transparent;
            border-left-color: black;
        }
  
        50% {
            width: 0;
            height: 100%;
            border-top-color: transparent;
            border-right-color: transparent;
            border-bottom-color: black;
            border-left-color: black;
        }
  
        100% {
            width: 100%;
            height: 100%;
            border-top-color: transparent;
            border-right-color: transparent;
            border-bottom-color: black;
            border-left-color: black;
        }
    }
</style>

chevron_right


Complete Code: It is the combination of both HTML and CSS codes.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0" />
    <title>GeeksforGeeks</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background: green;
        }
  
        .geeks {
            left: 40%;
            top: 40%;
            position: absolute;
  
            width: 300px;
            text-align: center;
        }
  
        h1 {
            position: relative;
        }
  
        .geeks::before {
            content: "";
            position: absolute;
            top: -2px;
            left: -2px;
            width: 0;
            height: 0;
            background: transparent;
            border: 2px solid transparent;
        }
  
        .geeks:hover::before {
            animation: animate 1s linear forwards;
        }
  
        @keyframes animate {
            0% {
                width: 0;
                height: 0;
                border-top-color: black;
                border-right-color: transparent;
                border-bottom-color: transparent;
                border-left-color: transparent;
            }
  
            50% {
                width: 100%;
                height: 0;
                border-top-color: black;
                border-right-color: black;
                border-bottom-color: transparent;
                border-left-color: transparent;
            }
  
            100% {
                width: 100%;
                height: 100%;
                border-top-color: black;
                border-right-color: black;
                border-bottom-color: transparent;
                border-left-color: transparent;
            }
        }
  
        .geeks::after {
            content: "";
            position: absolute;
            top: -2px;
            left: -2px;
            width: 0;
            height: 0;
            background: transparent;
            border: 2px solid transparent;
        }
  
        .geeks:hover::after {
            animation: animates 1s linear forwards;
        }
  
        @keyframes animates {
            0% {
                width: 0;
                height: 0;
                border-top-color: transparent;
                border-right-color: transparent;
                border-bottom-color: transparent;
                border-left-color: black;
            }
  
            50% {
                width: 0;
                height: 100%;
                border-top-color: transparent;
                border-right-color: transparent;
                border-bottom-color: black;
                border-left-color: black;
            }
  
            100% {
                width: 100%;
                height: 100%;
                border-top-color: transparent;
                border-right-color: transparent;
                border-bottom-color: black;
                border-left-color: black;
            }
        }
    </style>
</head>
  
<body>
    <div class="geeks">
        <h1>GeeksforGeeks</h1>
    </div>
</body>
  
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.