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

Sharpen Your Front-End Skills: Quick HTML, CSS & React Interview Challenges

  The source of this image is Chat GPT based on writing! Are you preparing for front-end developer interviews and looking for practical, hands-on ways to improve your HTML, CSS, and React skills? Whether you’re a beginner aiming to build confidence or an experienced developer brushing up on UI skills, small, targeted challenges can make a huge difference. In this article, I’ll walk you through some of the best free and low-cost resources that offer real-world front-end tasks — perfect for interview prep, portfolio building, and daily practice. 1. Frontend Mentor frontendmentor.io Frontend Mentor is one of the most popular platforms for hands-on HTML, CSS, and JavaScript challenges. You get beautifully designed templates (in Figma or image formats) and are asked to bring them to life using clean code. The platform offers difficulty levels ranging from newbie to expert, and it’s perfect for practicing responsiveness and semantic HTML. Bonus : You can even filter for React-based ...

Difference Between Three.js and Babylon.js: What Actually Should You Choose?

You don’t have to be just a graphic designer to create interactive designs. You can be a coder and still create visually appealing and eye-catching games. All thanks to JavaScript. The first cross-browser JavaScript library–three.js–that can create 3D computer graphics was first released on 24 April 2010 by Ricardo Cabello. He first wrote the code in ActionScript language, which was then used by Adobe Flash. But then in 2009, he ported the code to JavaScript. Previously, people used WebGL. But the problem was its limitation: it can create only simple pointers and lines. Ricardo, instead of abandoning WebGL as something that is futile, used it to his own advantage. He built three.js on top of WebGL. This renders three.js to create 3D graphics in the browser. Even a 3D scene can be created easily using Canvas and WebGL now. But then in 2013, Babylon.js was created. But why? Why did its creators, Microsoft and David Catuhe, make something that another JavaScript library–three.js –was alre...

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...