How to Create Border Animation using HTML/CSS ?

 Hello Guys,

      In this tutorial , we are create border animation using css. The css border animation using hover. Hover is used to create border animation when we hover over a text. we are going to use are before , after and hover selectors.

    Animation is to divide into two parts. The top and right will be done at one time using before selector , Bottom and left are done at one time using after selector. Animation looking like below picture :

    





HTML Code :

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

<head>
    <meta charset="UTF-8" />
        <title>Button Animation</title>
</head>

<body>
    <div class="button_animation">
        <h1>Animation</h1>
    </div>
</body>

</html>

 

CSS Code :

    
    body {
        margin0;
        padding0;
        background: dark;
    }

    .button_animation {
        left40%;
        top40%;
        positionabsolute;
        width300px;
        text-aligncenter;
    }

    h1 {
        positionrelative;
    }

    .button_animation::before {
        content"";
        positionabsolute;
        top-2px;
        left-2px;
        width0;
        height0;
        backgroundtransparent;
        border2px solid transparent;
    }

    .button_animation:hover::before {
        animation: animate 3s linear forwards;
    }

    @keyframes animate {
        0% {
            width0;
            height0;
            border-top-colorblack;
            border-right-colortransparent;
            border-bottom-colortransparent;
            border-left-colortransparent;
        }

        50% {
            width100%;
            height0;
            border-top-colorblack;
            border-right-colorblack;
            border-bottom-colortransparent;
            border-left-colortransparent;
        }

        100% {
            width100%;
            height100%;
            border-top-colorblack;
            border-right-colorblack;
            border-bottom-colortransparent;
            border-left-colortransparent;
        }
    }

    .button_animation::after {
        content"";
        positionabsolute;
        top-2px;
        left-2px;
        width0;
        height0;
        backgroundtransparent;
        border2px solid transparent;
    }

    .button_animation:hover::after {
        animation: animates 3s linear forwards;
    }

    @keyframes animates {
        0% {
            width0;
            height0;
            border-top-colortransparent;
            border-right-colortransparent;
            border-bottom-colortransparent;
            border-left-colorblack;
        }

        50% {
            width0;
            height100%;
            border-top-colortransparent;
            border-right-colortransparent;
            border-bottom-colorblack;
            border-left-colorblack;
        }

        100% {
            width100%;
            height100%;
            border-top-colortransparent;
            border-right-colortransparent;
            border-bottom-colorblack;
            border-left-colorblack;
        }
    }

    

Complete Code :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
        <title>Button Animation</title>

    <style>
        body {
            margin0;
            padding0;
            background: dark;
        }

        .button_animation {
            left40%;
            top40%;
            positionabsolute;
            width300px;
            text-aligncenter;
        }

        h1 {
            positionrelative;
        }

        .button_animation::before {
            content"";
            positionabsolute;
            top-2px;
            left-2px;
            width0;
            height0;
            backgroundtransparent;
            border2px solid transparent;
        }

        .button_animation:hover::before {
            animation: animate 3s linear forwards;
        }

        @keyframes animate {
            0% {
                width0;
                height0;
                border-top-colorblack;
                border-right-colortransparent;
                border-bottom-colortransparent;
                border-left-colortransparent;
            }

            50% {
                width100%;
                height0;
                border-top-colorblack;
                border-right-colorblack;
                border-bottom-colortransparent;
                border-left-colortransparent;
            }

            100% {
                width100%;
                height100%;
                border-top-colorblack;
                border-right-colorblack;
                border-bottom-colortransparent;
                border-left-colortransparent;
            }
        }

        .button_animation::after {
            content"";
            positionabsolute;
            top-2px;
            left-2px;
            width0;
            height0;
            backgroundtransparent;
            border2px solid transparent;
        }

        .button_animation:hover::after {
            animation: animates 3s linear forwards;
        }

        @keyframes animates {
            0% {
                width0;
                height0;
                border-top-colortransparent;
                border-right-colortransparent;
                border-bottom-colortransparent;
                border-left-colorblack;
            }

            50% {
                width0;
                height100%;
                border-top-colortransparent;
                border-right-colortransparent;
                border-bottom-colorblack;
                border-left-colorblack;
            }

            100% {
                width100%;
                height100%;
                border-top-colortransparent;
                border-right-colortransparent;
                border-bottom-colorblack;
                border-left-colorblack;
            }
        }
    </style>
</head>

<body>
    <div class="button_animation">
        <h1>Animation</h1>
    </div>
</body>

</html>


Output :



If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support.  🙏













Post a Comment

0 Comments