DEV Community

Paramanantham Harrison
Paramanantham Harrison

Posted on • Originally published at learnwithparam.com on

5 1

Heart beat animation using CSS

In the first part, we learned how to draw heart shape using CSS3

Now lets see, how to animate it to create a heart beat animation.

These are the steps to create the animation

  • Create a keyframe animation for heart beat
  • scale the heart to bigger and smaller size using transform property at different intervals.
  • Create a infinite animation on the container using the created keyframe.

Creating the CSS keyframes for animating heart

Inorder to give the heart throbbing effect, we scale the heart at different intervals. Its upto us to play around the intervals to get the desired effect.

Keep in mind, while drawing the heart shape, we have rotated the container with 45deg. We need to keep that in our keyframe transform, else the shape with be tilted during animation

@keyframes animateHeart {
  // scale down and scale up faster in irregular intervals to get the throbbing effect
  0% {
    transform: rotate(45deg) scale(0.8);
  }
  5% {
    transform: rotate(45deg) scale(0.9);
  }
  10% {
    transform: rotate(45deg) scale(0.8);
  }
  15% {
    transform: rotate(45deg) scale(1);
  }
  50% {
    transform: rotate(45deg) scale(0.8);
  }
  100% {
    transform: rotate(45deg) scale(0.8);
  }
}
Enter fullscreen mode Exit fullscreen mode

Add the keyframes to the Heart container

Here animation property accepts three values,

.heart {
  transform: rotate(45deg);
  // animation: <animation-name> <animation-duration> <animation-iteration-count>
  animation: animateHeart 1.2s infinite;
}
Enter fullscreen mode Exit fullscreen mode
  1. keyframes which we defined earlier animateHeart is the animation-name
  2. animation-duration defines how long do we need to run each animation. If we define 2s, then each time it runs for 2s
  3. animation-iteration-count will run that many times. If we define it as infinite, then it will run the animation infinitely after every X seconds which is defined in the animation-duration column

Checkout the codepen for complete code along with the heart shape.

Neon image

Build better on Postgres with AI-Assisted Development Practices

Compare top AI coding tools like Cursor and Windsurf with Neon's database integration. Generate synthetic data and manage databases with natural language.

Read more →

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay