Courses/HTML & CSS/Animations & Transitions

    CSS Animations & Transitions

    Lesson 11 โ€ข Intermediate Track

    What You'll Learn

    CSS transitions for smooth state changes
    CSS transforms: translate, scale, rotate, skew
    @keyframes for complex multi-step animations
    Animation timing functions and easing curves
    Popular animation patterns: fade, slide, pulse, spin
    Performance tips for smooth 60fps animations

    ๐Ÿ’ก Think of It Like This

    Transitions are like a light dimmer switch: You set a starting state and an ending state, and the browser smoothly transitions between them. A light switch is instant (on/off), but a dimmer gradually changes brightness โ€” that's what CSS transitions do for properties like colour, size, and position.

    Animations are like a movie: You define a sequence of frames (@keyframes) and the browser plays them in order, potentially looping forever. Much more powerful than simple transitions.

    1. CSS Transitions

    Transitions animate the change from one CSS value to another. You define what to animate,how long it takes, and the timing curve.

    PropertyWhat It DoesExample
    transition-propertyWhich property to animatebackground, transform, all
    transition-durationHow long the animation lasts0.3s, 500ms
    transition-timing-functionSpeed curve (easing)ease, linear, ease-in-out
    transition-delayWait before starting0s, 0.2s
    transition: property duration timing-function delay;

    CSS Transitions

    Hover over elements to see smooth transitions

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html>
    <head>
      <title>CSS Transitions</title>
      <style>
        body {
          background: #0f172a;
          font-family: system-ui, sans-serif;
          padding: 30px;
          color: #e5e7eb;
        }
    
        h3 { color: #22c55e; margin: 25px 0 10px; }
    
        .box {
          width: 150px;
          height: 80px;
          display: flex;
          align-items: center;
          justify-content: center;
          border-radius: 12px;
          font-weight: bold;
          cursor: pointer;
          margin: 10px 0;
        }
    
        /* Color t
    ...

    2. CSS Transforms

    Transforms change an element's shape, size, position, or rotation without affecting the page layout. They're GPU-accelerated, making them the fastest way to move things on screen.

    TransformWhat It DoesExample
    translate(x, y)Moves element horizontally/verticallytranslateX(50px)
    scale(x, y)Resizes element (1 = normal)scale(1.5)
    rotate(angle)Rotates clockwiserotate(45deg)
    skew(x, y)Tilts/slants elementskewX(15deg)

    ๐Ÿ’ก Pro Tip: You can combine multiple transforms: transform: translateX(50px) rotate(10deg) scale(1.1);

    3. @keyframes โ€” Multi-Step Animations

    While transitions only go from A to B, @keyframes lets you define any number of steps (A โ†’ B โ†’ C โ†’ D...) and control the timing of each.

    @keyframes bounce { 0% { transform: translateY(0); } 50% { transform: translateY(-30px); } 100% { transform: translateY(0); } } .element { animation: bounce 1s ease infinite; }

    @keyframes Animations

    See different animation patterns in action

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html>
    <head>
      <title>Keyframe Animations</title>
      <style>
        body {
          background: #0f172a;
          font-family: system-ui, sans-serif;
          padding: 30px;
          color: #e5e7eb;
        }
    
        h3 { color: #22c55e; margin: 30px 0 10px; }
    
        .demo {
          display: flex;
          gap: 20px;
          align-items: center;
          min-height: 100px;
        }
    
        .box {
          width: 80px;
          height: 80px;
          border-radius: 12px;
          display: flex;
          align-items: center;
          just
    ...

    4. Animation Properties Reference

    PropertyValuesWhat It Does
    animation-nameName of @keyframesWhich animation to play
    animation-duration0.5s, 2s, 300msHow long one cycle takes
    animation-timing-functionease, linear, ease-in-outSpeed curve / easing
    animation-delay0s, 0.5s, 1sWait before starting
    animation-iteration-count1, 3, infiniteHow many times to repeat
    animation-directionnormal, reverse, alternatePlay forward, backward, or both
    animation-fill-modenone, forwards, backwards, bothKeep end state after finishing
    animation: name duration timing-function delay iteration-count direction fill-mode;

    5. Practical: Animated Button & Loading Spinner

    Let's build two common real-world patterns โ€” a professionally animated button and a CSS-only loading spinner:

    Animated Button & Spinner

    Professional hover effects and a pure CSS loading animation

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html>
    <head>
      <title>Animated Button & Spinner</title>
      <style>
        body {
          background: #0f172a;
          font-family: system-ui, sans-serif;
          padding: 40px;
          color: #e5e7eb;
          display: flex;
          flex-direction: column;
          align-items: center;
          gap: 50px;
        }
    
        /* ANIMATED BUTTON */
        .btn {
          padding: 14px 40px;
          font-size: 1.1rem;
          font-weight: 700;
          border: none;
          border-radius: 12px;
          cursor: pointer;
          colo
    ...

    6. Animation Performance Tips

    Not all CSS properties animate equally well. For smooth 60fps animations, stick to properties that are handled by the GPU:

    โœ… Fast (GPU)โŒ Slow (causes reflow)
    transformwidth / height
    opacitytop / left / right / bottom
    filtermargin / padding

    ๐Ÿ’ก Golden Rule: Instead of animating top or left, use transform: translate(). Instead of animating width, use transform: scale().

    โš ๏ธ Common Mistakes

    • Animating width/height instead of transform โ€” Width/height changes force the browser to recalculate layout for every frame, causing jank. Use transform: scale() instead.
    • Too many animations at once โ€” Animating 50 elements simultaneously will drop frame rates. Be selective โ€” one or two well-placed animations are more effective than many.
    • Forgetting animation-fill-mode: forwards โ€” Without it, elements snap back to their original state after the animation ends. Add forwards to keep the end state.
    • Animations that never stop โ€” Infinite animations consume CPU/battery. Only use infinite for loading spinners or subtle background effects.
    • No reduced-motion support โ€” Some users get motion sickness. Add @media (prefers-reduced-motion: reduce) to disable animations for those users.

    ๐ŸŽ‰ Lesson Complete

    You now know how to bring your web pages to life with CSS animations. Key takeaways:

    • โœ… Transitions animate between two states (hover, focus, active)
    • โœ… Transforms move, scale, rotate, and skew elements efficiently
    • โœ… @keyframes create multi-step, looping animations
    • โœ… Animate transform and opacity for smooth 60fps performance
    • โœ… Use animation-fill-mode: forwards to keep the end state
    • โœ… Respect prefers-reduced-motion for accessibility

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service