Exploring Reusability in CSS
I'm now focusing on ensuring the animation library is truly reusable. I'm prioritizing the use of vanilla CSS keyframes and variables to achieve this. Avoiding complex JavaScript dependencies is key. I'm currently figuring out how to structure the library for maximum flexibility and adaptability. The goal is easy integration with different projects.
Prioritizing Configuration Flexibility
Now, I'm focusing on the configuration aspect. My goal is to maximize flexibility using only CSS. This includes variables for duration, delay, easing, fill mode, and distance. I'm exploring a hybrid approach for naming conventions: a base class for common properties and modifier classes for individual animations. This combination appears to be the most flexible and vanilla.
Constructing Animation Building Blocks
I've been drafting the fundamental code structure, starting with a base setup that defines key animation variables like duration, delay, and easing, using CSS custom properties. This forms the foundation for applying consistent and controllable animation styles. I'm now diving into the development of specific keyframes, beginning with fades and slides, implementing transform to control movement distance via a new variable. This iterative process is crucial for crafting versatile animation components.
Designing Animation Components
I'm now focusing on specific animation components, starting with zoom-in and attention-grabbing effects like pulse and shake. The key is to make these components variable-driven; I'm using CSS custom properties for scale and translateX within keyframes. My goal is to craft a truly reusable and configurable library, with var() enabling runtime adjustments. The library aims for simplicity and control, and I'm currently prioritizing practical utilities.
Refining and Structuring
I've been working on refining the code further. I'm now implementing utility classes like .anim-fast and .anim-infinite to override base animation speeds. I've also defined the core animation components: fade, slide, zoom, and bounce. My current focus is structuring the library output into distinct parts: CSS Core (variables and base classes), the Keyframes section, the utilities section, and a practical usage example. During code drafting, I realized I needed to namespace the classes with .anim. Also, I've created stagger utilities to avoid needing JavaScript.
Concluding and Documenting
I'm now putting the final touches on the CSS library. I'm focusing on defining easing variables for flexibility, and making sure that I have included accessibility checks for prefers-reduced-motion. The goal is now to write documentation. I'll include a clear introduction, the complete CSS code, practical HTML examples, and instructions on how to customize various aspects. The goal here is clarity in every aspect. I've confirmed I've met the core constraints: vanilla CSS, keyframes, heavy use of variables, and true reusability across projects. I'm organizing the CSS into root variables, base classes, utilities, and keyframes (fades, slides, zooms, and flips). The media query for accessibility is included in the plan.
Here is a complete, lightweight, and fully reusable animation library built with vanilla CSS.
It relies on CSS Custom Properties (Variables) to allow you to configure timing, distance, and delays without writing new keyframes every time.
1. The CSS Library (animations.css)
Copy this into your stylesheet. It is broken down into Defaults, Utilities, and Smart Keyframes.
/* =========================================
1. CORE SETTINGS & DEFAULTS
========================================= */
:root {
/* Timing */
--anim-duration: 0.6s;
--anim-delay: 0ms;
--anim-ease: cubic-bezier(0.25, 0.46, 0.45, 0.94); /* ease-out-quad */
--anim-fill: both;
/* Physics / Geometry defaults */
--anim-distance: 30px; /* How far elements slide */
--anim-scale-start: 0.8; /* Starting size for zooms */
--anim-rotate: 15deg; /* Rotation angle */
}
/* =========================================
2. BASE CLASS
========================================= */
[class*="anim-"] {
animation-duration: var(--anim-duration);
animation-delay: var(--anim-delay);
animation-timing-function: var(--anim-ease);
animation-fill-mode: var(--anim-fill);
will-change: transform, opacity; /* Performance optimization */
}
/* Accessibility: Turn off animations not essential */
@media (prefers-reduced-motion: reduce) {
[class*="anim-"] {
animation-duration: 1ms !important;
transition-duration: 1ms !important;
animation-iteration-count: 1 !important;
animation-delay: 0ms !important;
}
}
/* =========================================
3. UTILITIES (Overrides)
========================================= */
/* Speed Modifiers */
.anim-fast { --anim-duration: 0.3s; }
.anim-slow { --anim-duration: 1.2s; }
.anim-snail { --anim-duration: 2s; }
/* Delay Modifiers */
.anim-delay-1 { --anim-delay: 100ms; }
.anim-delay-2 { --anim-delay: 200ms; }
.anim-delay-3 { --anim-delay: 300ms; }
.anim-delay-5 { --anim-delay: 500ms; }
.anim-delay-10{ --anim-delay: 1000ms; }
/* Loop */
.anim-infinite { animation-iteration-count: infinite; }
/* Directions (for slides) */
.anim-far { --anim-distance: 100px; }
.anim-near { --anim-distance: 10px; }
/* =========================================
4. KEYFRAMES & CLASSES
========================================= */
/* --- FADES --- */
.anim-fade-in { animation-name: fade-in; }
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
.anim-fade-out { animation-name: fade-out; }
@keyframes fade-out {
from { opacity: 1; }
to { opacity: 0; }
}
/* --- SLIDES (Uses --anim-distance) --- */
.anim-slide-up { animation-name: slide-up; }
@keyframes slide-up {
from { opacity: 0; transform: translate3d(0, var(--anim-distance), 0); }
to { opacity: 1; transform: translate3d(0, 0, 0); }
}
.anim-slide-down { animation-name: slide-down; }
@keyframes slide-down {
from { opacity: 0; transform: translate3d(0, calc(var(--anim-distance) * -1), 0); }
to { opacity: 1; transform: translate3d(0, 0, 0); }
}
.anim-slide-left { animation-name: slide-left; }
@keyframes slide-left {
from { opacity: 0; transform: translate3d(var(--anim-distance), 0, 0); }
to { opacity: 1; transform: translate3d(0, 0, 0); }
}
.anim-slide-right { animation-name: slide-right; }
@keyframes slide-right {
from { opacity: 0; transform: translate3d(calc(var(--anim-distance) * -1), 0, 0); }
to { opacity: 1; transform: translate3d(0, 0, 0); }
}
/* --- ZOOMS (Uses --anim-scale-start) --- */
.anim-zoom-in { animation-name: zoom-in; }
@keyframes zoom-in {
from { opacity: 0; transform: scale(var(--anim-scale-start)); }
to { opacity: 1; transform: scale(1); }
}
.anim-zoom-out { animation-name: zoom-out; }
@keyframes zoom-out {
from { opacity: 1; transform: scale(1); }
to { opacity: 0; transform: scale(var(--anim-scale-start)); }
}
.anim-pop {
animation-name: pop;
--anim-ease: cubic-bezier(0.175, 0.885, 0.32, 1.275); /* Bouncy */
}
@keyframes pop {
from { opacity: 0; transform: scale(0.5); }
to { opacity: 1; transform: scale(1); }
}
/* --- ATTENTION --- */
.anim-pulse { animation-name: pulse; }
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
.anim-shake { animation-name: shake; }
@keyframes shake {
0%, 100% { transform: translateX(0); }
10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
20%, 40%, 60%, 80% { transform: translateX(5px); }
}
.anim-float { animation-name: float; }
@keyframes float {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-10px); }
}
2. How to Use It
Apply the base class and any modifiers to your HTML elements.
Basic Usage
<!-- Simple Fade In -->
<div class="anim-fade-in">I fade in</div>
<!-- Slide Up -->
<div class="anim-slide-up">I slide up</div>
Using Utility Modifiers
You can stack classes to change speed and delay without creating new CSS.
<!-- Start after 0.5s, run fast -->
<div class="anim-slide-right anim-delay-5 anim-fast">
Fast Delayed Slide
</div>
<!-- Infinite Float effect -->
<div class="anim-float anim-infinite anim-slow">
I am floating
</div>
Inline Customization (The Power of Variables)
Because the keyframes utilize variables (like var(--anim-distance)), you can change the physics of the animation inline in your HTML style attribute or in a specific CSS class.
<!-- Slide up from 100px away instead of the default 30px -->
<div class="anim-slide-up" style="--anim-distance: 100px;">
Big entrance
</div>
<!-- Zoom in from 1.5x scale (Zoom In effect) -->
<div class="anim-zoom-in" style="--anim-scale-start: 1.5;">
Shrink in
</div>
<!-- Non-standard duration -->
<div class="anim-fade-in" style="--anim-duration: 3s;">
Very slow fade
</div>
3. How to Stagger Lists (Grid/Flex)
You can create a staggered effect for lists without JavaScript by overriding the delay variable in your CSS on child elements.
/* Add this to your project CSS */
.list-item {
/* Determine animation */
animation-name: slide-up;
/* Enforce base animation properties if you aren't using the HTML classes */
animation-duration: var(--anim-duration, 0.5s);
animation-fill-mode: both;
}
/* Pro Tip: Stagger loop */
.list-item:nth-child(1) { --anim-delay: 100ms; }
.list-item:nth-child(2) { --anim-delay: 200ms; }
.list-item:nth-child(3) { --anim-delay: 300ms; }
.list-item:nth-child(4) { --anim-delay: 400ms; }
Why this approach is better than Animate.css?
- Smaller Size: You aren't importing hundreds of animations you won't use.
- Dynamic: In standard libraries,
slideInUp is hardcoded to move from -100%. In this library, slide-up moves based on --anim-distance. You can change it from 20px to 50vh just by changing one variable.
- Maintainable: You control the global speed (
--anim-duration) in one place (:root). If your client wants "everything snappier," you change 0.6s to 0.3s in one line.