How to Create Border Animation using CSS

admin_img Posted By Bajarangi soft , Posted On 13-10-2020

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

Create Border Animation

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.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content=
            "width=device-width, initial-scale=1.0" />
    <title>BAJARANGI SOFT</title>
</head>

<body>
<div class="geeks">
    <h1>BAJARANGI SOFT</h1>
</div>
</body>

</html>
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.
body {
margin: 0;
padding: 0;
background: #e8530c;
}

.geeks {
left: 40%;
top: 40%;
position: absolute;
width: 300px;
text-align: center;
}

h1 {
position: relative;
}
    

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.
.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;
}
} 

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.
.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;
}
} 
Complete CSS 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> 
Complete Code: It is the combination of both HTML and CSS codes
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content=
            "width=device-width, initial-scale=1.0" />
    <title>BAJARANGI SOFT</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background: #ee2c0a;
        }

        .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>BAJARANGI SOFT</h1>
    <p>Place The Cursor Over The Text</p>
</div>
</body>

</html>

Related Post