Creating Border Animation using CSS


CSS is used to create beautiful and engaging border animations, which add movement and interest to a web page. To create border animation, we will first need to define a border for the element we want to animate, then we’ll use CSS transitions and animations to add movement to the border.

Border animations can be used to create hover effects on buttons, links, and other interactive elements. They can also be used to create loading animations that show progress while a page or element is loading. We can also use border animations on call-to-action buttons to make them more noticeable.

Approach - 1

we will create a hover effect that animates the border of an element when a user hovers over it.

Algorithm

  • Create an HTML document and define the title as "Hover Effect Border Animation".

  • Set up the document's body using a flexbox to center the box and give it a background color of #48b6ff.

  • Define a box class with inline-block display, padding of 10px, font size of 18px, the color of #333, and a 2px solid transparent border that transitions for 0.5s with ease.

  • Add a pulsing animation to the border with an infinite duration and ease-in-out timing.

  • When the box has hovered, change the border to a gradient from red to green to blue and disable the pulsing animation.

  • Define the pulsing animation with a keyframe that changes the border color from red to green to blue.

  • Add a div element with the box class to the body of the HTML document.

  • Save and view the HTML file in a web browser to see the hover effect border animation.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Hover Effect Border Animation</title>
   <style>
      /* Set up the body with flexbox to center the box */
      body {
         display: flex;
         justify-content: center;
         align-items: center;
         flex-direction: column;
         background-color: #48b6ff;
         min-height: 100vh;
      }
      /* Style the box with a transparent border */
      .box {
         display: inline-block;
         padding: 10px;
         font-size: 18px;
         color: #333;
         border: 2px solid transparent;
         transition: border 0.5s ease;
         /* Add the pulsing animation to the border */
         animation: border-pulse 2s ease-in-out infinite;
      }
      /* When the box is hovered, change the border to a gradient and disable the pulsing animation */
      .box:hover {
         border-image: linear-gradient(to right, #f00, #0f0, #00f);
         border-image-slice: 1;
         animation: none;
      }
      /* Define the pulsing animation */
      @keyframes border-pulse {
         0% {
            border-color: #f00;
         }
         50% {
            border-color: #0f0;
         }
         100% {
            border-color: #00f;
         }
      }
   </style>
</head>
<body>
   <!-- Add the box element to the HTML -->
   <div class="box">
      Hover over me
   </div>
</body>
</html>

Approach - 2

Here, we will create a loading animation by animating the border of the loading icon.

Algorithm

  • Declare the document type as HTML using the <!DOCTYPE html> declaration.

  • Start the HTML document by opening the <html> tag.

  • Add a <head> tag inside the <html> tag.

  • Inside the <head> tag, add a <title> tag and specify the title of the document as "Loading Border Animation".

  • Add a <style> tag inside the <head> tag to add styling to the document.

  • Inside the <style> tag, add CSS rules to style the <body> element, including centering the loading animation and setting the background color.

  • Add a CSS rule to style the loading animation by setting its size, shape, border color, and animation properties.

  • Create an animation called "spin" using the @keyframes rule.

  • Add the transform rule to rotate the element 360 degrees.

  • Inside the <body> tag, add an <div> element with a class of "loading" to display the loading animation.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Loading Border Animation</title>
   <style>
      /* Styling the body element to center the loading animation */
      body{
         display: flex;
         justify-content: center;
         align-items: center;
         flex-direction: column;
         min-height: 100vh;
         background-color: rgb(253, 114, 114);
      }

      /* Styling the loading element */
      .loading {
         width: 50px;
         height: 50px;
         border: 5px solid #ccc;
         border-top-color: #3498db; /* Changing the top border color */
         border-radius: 50%; /* Making the border round */
         animation: spin 1s linear infinite; /* Adding animation to spin continuously */
         margin: 50px auto; /* Centering the element with margin */
      }

      /* Defining the animation */
      @keyframes spin {
         to {
            transform: rotate(360deg); /* Rotating the element 360 degrees */
         }
      }
   </style>
</head>
<body>
   <div class="loading"></div> <!-- The loading element -->
</body>
</html>

Approach - 3

we will apply border animation to the call-to-action button using CSS.

Algorithm

  • Create a container and center it.

  • Style the element with a border that is initially transparent and a transition property that animates the border over 0.5 seconds.

  • Create a hover effect for the element that changes the border to a linear gradient of three colors: red, green, and blue.

  • Define a keyframe animation called "border-pulse" that changes the border color of the element over time.

  • Apply the "border-pulse" animation to the element initially.

  • When the element is hovered over, disable the "border-pulse" animation by setting it to "none".

Example

<!DOCTYPE html>
<html>
<head>
   <title>Call to Action Border Animation</title>
   <style>
      /* Set the background color and center content vertically */
      body {
         background-color: #48b6ff;
         font-family: Arial, sans-serif;
         display: flex;
         justify-content: center;
         align-items: center;
         flex-direction: column;
         min-height: 100vh;
      }
      /* Style the button */
      .cta-button {
         display: inline-block;
         position: relative;
         padding: 16px 32px;
         background-color: #ff4d4d;
         color: #fff;
         font-size: 24px;
         text-transform: uppercase;
         text-decoration: none;
         border: none;
         overflow: hidden;
         transition: all 0.4s ease-out;
      }
      /* Add a pseudo-element to create the border animation */
      .cta-button:before {
         content: "";
         display: block;
         position: absolute;
         top: 0;
         left: 50%;
         width: 0;
         height: 100%;
         background-color: #fff;
         transform: translateX(-50%);
         z-index: -1;
         transition: all 0.4s ease-out;
      }
      /* Change the background and text color on hover */
      .cta-button:hover {
         background-color: #fff;
         color: #ff4d4d;
      }
      /* Animate the pseudo-element to create the border animation */
      .cta-button:hover:before {
         width: 110%;
      }
      </style>
</head>
<body>
   <a href="#" class="cta-button">Click Me</a>
</body>
</html>

Conclusion

However, Border animations can sometimes cause performance issues, especially when used excessively or on large elements, leading to slow page load times and reduced overall performance. Some older web browsers may not support certain animation techniques.

We can also create border animations using SVG graphics and JavaScript. They allow more complex animations and provide more control over the animation.

Updated on: 22-May-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements