Create an animated "rainbow" button with CSS
Let's discover in this article how to create a "rainbow" button that animates on mouse hover. A modern and sensational effect guaranteed!
Step 1
The HTML code.
<button>
Click me
</button>
Our HTML code consists of a simple button. We couldn't make it any simpler, could we?
Step 2
Let's style our button.
button {
padding: 20px 50px;
font-size: 1.5rem;
cursor: pointer;
border: 0px;
background: transparent;
position: relative;
transition: all .2s;
overflow: hidden;
color: #fff;
border-radius: 30px;
box-shadow: 0px -0px 0px 0px rgba(143, 64, 248, .5), 0px 0px 0px 0px rgba(39, 200, 255, .5);
}
In the previous step, we created a “rainbow” square. It is simply a 400px by 400px square that we offset to the side using the top
and left
properties to capture the most beautiful part of the gradient.
Step 3
Let's add a rainbow square.
button::after {
content: '';
width: 400px;
height: 400px;
position: absolute;
top: -50px;
left: -100px;
background-image: linear-gradient(225deg,
#27d86c 0%, #26caf8 50%, #c625d0 100%);
z-index: -1;
transition: all .5s;
}
In this step, we style the button::after
pseudo-element to create the rainbow square. This square will serve as the background for our button, and it will not overflow thanks to the overflow: hidden
property we added in step 2.
Step 4
Let's add hover effects.
button:hover::after {
transform: rotate(150deg);
}
button:hover {
transform: translate(0, -6px);
box-shadow: 10px -10px 25px 0px rgba(143, 64, 248, .25), -10px 10px 25px 0px rgba(39, 200, 255, .25);
}
Here, we simply rotate our rainbow square to change the gradient displayed in the button's background.
We also take the opportunity to raise it upwards using the transform
property and add a more significant shadow using the box-shadow
property.