Menu
CoddyTech

Transitions & Hover Effects

Part of the CSS Mastery section of Coddy's HTML journey. Lesson 24 of 43.

A transition lets you change a property smoothly over time instead of instantly.

selector {
  transition: property duration timing-function delay;
}
  • property: The CSS property you want to transition (e.g., background-color, width, opacity).
  • duration: The time it takes for the transition to complete (e.g., 1s for 1 second).
  • timing-function: How the transition progresses (e.g., linear, ease, ease-in).
  • delay: How long to wait before starting the transition (optional).

For example:

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

When you hover over the button, its background color will change from green to blue over 0.3 seconds.

To transition more than one property, list them in one transition declaration, separated by commas (a second transition line would replace the first):

transition: width 0.3s ease, opacity 0.3s ease;
challenge icon

Challenge

Easy

Add a transition property to the .card so that changes to the background color and box-shadow happen smoothly over 0.4 seconds with an ease timing function.

Then add a .card:hover rule that changes the card's background color and box-shadow, so the transition has something to animate.

Try it yourself

<!DOCTYPE html>
<html>
<head>
  <style>
    /* Write your CSS here */
    .card {
      background-color: white;
      padding: 10px;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    }
    
  </style>
</head>
<body>
  <div class="card">
    Hover over me!
  </div>
</body>
</html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in CSS Mastery

Practice on your own: Web playground