🎨

CSS Transitions

Intermediate
90 XP
30 min
Lesson Content

CSS Transitions

CSS transitions allow you to smoothly animate property changes over a specified duration. They make your website feel more polished and interactive.

Basic Transition

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

button:hover {
  background-color: red;
}

Transition Properties

  • transition-property: Which property to animate (e.g., background-color, width, all)
  • transition-duration: How long the transition takes (e.g., 0.3s, 500ms)
  • transition-timing-function: How the transition progresses (ease, linear, ease-in, ease-out, ease-in-out)
  • transition-delay: Delay before transition starts

Shorthand Syntax

/* Long form */
.element {
  transition-property: background-color;
  transition-duration: 0.3s;
  transition-timing-function: ease;
  transition-delay: 0s;
}

/* Shorthand */
.element {
  transition: background-color 0.3s ease 0s;
}

/* Multiple properties */
.element {
  transition: background-color 0.3s, width 0.5s, height 0.5s;
}

Timing Functions

  • ease - Slow start, fast middle, slow end (default)
  • linear - Constant speed
  • ease-in - Slow start
  • ease-out - Slow end
  • ease-in-out - Slow start and end

Common Use Cases

  • Hover effects on buttons and links
  • Smooth color changes
  • Size and position animations
  • Opacity fades
Example Code

Add transitions to buttons and links for smooth hover effects.

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #3b82f6;
  color: white;
  padding: 12px 24px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  /* Add transition for background-color, 0.3s duration, ease timing */
}

.button:hover {
  background-color: #2563eb;
  transform: scale(1.05);
}
</style>
</head>
<body>
  <button class="button">Hover Me</button>
</body>
</html>

Expected Output:

Button with smooth hover effect
Study Tips
  • Read the theory content thoroughly before practicing
  • Review the example code to understand key concepts
  • Proceed to the Practice tab when you're ready to code