How to Animate Rainbow Heart from a Square using CSS ?
In this article, you will learn how to make a magical heart using only CSS. Animations in CSS are a widely fascinating part of CSS. We will be creating a heart that changes color every 3 seconds. We will be creating the animation in two steps.
1. Building the heart: In this step we will be building the shape of the heart.
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.
First we will create two divisions in the body, with the classes of square and container. Then, in style.css file, we will add some general body and class styling to make all the content centered. We will then create two circles using the :before and :after selectors and using the border-radius property for making it a circle.
This will create the shape of the heart that will be animated in the next step. The code below demonstrates the HTML and CSS code.
Note: As of now, two different colors are used for drawing the circles, so that you can understand better, but in the final program, we will take the same color for the two circles and square.
We can make circles of different colors so that they can be distinguished easily. If we make all the colors the same, the output would be as given below:
2. Animating the heart: In this step we add the animations of the heart.
We will use two sets of animation for the heart. One is the motion of the heart and the other is the changing of colors. We will be using keyframes for defining the animations. The colors of the heart will be defined in each of the keyframes as needed. This will toggle the colors of the heart throughout the course of the animation.
The motion of the heart when changing from a square to the heart can be added using the transform property in one of the keyframes. This will cause the transformation from a square to the heart shape.