Skip to main content
CSS for Beginners
CHAPTER 10 Beginner

CSS Transitions, Transforms, and Animations

Updated: May 10, 2026
5 min read

# CSS Transitions & Animations

1. Introduction

Static websites are boring. Adding subtle movements, hover effects, and loading spinners makes a website feel modern and interactive. CSS provides powerful, hardware-accelerated tools to animate your elements without using JavaScript.

2. Learning Objectives

  • Smoothly transition states using transition.
  • Move, scale, and rotate elements using transform.
  • Create infinite loading spinners using @keyframes and animation.

3. Detailed Explanations

CSS Transitions

A transition smoothly changes a CSS property over a specified duration. It's usually paired with a :hover pseudo-class.
css
1234567891011
.btn {
    background-color: blue;
    color: white;
    padding: 10px 20px;
    /* property duration timing-function */
    transition: background-color 0.3s ease-in-out;
}

.btn:hover {
    background-color: darkblue; /* Smoothly changes over 0.3s! */
}

CSS Transforms

Transforms manipulate the physical appearance of an element without altering the document flow (other elements don't get pushed around).
  • Translate: Move an element X or Y pixels.
  • Scale: Make it bigger or smaller.
  • Rotate: Spin it.
css
12345678
.card {
    transition: transform 0.2s ease;
}

.card:hover {
    /* Moves UP 5px and grows by 5% */
    transform: translateY(-5px) scale(1.05);
}

CSS Keyframe Animations

While transitions only happen on state changes (like hover), animations can run infinitely on page load. You define "keyframes" (percentages of the animation).
css
1234567891011121314151617
/* Define the animation */
@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Apply the animation to an element */
.loader {
    width: 50px;
    height: 50px;
    border: 5px solid #f3f3f3;
    border-top: 5px solid #3498db;
    border-radius: 50%;
    
    /* name duration timing-function iteration-count */
    animation: spin 1s linear infinite;
}

4. Mini Project: Interactive Product Card

html
12345
<div class="product">
    <img src="shoes.jpg" alt="Shoes">
    <h3>Running Shoes</h3>
    <button>Buy Now</button>
</div>
css
123456789101112131415161718192021222324252627
.product {
    background: white;
    padding: 20px;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    text-align: center;
}

.product:hover {
    transform: translateY(-10px);
    box-shadow: 0 20px 25px rgba(0,0,0,0.15);
}

.product button {
    background: #10b981;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 6px;
    transition: background 0.2s;
    cursor: pointer;
}

.product button:hover {
    background: #059669;
}

5. MCQs

Q1: Which property defines how long a hover effect takes to complete? A) transform B) transition-duration C) animation-delay D) time *Answer: B*

6. Summary

Use transition for simple hover states like button colors and subtle card lifts. Use @keyframes for complex, multi-step, or infinite animations like loading spinners. Always prioritize performance by animating transform and opacity rather than width or margin.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·