DEV Community

Cover image for The Ultimate Guide: Master CSS @keyframes for Stunning Animations (No Bullsh*t)
Werliton Silva
Werliton Silva

Posted on

The Ultimate Guide: Master CSS @keyframes for Stunning Animations (No Bullsh*t)

You know that feeling of "I've never needed this before"? Well, that's exactly how I felt about @keyframes in CSS. For years, whenever a website needed an animation, the solution was always the same: rush to some third-party JavaScript library. These were ready-made solutions, packed with features, doing the heavy lifting for me. Why bother with pure CSS when jQuery or some animation library could solve everything with a single line of code? The truth is, I didn't know how and, by not having the practical need, I kept postponing learning it.

animation


The Day It Clicked: I Needed to Create My Own Animations

Then one day, reality knocked. I was on a project where performance was crucial, and every imported byte of JavaScript made a difference. Plus, the animations I needed were very specific, and generic libraries simply couldn't deliver the control or fluidity I was looking for. That's when I realized: I needed to create my own CSS animations.

I started, of course, with my old mindset: "It must be easy, it's just CSS." And the result? Frustration. The animations were choppy, strange, lifeless. I messed up the timing, the transitions, everything. It was a festival of jumps and "hiccups" that made me want to go back to my good old plugin. It was clear I didn't understand the logic behind motion in CSS.


The Turning Point: Understanding @keyframes and the animation Property

My big breakthrough was realizing that it wasn't just about copying and pasting code. I needed to understand the how and the why. My approach to fixing these errors was very direct, no detours or complicated theories:

  1. The ABC of @keyframes: from and to: I started with the absolute basics. Understanding that @keyframes is like a movie script: you define what happens at the beginning (from) and what happens at the end (to).
@keyframes myFirstAnimation {
  from {
    opacity: 0; /* Starts invisible */
  }
  to {
    opacity: 1; /* Ends visible */
  }
}
Enter fullscreen mode Exit fullscreen mode

This was the starting point. Simple, but fundamental.

  1. The Power of Percentages: After grasping from and to, the big revelation was using percentages. This gave me total control! I thought, "Oh, so I can say what the element does at 25%, at 50%, at 75% of the animation?" Bingo! It was like unlocking a new level of control.
@keyframes wave {
  0% { transform: translateX(0); } /* Starts at normal position */
  50% { transform: translateX(50px); } /* Moves right */
  100% { transform: translateX(0); } /* Returns to normal position */
}
Enter fullscreen mode Exit fullscreen mode

With this, animations came to life, with intermediate steps and smoother transitions.

  1. Connecting Everything with animation: There's no point in creating a beautiful @keyframes if you don't "link" it to an element. That's where the animation property came in. I needed to say: "Hey, div, use myFirstAnimation and make it last 3 seconds!"
.my-element {
  animation-name: wave; /* Which animation to use */
  animation-duration: 2s; /* How long it lasts */
  animation-iteration-count: infinite; /* Repeats forever */
}
Enter fullscreen mode Exit fullscreen mode

This was the icing on the cake. By combining the script (@keyframes) with the execution (animation), things finally made sense.


Where the Magic Happens: Shorthand and Fine-Tuning

Once I understood the basics, I started exploring the other animation properties and the famous shorthand. Instead of writing line by line, I could group everything. It seemed like secret code at first, but with practice, it became my best friend.

/* Before */
.element {
  animation-name: fadein;
  animation-duration: 1s;
  animation-timing-function: ease-in-out;
  animation-delay: 0.5s;
}

/* After (Shorthand) */
.element {
  animation: fadein 1s ease-in-out 0.5s forwards;
}
Enter fullscreen mode Exit fullscreen mode

And the fine-tuning? animation-timing-function (for the animation's rhythm) and animation-fill-mode (for what happens after the animation ends) were the cherries on top. They transformed robotic animations into something truly fluid and professional. My tip here was to focus on experimenting: change ease to linear, see what happened with forwards and backwards. It was all trial and error, but with the basic understanding, errors became learning opportunities.


My Realization: Native CSS Animations

Today, 99% of my animations are done with pure CSS. I control every detail, and best of all: the site is lighter, loads faster, and the user experience is much better.

If I learned it, you can too! The secret isn't to memorize a thousand properties, but to understand the logic:

  • @keyframes is the script for your animation.
  • animation is the engine that executes that script on the element.

My experience taught me that sometimes, the "harder" path initially (learning pure CSS) becomes the most efficient and rewarding in the long run.

Show me the code. Enjoy!

enjoy

Top comments (2)

Collapse
 
dariomannu profile image
Dario Mannu

| the "harder" path initially becomes the most efficient and rewarding in the long run.

Absolutely. Since these are web standards, they're not going away like most libraries around.

Collapse
 
werliton profile image
Werliton Silva

Yep. I agree. thanks