CSS Animations & Transitions

CSS Transitions

The CSS Transition property allows you to create smooth animations between property changes.

Without transitions, style changes happen instantly. With transitions, changes occur gradually over a specified duration, creating a more polished user experience.

Transitions are commonly used for:

  • Button hover effects
  • Menu interactions
  • Image effects
  • Card animations
  • Modern UI components

Syntax

Transition Syntax

css

selector {
    transition: property duration timing-function delay;
}

Transition Example

css

button {
    transition: background-color 0.3s ease;
}

Smoothly changes the button's background color over 0.3 seconds.

Attributes

PropertyDescriptionExample
transitionShorthand propertytransition: all 0.3s ease;
transition-propertySpecifies which property changestransition-property: width;
transition-durationSets animation timetransition-duration: 1s;
transition-timing-functionControls animation speed curvetransition-timing-function: ease;
transition-delayDelays animation starttransition-delay: 0.5s;

Example

CSS Transitions Complete Example

html

<!DOCTYPE html>
<html>
<head>
    <title>CSS Transitions Example</title>
    <style>
        .button {
            background-color: #007acc;
            color: white;
            padding: 12px 24px;
            border: none;
            cursor: pointer;
            transition: background-color 0.4s ease;
        }
        .button:hover {
            background-color: #005999;
        }
    </style>
</head>
<body>
    <button class="button">
        Hover Me
    </button>
</body>
</html>

Output

Browser Output

css

A blue button will appear
When the mouse hovers over the button, the color will gradually change to a darker blue
The change will occur smoothly over 0.4 seconds
When the mouse leaves, the button will smoothly return to its original color

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE
YesYesYesYesYesPartial

Notes

Transition Multiple Properties

Multiple Property Transition

css

transition:
    background-color 0.3s ease,
    transform 0.3s ease;

Animates both color and transform changes.

Width Transition

Width Transition

css

.box {
    width: 100px;
    transition: width 0.5s ease;
}
.box:hover {
    width: 200px;
}

Smoothly expands the width.

Timing Functions

ValueEffect
easeSlow start and end
linearConstant speed
ease-inSlow start
ease-outSlow end
ease-in-outSlow start and end

Transition All Properties

Transition All Properties

css

transition: all 0.3s ease;

Applies transitions to all animatable properties.

Common Transition Example

Common Transition Example

css

.card {
    transition: transform 0.3s ease;
}
.card:hover {
    transform: scale(1.05);
}

Creates a smooth zoom effect.

Conclusion

CSS Transitions provide an easy way to create smooth and professional visual effects when element properties change. By controlling duration, timing, and delays, transitions enhance user experience and make interfaces feel more responsive and interactive.