Unlocking the Power of CSS Keyframes
Tags: webdev, css
Published: 2023-03-25 02:41:14


CSS animations are a game-changer for web developers, enabling us to create engaging and interactive websites. Keyframes play a significant role in CSS animations, allowing for the creation of complex, multi-stage animations with precise control over each step. In this article we will dig into the world of keyframes, discuss their benefits, and present some inspiring examples to fuel your next project.

&nbsp;
## What is CSS Keyframes?

Keyframes are used in CSS to create animations, they define the various stages of the animation. They give you control over the styling of an element at specific points during the animation, allowing for smooth transitions between styles. Think of keyframes as a way to tell the browser: “Hey, I want this element to change its style from this point (A) to that point (B) over a certain period of time.” You can also add multiple points, which creates the possibility to make more complex animations.

To create a keyframe animation, you’ll define a set of keyframes using the `@keyframes` rule, and then apply that animation to an element using the `animation` property.

&nbsp;
## Benefits of Using Keyframes
<ul>
	<li><b>Fine-grained control:</b> Keyframes provide precise control over the timing and styles applied at each stage of an animation, enabling you to create complex and realistic animations.</li>
	<li><b>Reusability:</b> You can apply the same set of keyframes to multiple elements or use them in combination with other keyframe animations, making your CSS code more modular and maintainable.</li>
	<li><b>Browser compatibility:</b> Keyframe animations are widely supported across modern browsers, making them a reliable choice for creating animations on the web as indicated in Figure 1 below.</li>
	<li><b>Performance:</b> CSS animations, including keyframes are GPU-accelerated resulting in smoother animations and reduced CPU usage compared to JavaScript-based animations.</li>
</ul>


![Figure 1 - keyframes browser compatibility](https://cdn-images-1.medium.com/max/1600/1*oI9ZYFktFQ6qaKBYma1wQA.png)
<small>_Figure 1 - keyframes browser compatibility_</small>

&nbsp;
## What We All Really Came for: Examples
#### ⦾ Loading Spinner

A classic use case for keyframes is creating a simple loading spinner. By animating the rotation of a spinner element, you can create a smooth, infinite spinning effect. Perfect for those slow API calls 😆

```css
@keyframes spinner {  
  0% {  
    transform: rotate(0deg);  
  }  
  100% {  
    transform: rotate(360deg);  
  }  
}  
  
.spinner {  
  width: 50px;
  height: 50px;
  border: 3px solid #fff;
  border-radius: 50%;
  border-top-color: #F6A23C;
  animation: spinner 2s linear infinite;
}
```
{% codepen https://codepen.io/marcialc/pen/zYJyqbo %}

&nbsp; 
#### ⦾ Typewriter Effect

With keyframes, you can create a typewriter effect that reveals text one character at a time, simulating the look of a typewriter. By animating the width of a hidden text container, you can create a captivating effect for your website’s headings or call-to-action text. 


```css
@keyframes typewriter {  
  0% {  
    width: 0;  
  }  
  100% {  
    width: 100%;  
  }  
} 

@keyframes blink-cursor {
  from,
  to {
    border-color: transparent;
  }
  50% {
    border-color: #A68DBA;
  }
} 
  
.typewriter-text {  
  overflow: hidden;  
  border-right: 0.15em solid;  
  white-space: nowrap;  
  animation: typewriter 3s steps(30, end), blink-cursor 0.5s step-end infinite; 
}
```
Let's break down the animation here since it looks longer than usual. There is actually two separate animations that run simultaneously on the same element 😲
<ul>
<h4>First animation: <u>typewriter 3s steps(30, end)<u></h4>
<li><b>typewriter</b>: the keyframes animation that will be applied to the element for the typing effect</li>
<li><b>3s</b>: duration of the typewriter animation</li>
<li><b>steps(30, end)</b>: timing function that controls the pacing of the animation. In this case, the animation is divided into 30 steps, with the last step at the very end.</li>
<h4>Second animation: <u>blink-cursor 0.5s step-end infinite<u></h4>
<li><b>blink-cursor</b>: the keyframes animation that will be applied to the element for the blinking cursor effect</li>
<li><b>0.5s</b>: duration of the blink-cursor animation</li>
<li><b>step-end</b>: timing function that causes an immediate jump to the final state of the animation. It is effectively the same as steps(1, end)—the animation has only one step, and that step is at the very end of the animation</li>
<li><b>infinite</b>: iteration count, which specifies how many times the animation should run. In this case, the animation will run infinitely.</li>
</ul>

{% codepen https://codepen.io/marcialc/pen/XWPoyeR %}

&nbsp;
#### ⦾ Bouncing Ball

Keyframes can also be used to create fun animations, like a bouncing ball. By combining translations and scaling, you can create a realistic bouncing effect that adds a playful touch to your website.

```css
@keyframes bounce {  
  0%, 100% {  
    transform: translateY(0) scaleY(1);  
  }  
  50% {  
    transform: translateY(-50px) scaleY(1.2);  
  }  
}  
  
.bouncing-ball {  
  animation: bounce 1s ease-in-out infinite;  
}
```
Let's breakdown these keyframes ... I promise we are almost done!
<ul>
<li><b>0%, 100%</b>: These are the keyframes of the animation. The % values represent the progress of the animation. In this case, the animation starts and ends at 0% and 100%, respectively.</li>
<li><b>transform: translateY(0) scaleY(1)</b>:  This is the CSS transform property that specifies how the element should be transformed at the keyframes. In this case, the element is translated vertically by 0 pixels and scaled in the Y direction by 1 (i.e., no scaling).</li>
<li><b>50%</b>: This is the midpoint of the animation</li>
<li><b>transform: translateY(-50px) scaleY(1.2)</b>: This is the CSS transform property at the midpoint of the animation. In this case, the element is translated vertically by -50 pixels (i.e., upwards) and scaled in the Y direction by 1.2 (i.e., slightly larger than its original size).</li>

* Adding the 0.2 to the scaling really gives it that bouncing effect otherwise it would just be moving up and down... optical illusions can be cool :-)
</ul>

{% codepen https://codepen.io/marcialc/pen/PodXxXY %}

&nbsp;
Voila, there you have it!

CSS keyframes are like your secret weapon to make super cool animations for your website. Once you get the hang of their advantages and play around with these examples, you'll see how keyframes can level up your site's user experience. So, don't hold back—dive into the world of keyframes and watch your web designs come alive! 👾