CSS Animations and Transitions:
CSS Animations Transitions:
CSS transitions allow you to smoothly change property values over a specified duration. Unlike CSS animations (which use @keyframes), transitions are simpler and ideal for hover effects, button interactions, and micro-interactions.
What Are CSS Transitions? CSS transitions let you animate changes in CSS properties (like color, width, opacity) without JavaScript. They work by defining:
Which property to animate (transition-property
)
How long the animation should take (transition-duration
)
Delay before the animation starts (transition-delay
)
Speed curve of the transition (transition-timing-function
)
Basic Syntax
.element {
transition: [property] [duration] [timing-function] [delay];
}
Property | Description | Example |
---|---|---|
transition-property |
Specifies which CSS property to animate |
transition-property: width; |
transition-duration |
Sets how long the transition lasts (in s or ms) |
transition-duration: 0.5s; |
transition-timing-function |
Controls the speed curve (ease, linear, ease-in-out) |
transition-timing-function: ease-in-out; |
transition-delay |
Delays the start of the transition |
transition-delay: 0.2s; |
Shorthand Example
button {
background: blue;
transition: background 0.3s ease-in-out 0.1s;
}
button:hover {
background: red;
}
Practical CSS Transition Examples
Example 1: Smooth Hover Effect
.button {
padding: 10px 20px;
background: #3498db;
color: white;
transition: background 0.3s ease;
}
.button:hover {
background: #e74c3c;
}
✅ Effect: Button color fades smoothly on hover.
Example 2: Expanding Width
.box {
width: 100px;
height: 100px;
background: #2ecc71;
transition: width 0.5s ease;
}
.box:hover {
width: 200px;
}
✅ Effect: The box expands smoothly when hovered.
Example 3: Fade-In Effect
.fade-element {
opacity: 0;
transition: opacity 1s ease-in;
}
.fade-element.show {
opacity: 1;
}
Effect: Element fades in when the .show class is added (useful with JavaScript).
Transition Timing Functions Control the animation speed curve:
Value | Behavior |
---|---|
ease |
Starts slow, speeds up, ends slow (default) |
linear |
Constant speed |
ease-in |
Starts slow |
ease-out |
Ends slow |
ease-in-out |
Slow start and end |
cubic-bezier() |
Custom speed curve (advanced) |
Example:
.element {
transition: transform 0.5s cubic-bezier(0.17, 0.67, 0.83, 0.67);
}
Feature | Transition | Animation (@keyframes) |
---|---|---|
Trigger | Needs a state change (e.g., :hover) | Automatically or via JavaScript |
Complexity | Simple property changes | Multi-step animations |
Control | Limited to start/end states | Full control with keyframes |
Performance | Lightweight | Slightly heavier |
✔ Hover effects
✔ Micro-interactions (buttons, menus)
✔ Simple state changes
✔ Complex multi-step animations
✔ Looping effects
✔ Scroll-triggered animations
Best Practices for CSS Transitions
Optimize Performance – Animate opacity
and transform
(GPU-accelerated).
Keep It Subtle – Avoid excessive animations (0.3s–0.5s durations work best).
Use will-change
for Smoothness – Helps browsers optimize rendering.
.element {
will-change: transform, opacity;
}
Test on Mobile – Some transitions may lag on low-end devices.
CSS transitions are perfect for smooth, lightweight animations without JavaScript. By mastering transition-property
, duration
, and timing-function
, you can enhance user experience with engaging hover effects, fades, and transformations.