Create smooth, performant animations and transitions using CSS β from simple hover effects to complex multi-step keyframe animations and 3D transforms.
Background
CSS animations make interfaces feel alive and responsive. Done well, they guide user attention, provide feedback, and delight users. Done poorly, they cause motion sickness and slow pages. This lab teaches both the technique and the taste.
π‘ transition animates CSS property changes. transition: all 0.3s ease catches everything but is inefficient β name specific properties. Only transform and opacity are GPU-accelerated and won't cause layout reflow. Prefer those for performance.
πΈ Verified Output:
Step 2: CSS Transforms
Write this file:
π‘ Transforms don't affect layout β they move/scale the painted element without pushing other elements around. This makes them perfect for animations. Multiple transforms combine left-to-right: transform: translateY(-5px) scale(1.1) translates first, then scales.
πΈ Verified Output:
Step 3: @keyframes Animations
Write this file:
π‘ @keyframes defines the animation sequence. Use from/to for simple start-end or 0%, 50%, 100% for multi-step. Staggered animation-delay creates a cascade effect. animation-fill-mode: forwards keeps the final state after the animation ends.
πΈ Verified Output:
Step 4: Animation Properties
Write this file:
π‘ Key animation properties:animation-iteration-count (infinite or number), animation-direction (normal/reverse/alternate), animation-fill-mode (forwards = stay at end), animation-play-state (running/paused β controllable with JavaScript).
πΈ Verified Output:
Step 5: Hover Effects & Interactive Animations
Write this file:
π‘ CSS pseudo-elements (::before, ::after) add decorative content without extra HTML. The ripple effect uses a pseudo-element that starts at 0 size and grows to cover the button on hover. overflow: hidden clips it to the button shape.
πΈ Verified Output:
Step 6: Loading Spinner Animation
Write this file:
π‘ Loading indicators reduce perceived wait time and prevent users from thinking something broke. The shimmer/skeleton loader is especially effective β it shows content shape before data loads, reducing layout shift when content appears (better CLS score).
πΈ Verified Output:
Step 7: Card Flip Animation (3D Transforms)
Write this file:
π‘ 3D transforms require three things:perspective on the parent (creates depth), transform-style: preserve-3d on the flip container, and backface-visibility: hidden on each face. perspective: 1000px means the vanishing point is 1000px away β smaller = more dramatic 3D effect.
πΈ Verified Output:
Step 8: Capstone β Animated Hero Section
Write this file:
π‘ Animation checklist: Use animation-fill-mode: both to apply the start state immediately. Stagger delays with multiples of 0.2s for elegant cascades. Always check prefers-reduced-motion in production for accessibility. Gradient animations use background-size: 400% to give the position something to move through.