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.
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.
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.
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.
Complete CSS Code:
Complete Code: It is the combination of both HTML and CSS codes.