CSS Transitions

Animate smooth changes between interface states.

Modern websites often include small visual effects that make interactions feel smooth and professional. For example, a button may gradually change color when a user hovers over it, or an image may slowly enlarge when selected. These effects are created using CSS Transitions.

A CSS transition allows a property to change from one value to another over a specified period of time. Instead of an instant change, the transition creates a smooth animation effect.

Transitions are simple to use and can greatly improve the user experience without requiring JavaScript.

Why Use CSS Transitions?

CSS transitions help developers:

  • Create smoother user interactions
  • Improve website appearance
  • Provide visual feedback to users
  • Make buttons and menus feel more interactive
  • Enhance the overall user experience

How CSS Transitions Work

A transition occurs when a CSS property changes value.

Without a transition:

CSS
button:hover {
  background-color: green;
}

The color changes immediately.

With a transition:

CSS
button {
  transition: background-color 0.5s;
}

button:hover {
  background-color: green;
}

The color gradually changes over half a second.

The Transition Property

The transition property is used to define how the animation should behave.

Basic Syntax

CSS
transition: property duration;

Example:

CSS
transition: color 0.3s;

This smoothly changes the text color over 0.3 seconds.

Transition Duration

The duration determines how long the transition takes.

Example

CSS
button {
  transition: background-color 1s;
}

The transition will take one second to complete.

Time values can be written using seconds or milliseconds.

Transition Multiple Properties

You can animate multiple properties at the same time.

Example

CSS
.card {
  transition: background-color 0.5s, transform 0.5s;
}

.card:hover {
  background-color: lightblue;
  transform: scale(1.05);
}

When hovered, the background color changes and the card becomes slightly larger.

Transition Delay

The transition-delay property waits before starting the animation.

Example

CSS
button {
  transition: background-color 0.5s;
  transition-delay: 0.2s;
}

The transition begins after a short delay.

Transition Timing Functions

Timing functions control the speed pattern of a transition.

Example

CSS
button {
  transition: all 0.5s ease;
}

Common timing functions include ease, linear, ease-in, ease-out, and ease-in-out.

Key Takeaways

  • Transitions is an important topic to understand.
  • Start with small examples and practice one step at a time.
  • Use the idea in simple projects so it becomes easier to remember.