CSS Pulse Animation

By Marko Denic
Contents
  1. HTML element
  2. Circle
  3. Animation
  4. Performance tip
  5. Reusable Example with CSS Variables

In this article I will show you how to create a nice pulsing effect. I will also cover the use cases and also how to make it more performant.

Let’s start!

HTML element

First, we need one HTML element. Let’s give it a class .pulse. It can be an empty div as we don’t need any content here, at least not for this basic example.

html
<div class="pulse"></div>

Circle

Now, we need some styles to make this element look like a circle.

css
.pulse {
    background: rgb(222, 84, 72);
    border-radius: 50%;
    height: 30px;
    width: 30px;
}

Animation

Next, we will create this animation utilizing box-shadow and transform properties.

The box-shadow property adds a shadow around elements. The transform property lets you rotate, scale, skew, or translate an element.

css
.pulse {
    background: rgb(222, 84, 72);
    border-radius: 50%;
    height: 30px;
    width: 30px;
    box-shadow: 0 0 0 0 rgba(222, 84, 72, 1);
    transform: scale(1);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% {
        transform: scale(0.95);
        box-shadow: 0 0 0 0 rgba(222, 84, 72, 0.7);
    }

    70% {
        transform: scale(1);
        box-shadow: 0 0 0 15px rgba(222, 84, 72, 0);
    }

    100% {
        transform: scale(0.95);
        box-shadow: 0 0 0 0 rgba(222, 84, 72, 0);
    }
}

Performance tip

To help the browser optimize the animation, you can add the will-change property to your element:

css
.pulse {
    will-change: transform, box-shadow;
}

This hints to the browser that these properties are going to change, allowing it to prepare the necessary optimizations in advance. Use it only where needed though, as overusing will-change can actually hurt performance by consuming extra memory. If you animate from JavaScript instead, use requestAnimationFrame so every update lines up with the screen refresh.

Reusable Example with CSS Variables

This will already work, but let’s make it a more real-life example by making it a bit more dynamic.

For this purpose, I will rewrite the previous demo and use CSS variables to make it reusable. I will create 4 elements with different colors.

Let’s see what it looks like:

This is it, but I’ll provide one more example for you. Let’s create a “play” button you can use to create a CTA if you have some videos on your website.

In this example, we’ll need a bit more HTML and CSS, but I promise it’ll be fun. 🙂

Further reading: CSS Pulse Animation.

Using Tailwind? See how Tailwind animate-pulse works and how to write it in plain CSS.

Prefer to tweak it visually? Use the CSS Pulse Animation Generator and copy the code.

For a subtler way to highlight a button, try an animated gradient border.