Episode 3 of 13

Transitions

Create smooth transitions between CSS states — hover effects, state changes, and interactive transitions.

Transitions

CSS transitions let you smoothly animate property changes over a specified duration. Instead of an instant jump from one state to another, the browser interpolates between the values creating smooth motion.

The Transition Property

.button {
    background: #3498db;
    transform: scale(1);
    transition: all 0.3s ease;
}

.button:hover {
    background: #2980b9;
    transform: scale(1.05);
}

Without transition, the hover change is instant. With it, the color and scale change smoothly over 0.3 seconds.

Transition Sub-Properties

PropertyWhat It ControlsExample
transition-propertyWhich CSS property to animatebackground, transform, all
transition-durationHow long the transition takes0.3s, 500ms
transition-timing-functionThe acceleration curveease, linear, ease-in-out
transition-delayWait before starting0.1s, 200ms

Longhand Syntax

.card {
    transition-property: transform, box-shadow;
    transition-duration: 0.3s;
    transition-timing-function: ease;
    transition-delay: 0s;
}

.card:hover {
    transform: translateY(-8px);
    box-shadow: 0 10px 30px rgba(0,0,0,0.15);
}

Shorthand Syntax

/* property | duration | timing | delay */
transition: transform 0.3s ease 0s;

/* Multiple properties */
transition: transform 0.3s ease,
            box-shadow 0.3s ease,
            background 0.2s ease;

What Can Be Transitioned?

✅ Animatable❌ Not Animatable
opacitydisplay
transformfont-family
background-colorposition (static/relative/etc.)
colorfloat
width, heightcontent
margin, paddingtext-align
border-radiusoverflow
box-shadow

Common Transition Patterns

Hover Lift Effect

.card {
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
    transform: translateY(-6px);
    box-shadow: 0 12px 24px rgba(0,0,0,0.12);
}

Color Fade

.link {
    color: #333;
    transition: color 0.3s ease;
}
.link:hover {
    color: #3498db;
}

Grow on Hover

.icon {
    transition: transform 0.2s ease;
}
.icon:hover {
    transform: scale(1.2);
}

Background Slide

.btn {
    background: linear-gradient(to right, #3498db 50%, #2c3e50 50%);
    background-size: 200% 100%;
    background-position: right;
    transition: background-position 0.4s ease;
}
.btn:hover {
    background-position: left;
}

Transition on all vs Specific Properties

allSpecific Properties
Quick to writeMore explicit and intentional
Transitions everything — even unintended propertiesOnly transitions what you specify
Can cause performance issuesBetter performance — only animate what matters

Key Takeaways

  • Transitions animate property changes between two states (A → B)
  • Specify property, duration, timing-function, and optionally delay
  • Prefer specific properties over all for better performance
  • Not all properties are animatable — display cannot be transitioned
  • Transitions are triggered by state changes (hover, focus, class toggle)
  • Combine transform and box-shadow for the classic hover-lift effect