Originally created in 2015 on valentines day (February 14, mark it in your calendar) for my girlfriend. Since this years Valentine’s day is around the corner I thought it would be nice to kick off this blog with the very same animation. I know, it’s not the quality posts I am planning on posting here. But for now, it will do. Since i don’t want to just recycle the code, i also make this a sort of tutorial.

The animation consists of 4 hearts, one that is stationary, and 3 that animate.

See the Pen Be my Valentine heart, beating with css3 animations by Skippy (@Skippy) on CodePen.dark

For anyone not familiair with codepen, it is a online playground for HTML, CSS and JavaScript. It has tons of options, and even supports Emmet and preprocessors such as Less, Sass/SCSS and more. You can edit my pens on codepen too! Just fork my pen and it will be saved to your account. Now you have a personal copy and can make changes, or break the code 😉


The HTML is not much, it has some containers that makes positioning the heart easier. There are some elements i want to point out. While the HTML in itself is not that exciting the placement of the elements is a key part of the animation.

Icons and click me

As you can see, i have used a span element with the class “ico”. Inside of it there is another icon, I have named this ico2. These 2 spans, are the hearts you see popping out. There are more than just 2 hearts on screen, that is where the pseudo elements come in to play. I’ll explain more on that in the css section.

The hidden part

This part is hidden, unless the user clicked the heart. It will be animated via css, more on this later on.


This is where all the magic happens, I’ll take snippets of the css and go through them one by one. You can view the complete code on codepen.


The whole animations would have been a lot less exciting without a good font. I have chosen for a playful swirly style of Pacifico. It is available for free on Google Fonts, I’ll make a separate post about that later.

Backgrounds, subtle patterns and bright colors

I wanted the page to be bright, but the heart had to pop out, other than the animation of course :). But I didn’t wanted the page to be a plain color. So i searched for a pattern that was not to busy and subtle enough that it wouldn’t draw attention. I found a noisy one on Subtle Patterns. The class “bgoverlay” is a absolute positioned div that overlays the whole page, the background color is set to a bright purple, but i have lowered the alpha channel to 70%. This makes the pattern even more subtle and this way I can colorize it without re-hosting the image.

The Trick

Now, how do we know that the user has clicked? JavaScript! I know we can do this without JavaScript or even the bloated library of jQuery to do this. But why spend a lot of time on something fun that has no real use case outside to aw your girl. In the section titles JavaScript i’ll explain more on the code to do this but for now we focus on the css.

As you can see, I have 1 element. The class “ico” is defined twice, but once the class “open” is prepended. This is the toggle class that JavaScript has set for us, it will let the css know when to load the next part of the animation. In this case, it opens the heart. The class “open” has been set to a parent element.

I will not go through all the css and animations, it’s kind of straight forward but I will point out some little details that will make your life easier.

2 animations defined, 3 variations

Here we have the pseudo elements, it’s a simple trick to have less HTML markup in your scripts. They have some limitations, so be careful when using them. Here I hooked up the animations, they are defined elsewhere in the css file. In the css, I have left some comments to make it clear what lines I am talking about and what they mean. Note: this is not how you set up the pseudo elements. Look in the css where they are first defined. Just make sure to have a “content: ‘ ‘;” attribute. Otherwise they won’t show up.

I have 2 hearts that use the same animation, but I have used a different duration value. This creates an alternating effect. The first ico declared, uses a different animation that explodes the heart much larger and runs for 4 seconds.

Defining animations in css

In the previous part we referenced some css animations, but we have to actually define them in the css to make them work. The great thing about codepen is that you can set a auto prefixer. On codepen you can click on the “view compiled css” button. Shown here:

The animations are nearly the same, they just have different values. In the css animations you can use a simple “from” and “to” to have a beginning and an end. If you need more steps, you can use percentages. Start at 0 and make as many steps as needed until you reach 100%.

The transform function uses the scale modifier, it starts at scale set to 1. This is the starting position and initial scale of the heart. We scale it up by 60%, this is done by using the value 1.6. Since 1 is it’s starting position, it gets a scaling of 0.6. We also fade the exploding heart out, this is done by lowering the opacity value to 0.

The hidden part (css)

As promised, the snippet of the hidden text. Do you see the value for “top”? It’s hidden out of view, also the opacity is set to 0.

The silly part is, this is not even the best way to do this. But to stay true to the original concept I will explain why I did this. And why you shouldn’t do this.

First I declare the endtext class. I set the position and such, this is all good.
Next, I declare the open state, the state the page is in when the user has clicked on the heart and the JavaScript has fired off. Here is where it all goes wrong, i set the opacity and the top value. But i also set an animation.

In the animation, I essentially do the same thing over again. I set the top value, and the opacity. The starting point is how i declared it in the class, and the ending point is how I set the other class when the heart would open. This is redundant and unnecessary.

It would have been easier to do this, but this was made years ago and I was just getting the hang of animation outside of jQuery.

Here i use an transition with the selector set to all and with a duration of 5s.

JavaScript (jquery)

This part we can go through real quick. What it loosely says, if the page is loaded, and the user clicked on the element with the class “title”. I will add a class called “open” the the element with the class “container”.

The same goes for close, but it just removes the class.




0 thoughts on “Beating heart, be my valentine”

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Tips and Tricks

Google Fonts, an easy and free way to spice up your project

Today, we are going to talk about google fonts. As promised in an earlier post. “The web is the best place for text. Unlike a printed artifact, text at a URL can be searched, copied, Read more...


Animate SVG – How to animate a path in SVG

Last month I’ve posted the first part of the SVG mini series, it teaches you how to create a path that is valid for animation. Today I am going to follow up on this post, Read more...


Reachable mobile menu, what to look out for

On mobile devices the screen estate is small, and it has to be as the mobile screens are not that big (see image below). Common screen sizes are around the 360 pixels in width, this does Read more...