Skip to content

Courses / CSS3 Mastery: Beginner to Advanced / CSS Transitions

2,087 people viewed this

CSS Transitions

Section 27: CSS Transitions

CSS transitions smoothly interpolate supported property changes over a specified duration. A state change may come from :hover, :focus-visible, :active, a class change, or JavaScript.

Transition syntax

.button {
  background-color: #2563eb;
  transform: translateY(0);
  transition: background-color 200ms ease, transform 200ms ease;
}
.button:hover {
  background-color: #1d4ed8;
  transform: translateY(-2px);
}

Four key transition properties

  • transition-property: which CSS properties should transition.
  • transition-duration: how long the transition lasts.
  • transition-timing-function: how progress changes over time.
  • transition-delay: how long to wait before starting.

Timing functions

transition-timing-function: linear;
transition-timing-function: ease;
transition-timing-function: ease-in;
transition-timing-function: ease-out;
transition-timing-function: ease-in-out;
transition-timing-function: cubic-bezier(.2, .8, .2, 1);

linear uses constant progress; ease variants accelerate and decelerate in different ways. cubic-bezier() lets you define a custom timing curve.

Runnable example

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS Transitions</title>
<style>
body { font-family: Arial, sans-serif; padding: 30px; }
.button { padding: 12px 20px; border: 0; border-radius: 8px; background: #2563eb; color: white; cursor: pointer; transition: background-color .2s ease, transform .2s ease, box-shadow .2s ease; }
.button:hover { background: #1d4ed8; transform: translateY(-3px); box-shadow: 0 8px 18px #0002; }
.button:focus-visible { outline: 3px solid #f59e0b; outline-offset: 4px; }
.button:active { transform: translateY(0); }
</style>
</head>
<body>
<button class="button">Hover or focus me</button>
</body>
</html>

Best practices and common mistakes

  • Prefer transitioning specific properties rather than using transition: all when you can.
  • Do not transition properties that should change immediately for clarity or usability.
  • Transitions do not create a repeating animation by themselves; they respond to changes in values.
  • Provide a visible keyboard focus style. Hover is not a substitute for keyboard accessibility.
  • Use reasonable durations, often around 150–300ms for small interface interactions.

Practice exercise

Create a button that changes background, moves upward by 3px, and adds a shadow on hover. Make keyboard focus clearly visible.

Knowledge check: 10 MCQs

  1. What is the purpose of transition-duration? Answer: Sets how long the transition takes. Explanation: It specifies the time from the old value to the new value.
  2. Which property controls the easing curve? Answer: transition-timing-function. Explanation: It controls the rate of change over time.
  3. Does a transition repeat continuously by default? Answer: No. Explanation: A transition responds to a property value change.
  4. What does linear mean? Answer: Constant progress over time. Explanation: The rate of change remains constant.
  5. What does transition-delay do? Answer: Delays the start. Explanation: It waits before the transition begins.
  6. Which pseudo-class is useful for keyboard focus styling? Answer: :focus-visible. Explanation: It typically shows focus indicators when users navigate with a keyboard.
  7. Is transition: all always the best choice? Answer: No. Explanation: Explicit properties are easier to control and maintain.
  8. Can background-color be transitioned? Answer: Yes. Explanation: It is a commonly transitioned visual property.
  9. What is the role of :active? Answer: Matches an element while it is being activated. Explanation: It is useful for a pressed-button state.
  10. Should hover be the only way to reveal an important control? Answer: No. Explanation: Touch and keyboard users also need access.

Rate this course

No ratings yet — yours can be the first.

Stars show on this course. Your written note stays private.

Your stars