Skip to main content

Write Elegant Animations in CSS with Dynamic and Composable Keyframes


Write Elegant Animations in CSS with Dynamic and Composable Keyframes

I struggled with bloated CSS animations for years, repeating keyframe blocks until my code felt like a tangled mess.

Here’s the usual way we write keyframe animations:

@keyframes fadeOut {
from {
opacity: 1;
}
to {
opacity: 0;
}
}

What happens if you skip the keyframe block entirely and lean on inherited CSS properties?

@keyframes fadeToTransparent {
to {
opacity: 0;
}
}

It works just as smoothly, with half the code. Less clutter, more control. I thought it was just a neat shortcut. Then I realized it lets you dynamically tweak animations using CSS custom properties, and my mind was blown.

Let’s dive into how this works, plus a bonus tip to make your animations pop.

Core Concept Explained

When you skip the from block in your keyframe animation, the starting values pull directly from the element’s existing context.

Let’s make it clear with a quick demo. Take a look:

@keyframes fadeToTransparent {
to {
opacity: 0;
}
}
.to-transparent {
animation: fadeToTransparent 1000ms forwards;
}
<div class="row">
<div class="ball to-transparent"></div>
<div class="ball to-transparent" style="opacity: 0.6"></div>
<div class="ball to-transparent" style="opacity: 0.3"></div>
</div>

Traditional keyframes lock you into fixed values. This tweak lets you override start states dynamically. When you drop the from part of a keyframe, the animation grabs the element’s current opacity and fades from that point. Mind blown. 🤯

It’s like the browser just knows where to begin. So slick. 😄

Reverse the Trick

This technique also works in reverse. By leaving out the to value, the animation transitions from a defined starting point to the element’s current value.

<style>
@keyframes fadeFromTransparent {
from {
opacity: 0;
}
}
  .ball {
animation: fadeFromTransparent 1000ms;
}
</style>
<div class="ball" style="opacity: 0.5;">

In above example, when an element‘s opacity, it defaults to 1, behaving like a standard fade-in animation.

Why use this? It’s perfect for elements with non-opaque defaults or state-driven opacity such as a modals. Check out this practical example:

<style>
@keyframes fadeFromTransparent {
from {
opacity: 0;
}
}
  .icon-btn {
opacity: 0.7;
animation: fadeFromTransparent 1000ms;
    &:hover, &:active, &:focus-visible {
opacity: 1;
}
}
</style>
<button class="icon-btn">...</button>

In above example the button changes it’s opacity from 0 to 0.7. When the user hovers over the button, opacity changes to 1.

Advanced Combo Technique

This advanced trick will blow your mind. It allows you to dynamically animate using a value from another keyframe.

<style>
@keyframes twinkle {
from {
opacity: 0.25;
}
to {
opacity: 0.75;
}
}
  @keyframes fadeFromTransparent {
from {
opacity: 0;
}
}
  .ball {
animation:
twinkle 250ms alternate infinite,
fadeFromTransparent 2000ms;
}
</style>
<div class="ball"></div>

Here’s how this works:

The twinkle keyframe animation makes the ball’s opacity change between 0.25 and 0.75. The animation here runs in cycle. The first cycle takes opacity from 0.25 to 0.75. Next cycle takes opacity from 0.75 to 0.25.

The animation starts at an opacity of 0 but leaves the target opacity undefined.
When combined together, first animation takes opacity from 0 to twinkle with both animation having different time duration.

This example proves that multiple animations can change the same property without one cancelling the other.

The Final Master Trick

This animation trick will blow your mind completely. When I discovered it, I was surprised to see it is even possible in CSS animation.

In above animation, we have four balls and all of them has different motion of animation.

To make our CSS animation dynamic, we can use CSS variable defined in the ball element itself. We can access the CSS variable in our keyframe animation itself to make it dynamic.

<style>
@keyframes oscillate {
from {
transform: translateX(calc(var(--amount) * -1));
}
to {
transform: translateX(var(--amount));
}
}
  .ball {
animation: oscillate 700ms ease-in-out alternate infinite;
}
</style>
<!-- Edit these values to change the oscillation amount: -->
<div class="ball" style="--amount: 8px"></div>
<div class="ball" style="--amount: 16px"></div>
<div class="ball" style="--amount: 32px"></div>
<div class="ball" style="--amount: 64px"></div>

Final Takeaway

These cool tricks allows you to write animation in CSS like a pro. Try it out in your next project and share your thoughts below.

Follow me for more cool CSS nuggets.

Thank you for being a part of the community


Comments

Popular posts from this blog

Exploring Google’s New Gemini CLI: The Ultimate Open-Source Dev Tool

  Google quietly released a local AI agent that builds apps, debugs code, parses your repo, and fetches real-time data, right inside your terminal. And it’s completely free. This year, the most revolutionary developer tools I’ve used didn’t come with a splashy launch or billion-dollar hype. It came as a simple CLI: Gemini CLI, a terminal-based AI agent built on top of Google’s Gemini 2.5 Pro model . At first glance, it looks like a lightweight alternative to Claude Code. But after just 10 minutes of use, it became clear: this isn’t just a convenient utility. It’s a powerful local AI development assistant that can analyze, automate, and accelerate almost every part of your software workflow. And best of all? It’s fully open-source under the Apache 2.0 license It gives you up to 1,000 free requests per day It integrates with your local filesystem, IDE, and the web And it runs entirely in your terminal , no browser needed In this guide, I’ll show you what Gemini CLI is, how it works...

React Native vs React JS — Key Difference, Advantages-Disadvantages, Limitations

  React Native vs React JS — Key Difference, Advantages-Disadvantages, Limitations React JS It is a JavaScript library that supports each face and server-side. It’s a popularly used library that focuses on developing user interfaces for mobile and internet-primarily based applications. React Native It is a cross-platform mobile framework that uses the ReactJS framework. It’s primarily used for developing native mobile applications like Windows, iOS and mechanical man. The major advantage provided by React Native is that it permits the developers to form mobile applications on varied platforms while not compromising the tip user’s expertise. Components of React JS Components of React Native Basic parts View — it is the essential building block of internet applications. Text — It helps to point out the text. The text element contains nesting, styling, and bit handling. Image — this is often a React element for showing multiple footages like network pictures and static resources. Text...

Top 25 JavaScript Array Methods Every Developer Should Learn

  You wrote some code. You ran it. And then your array went from a list of users to an angry collection of undefined , NaN And more bugs than a summer camping trip. Staring at map , filter , and reduce like they were ancient scrolls written in Elvish. Copy-pasting from Stack Overflow like a caffeinated zombie. Wondering why the heck splice just murdered half my data. But here’s the truth: mastering arrays is non-negotiable. If you’re fumbling with arrays, you’re fumbling with everything . Web apps, APIs, UIs — they all depend on your ability to tame this glorious beast. So buckle up. I’m about to drop 25 methods that will make you look at arrays like a surgeon looks at a scalpel. Edited by me 1. map() - Because Loops Are for Cavemen You want to transform every item in an array? Don’t go forEach your way to hell. map It is concise, expressive, and doesn’t mutate your data. It’s like therapy for arrays. const names = [ 'alice' , 'bob' , 'charlie' ]; const u...