DEV Community

Cover image for Valentine Colored Hearts
Chris Jarvis
Chris Jarvis

Posted on

Valentine Colored Hearts

Color Change

I wanted to change the color of a Valentines heart. The structure is same as in JavaScript heart from an earlier post. This post is focused on the CSS Animation.

This is a screenshot. I'm having issues trying to share the live version of this.

orange heart on red background. Should be a heart change from red to yellow to orange and back to red then repeating

/* Animate the colors */
.bigheart, .bigheart:before, .bigheart:after {
    animation: ColorChange 10s infinite;
}

@keyframes ColorChange {
    0% {
        background-color: var(--heart-red);
    }

    100% {
        background-color: var(--orange);
    }
}
Enter fullscreen mode Exit fullscreen mode

This looks good but it loops back to red abruptly.

Make it smoother by changing the orange and adding a 100% stop to the original color. Now it will change from red to orange and slowly back to red.

/* Animate the colors */
.bigheart, .bigheart:before, .bigheart:after {
    animation: ColorChange 10s infinite;
}

@keyframes ColorChange {
    0%, 100% {
        background-color: var(--heart-red);
    }

    50% {
        background-color: var(--orange);
    }
}
Enter fullscreen mode Exit fullscreen mode

Three stops

Adding a third color. So it will go red to yellow to orange and back to red.

@keyframes ColorChange {
    0%, 100% {
        background-color: var(--heart-red);
    }

    50% {
        background-color: var(--yellow);
    }

    66% {
        background-color: var(--orange);
    }
}



Enter fullscreen mode Exit fullscreen mode

Wrap-up

This was fun to play win animations and make the heart change colors. Getting the timing right too some work. at first it was too quick more distracting than enjoyable so I make the time longer.

Previous Posts

Top comments (0)