Author -  Sai gowtham

How to Create a Codepen animated Button with CSS

In this tutorial, we are going to learn about creating a Codepen animated button with css.

Note: This is my third project from the #15daysofcss challenge.

Codepen Demo

Here is the demo for the Codepen animated button.

Getting started

First, let’s add the HTML markup.

<a href="https://reactgo.com" target="_blank" class="animated-button">
  <span>Start Coding</span>
</a>

In the above code, we have an anchor tag <a> with a class animated-button and a span tag.

The CSS styles

In the below code, we have used the linear-gradient() function to apply the 5 different colors to the border of our button.

.animated-button{
    font-size: 1rem;
    font-weight: 400;
    text-decoration:none;
    color: #ffff;
    cursor: pointer;
    background-image: linear-gradient(164deg,#c1b562,#b1b1ea,#bb46a7,#b4b783,orange);    border-radius: 3px;
    padding:14px 3px; /* it helps to increase border width*/
}

.animated-button span{
  background: black;
  color: #ffff;
  padding:11px 30px; /* it helps to increase button width and height*/
  height:100%;
}

.animated-button:hover{
  animation: bordermove .7s linear infinite
}

The Animation

Now, we need to animate the border whenever a user hovers on the button.

To do that, we need to change the linear-gradient colors at different percentages.

@keyframes bordermove{
  0%{
    background-image: linear-gradient(114deg,#c1b562,#b1b1ea,#bb46a7,#b4b783,orange);
  }

  25%{
     background-image: linear-gradient(124deg,orange,#b1b1ea,#c1b562,#b4b783,#bb46a7);
  }

  50%{
    background-image: linear-gradient(134deg,#b4b783,#bb46a7,orange,#c1b562,#b1b1ea);
  }

  75%{
    background-image: linear-gradient(144deg,#b4b783,#b1b1ea,orange,#bb46a7,#c1b562);
  }

  100%{
    background-image: linear-gradient(114deg,#c1b562,#b4b783,#b1b1ea,orange,#bb46a7);
  }
}

We have used the same five colors in the 0% to 100% but each level we have changed the position of the colors.

I hope you enjoyed ✓ and learned something new.

If you are interested in doing 15daysofcss challenge checkout 15daysofcss page.

Accessibility

If you want a button element with a link tag and at the same time to be accessible with tabs you can use the below HTML markup.

<button class="animated-button">
    <a href="https://reactgo.com" target="_blank" tab-index="-1">
      <span>Start Coding</span>
    </a>
</button>

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY