CSS animations and keyframes for WordPress themes
Fade-ins for cards, a loading spinner, a pulsing “New” badge: CSS animations handle these without JavaScript or animation libraries. A @keyframes rule describes the steps, the animation property applies them to an element. The rules for doing it well are short: animate only cheap properties, keep it subtle, and respect visitors who asked their system for less motion.
Problem: A theme needs an entrance animation for post cards, a spinner for AJAX loading and a hover effect for buttons, smooth on phones and without a JavaScript library.
Solution: Define the motion with @keyframes, apply it with animation, animate only transform and opacity, use transition for simple state changes like hover, and switch animations off in @media (prefers-reduced-motion: reduce).
An entrance animation
@keyframes ha-fade-in-up {
from {
opacity: 0;
transform: translateY(1.5rem);
}
to {
opacity: 1;
transform: none;
}
}
.post-card {
animation: ha-fade-in-up 0.5s ease-out both;
}
/* Small delays make a row of cards appear one after another */
.post-card:nth-child(2) { animation-delay: 0.08s; }
.post-card:nth-child(3) { animation-delay: 0.16s; }
both (the fill mode) applies the first keyframe during the delay and keeps the last one at the end, so cards do not flash visible before their animation starts.
A spinner
@keyframes ha-spin {
to { transform: rotate(360deg); }
}
.ha-spinner {
width: 2rem;
height: 2rem;
border: 3px solid rgba(0, 0, 0, 0.15);
border-top-color: currentColor;
border-radius: 50%;
animation: ha-spin 0.8s linear infinite;
}
Show it while a request runs (element.hidden = false) and add role="status" with a hidden text such as “Loading” on the container, so screen readers announce what is happening.
Hover: transition instead of animation
.button {
transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.button:hover,
.button:focus-visible {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
A transition animates between two states and is simpler than keyframes for hover and focus. Give keyboard users the same effect with :focus-visible.
Cheap and expensive properties
transform and opacity can be animated by the browser’s compositor without recalculating the layout, so they stay smooth on weak phones. Animating width, height, top, margin or box-shadow on many elements forces layout or paint on every frame and makes scrolling stutter. Move things with translate, grow them with scale.
Reduced motion
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
Some people get dizzy or nauseous from motion and switch on “reduce motion” in their operating system. This rule shortens all animations to an instant for them, while the end state (visible cards, a working spinner replaced by its final frame) stays the same.
NOTE: Entrance animations on content above the fold delay what the visitor sees and can make the Largest Contentful Paint later. Animate secondary elements, not the main heading or the hero image, and keep durations under half a second.