DEV Community

Cover image for 180 Days of Frontend Development Challenge. Day 38 CSS Transitions and Animations.
CodeWithDhanian
CodeWithDhanian

Posted on

180 Days of Frontend Development Challenge. Day 38 CSS Transitions and Animations.

Today, we elevate our frontend skills by transitioning from static interfaces to dynamic, interactive user experiences through CSS Transitions and Animations.

Mastering these techniques is essential for modern frontend development, enabling you to craft visually appealing, responsive, and professional interfaces without relying on JavaScript for every animation.

1. Why CSS Transitions and Animations Matter

Transitions and animations:

→ Enhance user experience through smooth feedback and micro-interactions.
→ Improve visual hierarchy and guide user attention.
→ Add subtle polish that differentiates professional applications from amateur projects.
→ Maintain performance since CSS animations often use the GPU and run on the compositor thread.

Understanding when and how to apply them will significantly improve your UI and UX design capabilities.

2. CSS Transitions: Controlled State Changes

What Transitions Do

CSS Transitions allow properties to change smoothly over a specified duration rather than instantly, triggered by:

→ Hover (:hover)
→ Focus (:focus)
→ State changes via class manipulation
→ Direct property changes via JavaScript

Core Properties

  • transition-property: Specifies which property to animate.
  • transition-duration: Sets the length of the animation.
  • transition-timing-function: Defines the acceleration curve.
  • transition-delay: Delays the start of the animation.
  • transition (shorthand): Combines the above properties for clarity.

Timing Functions Deep Dive

Understanding timing functions is critical for creating natural-feeling animations:

  • ease (default): Slow start, fast middle, slow end.
  • linear: Constant speed.
  • ease-in: Slow start.
  • ease-out: Slow end.
  • ease-in-out: Slow start and end.
  • cubic-bezier(x1, y1, x2, y2): Allows custom control curves.

Use tools like cubic-bezier.com to experiment and visualize curves before applying them.

Practical Example: Hover Button Transition

.button {
    background-color: #2c3e50;
    color: white;
    padding: 12px 24px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.3s ease, transform 0.2s ease, box-shadow 0.2s ease;
}

.button:hover {
    background-color: #34495e;
    transform: translateY(-3px);
    box-shadow: 0 6px 12px rgba(0,0,0,0.15);
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Transitions background-color, transform, and box-shadow.
  • Uses ease for a natural feel.
  • Creates a lift effect, enhancing clickability.

3. CSS Animations: Multi-Step, Complex Motion

While transitions animate between two states, animations allow control over multiple keyframes, looping, and advanced timing.

Animations are triggered automatically when applied and do not require direct state changes.

Core Animation Properties

  • @keyframes: Defines animation stages (0% to 100%).
  • animation-name: Links to @keyframes.
  • animation-duration: Total duration.
  • animation-timing-function: Speed curve between frames.
  • animation-delay: Start delay.
  • animation-iteration-count: Number of times to repeat (infinite for loops).
  • animation-direction: Normal, reverse, alternate.
  • animation-fill-mode: Determines if styles before/after animation apply.
  • animation-play-state: Running or paused.
  • animation (shorthand): Combines the above properties.

Animation Fill Modes

  • none (default): No styles retained before or after animation.
  • forwards: Retains the final keyframe styles.
  • backwards: Applies the first keyframe during delay.
  • both: Applies both forwards and backwards behaviors.

Using animation-fill-mode: forwards is crucial for one-time entrance animations.

Practical Example 1: Bouncing Box

@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-20px); }
}

.box {
    width: 80px;
    height: 80px;
    background-color: #e67e22;
    border-radius: 8px;
    animation: bounce 1.5s ease-in-out infinite alternate;
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Uses alternate to bounce back and forth smoothly.
  • ease-in-out for a natural bounce.
  • Runs infinitely as a loader or decorative motion element.

Practical Example 2: Loader Spinner

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

.loader {
    width: 40px;
    height: 40px;
    border: 5px solid #ddd;
    border-top: 5px solid #2980b9;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Creates a continuous loading spinner.
  • Uses linear for consistent speed, suitable for loaders.

Practical Example 3: Modal Entrance Animation

@keyframes fadeInScale {
    0% { opacity: 0; transform: scale(0.8); }
    100% { opacity: 1; transform: scale(1); }
}

.modal {
    opacity: 0;
    transform: scale(0.8);
}

.modal.show {
    animation: fadeInScale 0.3s ease-out forwards;
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Combines scaling and fading for a professional modal appearance.
  • Uses forwards to retain the final visible state.

4. Best Practices for Using Transitions and Animations

→ Use animations and transitions to enhance UX, not distract.
→ Avoid animating properties that trigger reflow (width, height, top, left) unless necessary; prefer transform and opacity for better performance.
→ Ensure accessibility by allowing users to disable animations via media queries:

@media (prefers-reduced-motion: reduce) {
    * {
        animation: none !important;
        transition: none !important;
    }
}
Enter fullscreen mode Exit fullscreen mode

→ Use short, snappy durations for interactive elements (0.2s–0.5s) and longer for decorative animations (1s+).
→ Use cubic-bezier for fine-tuning and unique motion effects when needed.

5. Your Day 38 Practical Challenge

To apply these concepts deeply:

→ Create a navigation menu with 4+ links, adding smooth transitions for background color, text color, and transform: translateY() or subtle box-shadow on hover.
→ Build a responsive image gallery that scales images on hover with transform: scale(), adds opacity adjustments, and subtle shadows.
→ Design a custom loading indicator (spinner, bouncing dots, or pulsing shape) using @keyframes and infinite loops.
→ Implement a modal with smooth entrance animations using opacity and scaling, retaining its final state using animation-fill-mode: forwards.
→ Create a shake animation for input validation feedback using @keyframes, triggering on a class change for invalid input.
→ Bonus: Research and try building a CSS-only parallax scrolling effect using transform and position: sticky.

6. Extend Your Learning

To solidify your Frontend Development journey, including layouts, CSS mastery, and interactive interfaces, study:

The Ultimate Guide to Responsive Web Design by Dhanian

This ebook will guide you through practical, real-world projects while sharpening your CSS, HTML, and responsive design skills.

Conclusion

CSS transitions and animations are critical skills for any frontend developer aiming to build modern, interactive, and user-friendly interfaces.

They help:

→ Guide user focus and improve usability.
→ Create a polished, professional feel.
→ Increase user satisfaction and perceived performance of your interfaces.

Take time today to experiment deeply with these concepts, adding them to your project portfolio.

Top comments (1)

Collapse
 
ritikaagrawal08 profile image
Ritika Agrawal

Informative!