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

CSS only Click-handlers You Might not be using, but you should

  You’re building a simple website, a good-looking landing page with a “See More” button. Instinctively, you reach for JavaScript to handle the button click event. But wait — what if I told you that CSS alone could do the job? Yes. CSS is often underestimated, but it can handle click interactions without JavaScript. In this guide, you’ll learn how to create CSS-only click handlers using the :target pseudo-class, and explore scenarios where this approach makes perfect sense. The :target Pseudo-Class CSS offers several pseudo-classes that let you style elements based on different states ( :hover , :focus , :checked ). But there’s one you might not have used before —  :target . The :target pseudo-class applies styles to an element when its ID matches the fragment identifier in the URL (the part after # ). This behavior is commonly seen when clicking an anchor link that jumps to a section on the same page. Here’s a simple example : <a href="#contact">Go to Contact</...

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

6 Essential JavaScript Concepts Every Developer Should Understand

It’s the only language I’ve used where [] == ![] it's true and where you can, typeof null and somehow get 'object' . But despite all its quirks (and there are many), there are a few core concepts that make life with JS not just easier, but saner. This isn’t some computer science flex. These are practical concepts that, once you understand them, make you write better, cleaner, and less buggy code. 1. Hoisting  Before you rage at your variables being undefined , understand this: JS hoists variable and function declarations to the top of their scope. But —  and this is important  —  only the declarations , not the assignments. Why? Because JS reads it like: This is also why let and const behave differently — they’re hoisted too, but live in the “Temporal Dead Zone” until declared. 2. Closures Closures are like little memory vaults for your functions. They allow functions to remember variables from the scope they were created in, even after that scope has gone. Why care? T...