CSS Transitions provide a way to smoothly change a CSS property's value over a specified duration when its state changes. Think of them as simple "before and after" animations. Instead of a property instantly jumping from one value to another (like background-color: blue to background-color: red), a transition allows you to define how long and in what manner that change should occur. You specify the transition-property (e.g., all, color, transform), transition-duration (how long the animation takes), transition-timing-function (the acceleration curve, like ease-in or linear), and transition-delay (before it starts). Common triggers include pseudo-classes like :hover, :focus, or when JavaScript adds/removes a class. Transitions are perfect for enhancing user interaction, providing visual feedback for button presses, navigation hovers, or form element states.
For more complex, multi-step, or continuous motion, CSS Keyframe Animations are your tool. Unlike transitions which are limited to a single start and end state change, keyframe animations let you define specific styles at various points (keyframes) throughout an animation sequence. You declare an animation with the @keyframes rule, naming it, and then define its states using percentages (e.g., 0%, 50%, 100%, or from and to). Once defined, you apply the animation to an element using properties like animation-name, animation-duration, animation-iteration-count (e.g., infinite), animation-direction (e.g., alternate), and animation-fill-mode. Keyframe animations are ideal for creating loading spinners, parallax effects, attention-grabbing pulses, or complex onboarding sequences that run independently of user interaction.
While both transitions and keyframe animations add motion to your web elements, they serve distinct purposes. Transitions are generally simpler to implement for direct user interaction or state changes, offering a straightforward way to animate between two styles. Keyframe animations provide a much higher degree of control, enabling you to choreograph intricate sequences, loop indefinitely, and dictate exact styles at multiple points. As a frontend developer, knowing when to use each is key: transitions for responsive feedback, and keyframes for rich, predefined, or continuous motion. You might even combine them—a keyframe animation for a subtle loading indicator, which itself has a transition applied when it appears or disappears.
Key Takeaways
- Transitions smooth property changes between two states (e.g., on hover).
- Keyframe animations define multi-step sequences for complex or continuous motion.
- Transitions are simpler, often triggered by direct user interaction or state changes.
- Keyframe animations offer more control, allowing for looping, delays, and detailed choreography.
- Choose transitions for responsive feedback; choose keyframes for elaborate or autonomous animations.
Code Example
How this code works
This CSS code showcases two fundamental ways to add dynamic motion to elements: CSS transitions and keyframe animations. The first section, focusing on .box-transition, creates smooth visual effects tied to changes in an element's state, such as when a user hovers their mouse over it. The transition property on .box-transition declares that specific properties, like background-color and transform, should change over 0.3s with an ease timing. When the element is hovered, as defined by .box-transition:hover, its background-color becomes salmon and it slightly enlarges with transform: scale(1.1). A key point for beginners is placing the transition property on the base .box-transition class, not just :hover, which ensures the smooth animation plays both when hovering on and off.
The second part demonstrates continuous animation using @keyframes with .box-animation. The @keyframes wiggle rule outlines a series of transform changes over the animation's duration, starting and ending at rotate(0deg) and reaching rotate(5deg) at 25% and rotate(-5deg) at 75%, creating a gentle rocking motion. The animation property on .box-animation then applies this wiggle sequence, setting its duration to 0.8s, making it repeat infinitely, run in alternate directions each cycle, and use an ease-in-out timing function for its speed.