CSS transition Property

The CSS transition property is a shorthand property that controls how changes between CSS property values are animated over time.

It combines the transition property, duration, timing function, delay, and behavior into a single declaration. Transitions are commonly used for hover effects, focus indicators, color changes, transforms, and interactive interface elements. A transition occurs when an eligible CSS property changes between values that can be transitioned.

transition Syntax

The transition shorthand accepts values that identify the CSS property to animate, the duration, the timing function, an optional delay, and an optional transition behavior.

Basic Syntax

.example {
  transition: background-color 0.3s ease;
}

Duration Only

.example {
  transition: 0.3s;
}

When no property is specified, the transition applies to all eligible properties.

Property and Duration

.example {
  transition: opacity 0.5s;
}

Property, Duration, and Timing Function

.example {
  transition: transform 0.4s ease-in-out;
}

Property, Duration, Timing Function, and Delay

.example {
  transition: opacity 0.5s ease 0.2s;
}

Including Transition Behavior

.example {
  transition: display 0.3s ease allow-discrete;
}

The allow-discrete keyword permits eligible discrete properties to participate in transitions. It does not make their values interpolate continuously.

Multiple Transitions

.example {
  transition: background-color 0.3s ease, transform 0.5s ease-in-out;
}

CSS-Wide Keywords

.example {
  transition: initial;
}
.example {
  transition: inherit;
}
.example {
  transition: unset;
}
.example {
  transition: revert;
}
.example {
  transition: revert-layer;
}

Invalid Examples

.example {
  transition: opacity 300;
}
.example {
  transition: opacity -1s;
}
.example {
  transition: opacity 0.3s 0.2s 0.1s;
}
.example {
  transition: opacity 0.3s ease-in-out 50%;
}

Time values require units such as s or ms. A transition duration cannot be negative, only two time values are permitted in a single transition definition, and percentages cannot replace time values.

Property Values

Component Description
transition-property Identifies which CSS property or properties should transition.
transition-duration Specifies how long the transition takes.
transition-timing-function Controls how the transition progresses over time.
transition-delay Specifies when the transition begins relative to the property change.
transition-behavior Controls whether eligible discrete properties are permitted to transition.

Property Characteristics

Characteristic Value
Initial value See individual shorthand components
Inherited No
Applies to All elements, including ::before and ::after
Computed value See individual shorthand components
Animation type Not animatable as a shorthand
Percentages Not accepted for transition times
Specification CSS Transitions Level 1 and Level 2

Initial Component Values

Property Initial Value
transition-property all
transition-duration 0s
transition-timing-function ease
transition-delay 0s
transition-behavior normal

Note: The default duration is 0s, so a visible transition generally requires an explicit positive duration. A positive delay can postpone a zero-duration change, but it does not create continuous interpolation.

How It Works

A CSS transition occurs when a property changes between eligible values and the browser determines that the change can be transitioned.

Step 1: Create an Element

<button class="transition-button" type="button">Hover Over Me</button>

Step 2: Define the Initial Appearance

.transition-button {
  background-color: #2563eb;
  color: #fff;
  padding: 12px 20px;
  border: 0;
}

Step 3: Add a Transition

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

Step 4: Change the Property on Hover

.transition-button:hover {
  background-color: #1e40af;
}

When the pointer enters the button, the background color changes smoothly over 0.3 seconds. When the pointer leaves, the property normally transitions back toward its original value.

Step 5: Transition Multiple Properties

.transition-button {
  transition: background-color 0.3s ease, transform 0.3s ease;
}
.transition-button:hover {
  background-color: #1e40af;
  transform: translateY(-3px);
}

Understanding the Trigger

The transition declaration does not start an animation by itself. A change in the computed value of an eligible property triggers the transition. Such changes can result from pseudo-classes, class changes, media queries, or other style updates.

transition-property Component

The transition-property component identifies which CSS properties are eligible for transitions.

Transition One Property

.example {
  transition: opacity 0.4s ease;
}

Transition All Eligible Properties

.example {
  transition: all 0.3s ease;
}

Transition a Transform

.example {
  transition: transform 0.3s ease;
}

Disable Transitions

.example {
  transition: none;
}

The none keyword disables transitions for the element.

Using a Custom Property

.example {
  transition: --progress 0.5s linear;
}

Custom properties can transition smoothly when registered with an appropriate syntax using @property or the CSS Properties and Values API. Unregistered custom properties generally use discrete animation behavior.

Recommended Practice

Specify the properties you intend to transition instead of automatically using all. This makes the effect more predictable and reduces unintended transitions.

transition-duration Component

The transition-duration component determines how long a transition takes to complete.

Seconds

.example {
  transition: opacity 0.5s;
}

Milliseconds

.example {
  transition: opacity 250ms;
}

Short Transition

.example {
  transition: background-color 0.15s ease;
}

Long Transition

.example {
  transition: transform 1.5s ease;
}

Zero Duration

.example {
  transition: opacity 0s;
}

A zero-duration transition does not produce a gradual visual change.

Invalid Negative Duration

.example {
  transition: opacity -0.5s;
}

Negative transition durations are invalid.

Choosing a Duration

Short durations are commonly used for hover and focus feedback. Longer durations may be appropriate for larger visual changes, but should not unnecessarily delay interaction.

transition-timing-function Component

The transition-timing-function component controls the rate at which a transition progresses.

ease

.example {
  transition: transform 0.5s ease;
}

The default timing function starts slowly, accelerates, and slows toward the end.

linear

.example {
  transition: opacity 0.5s linear;
}

Progresses at a constant rate.

ease-in

.example {
  transition: transform 0.5s ease-in;
}

Starts slowly and accelerates.

ease-out

.example {
  transition: transform 0.5s ease-out;
}

Starts quickly and slows toward the end.

ease-in-out

.example {
  transition: transform 0.5s ease-in-out;
}

Starts and ends slowly.

Custom cubic-bezier()

.example {
  transition: transform 0.5s cubic-bezier(0.2, 0.8, 0.2, 1);
}

Defines a custom easing curve using four control-point coordinates.

steps()

.example {
  transition: opacity 1s steps(4, end);
}

Changes the transition's progress in discrete steps rather than continuously.

step-start and step-end

.example {
  transition: opacity 1s step-start;
}
.example {
  transition: opacity 1s step-end;
}

linear() Timing Function

.example {
  transition: transform 0.6s linear(0, 0.4 30%, 0.8 60%, 1);
}

The linear() function defines a piecewise linear timing curve using specified progress points.

Important Consideration

Timing functions control the progression of the transition. They do not change the duration specified by transition-duration.

transition-delay Component

The transition-delay component specifies how long the browser waits before a transition begins.

Positive Delay

.example {
  transition: opacity 0.5s ease 0.2s;
}

The transition begins after a delay of 0.2 seconds.

No Delay

.example {
  transition: opacity 0.5s ease 0s;
}

Longer Delay

.example {
  transition: transform 0.4s ease 1s;
}

Negative Delay

.example {
  transition: opacity 1s linear -0.3s;
}

A negative delay starts the transition as though part of its duration has already elapsed.

Duration and Delay Order

.example {
  transition: opacity 0.5s ease 0.2s;
}

The first time value is the duration. The second time value is the delay.

Important Consideration

Delays can also apply when a transition reverses. Use them carefully to avoid making interface feedback feel unresponsive.

transition-behavior Component

The transition-behavior component controls whether properties with discrete animation behavior are permitted to participate in transitions.

Default Behavior

.example {
  transition: opacity 0.3s ease normal;
}

The normal value does not enable transitions for properties that require discrete transition behavior.

Allow Discrete Transitions

.example {
  transition: display 0.3s allow-discrete;
}

Transition Multiple Properties

.example {
  transition: opacity 0.3s ease, display 0.3s allow-discrete;
}

Using the Longhand Property

.example {
  transition: opacity 0.3s ease, display 0.3s;
  transition-behavior: allow-discrete;
}

Discrete Value Changes

Discrete transitions switch between values rather than interpolating continuously. The switch normally occurs at a defined point during the transition, but properties such as display and content-visibility have special switching behavior when transitioning to or from a visible state.

Important Consideration

The allow-discrete keyword permits eligible discrete transitions but does not make every CSS property smoothly animatable.

Browser Compatibility

Discrete transitions are newer than traditional CSS transitions. Check current support before relying on them for essential interface behavior.

Multiple Transitions

Use commas to define multiple transitions within a single declaration.

Two Properties

.example {
  transition: opacity 0.3s ease, transform 0.5s ease-in-out;
}

Different Durations

.example {
  transition: background-color 0.2s ease, transform 0.6s ease;
}

Different Timing Functions

.example {
  transition: opacity 0.3s linear, transform 0.4s ease-out;
}

Different Delays

.example {
  transition: opacity 0.3s ease 0s, transform 0.4s ease 0.2s;
}

Using all With an Override

.example {
  transition: all 0.3s ease, opacity 0.6s linear;
}

When multiple transition definitions match the same property, the last matching definition takes precedence for that property.

Recommended Practice

List individual properties when practical. This helps prevent unrelated style changes from unexpectedly animating.

Shorthand Value Order

The components within a single transition definition can appear in different orders, subject to parsing rules.

Standard Ordering

.example {
  transition: opacity 0.5s ease 0.2s;
}

Alternative Ordering

.example {
  transition: ease opacity 0.5s 0.2s;
}

Duration Before Delay

The first time value always represents transition-duration. The second time value represents transition-delay.

.example {
  transition: opacity 0.5s 0.2s ease;
}

Omitted Values

.example {
  transition: opacity 0.5s;
}

Omitted components use their initial values. In this example, the timing function defaults to ease, the delay defaults to 0s, and the behavior defaults to normal.

Shorthand Reset Behavior

Declaring transition resets all of its longhand components, including transition-behavior.

.example {
  transition-behavior: allow-discrete;
  transition: opacity 0.3s ease;
}

The shorthand resets transition-behavior to normal because the behavior was omitted.

Corrected Ordering

.example {
  transition: opacity 0.3s ease;
  transition-behavior: allow-discrete;
}

Alternatively, include allow-discrete directly in the shorthand declaration.

Animatable Properties

CSS properties have different animation types. These determine whether values can interpolate continuously or change discretely.

Colors

.example {
  transition: background-color 0.3s ease;
}

Opacity

.example {
  transition: opacity 0.3s ease;
}

Transforms

.example {
  transition: transform 0.4s ease;
}

Dimensions

.example {
  transition: width 0.4s ease;
}

Numeric dimensions can transition between compatible values. Transitions involving intrinsic sizes require additional CSS features and browser support.

Spacing

.example {
  transition: padding 0.3s ease;
}

Border Radius

.example {
  transition: border-radius 0.3s ease;
}

Discrete Properties

.example {
  transition: display 0.3s allow-discrete;
}

Performance Considerations

Transitions involving transform and opacity can often be rendered efficiently. Properties affecting layout or paint may require more work, especially on complex pages.

Important Distinction

Including a property in a transition declaration does not guarantee a continuous animation. The property's animation type and the values involved determine the result.

Transitions and Interaction

Transitions are commonly triggered by changes in pseudo-class states or CSS classes.

Hover

.button {
  transition: background-color 0.3s ease;
}
.button:hover {
  background-color: #1e40af;
}

Keyboard Focus

.button {
  transition: outline-color 0.2s ease;
}
.button:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 3px;
}

Active State

.button {
  transition: transform 0.15s ease;
}
.button:active {
  transform: scale(0.96);
}

Class Changes

.panel {
  opacity: 0.5;
  transition: opacity 0.3s ease;
}
.panel.is-active {
  opacity: 1;
}

JavaScript Class Toggle

const panel = document.querySelector(".panel");
panel.classList.toggle("is-active");

Responsive Style Changes

.panel {
  padding: 12px;
  transition: padding 0.3s ease;
}
@media (min-width: 768px) {
  .panel {
    padding: 24px;
  }
}

When a media query changes an eligible property value, the transition may run if the element already has an established before-change style.

Important Consideration

Hover is not available on every device. Important interactions should also support keyboard, touch, and other applicable input methods.

Transitions vs. Animations

CSS transitions and CSS animations both create motion, but they are designed for different situations.

Feature Transitions Animations
Trigger Eligible property value change Applying or changing an animation
Keyframes Not required Defined using @keyframes
Multiple stages Primarily transitions between two values Supports multiple keyframe stages
Repetition Not directly supported Supports iteration counts and infinite loops
Typical use Interactive state changes Sequenced or repeating motion

Transition Example

.button {
  transition: transform 0.3s ease;
}
.button:hover {
  transform: scale(1.1);
}

Animation Example

.icon {
  animation: pulse 1s ease-in-out infinite alternate;
}
@keyframes pulse {
  from {
    transform: scale(1);
  }
  to {
    transform: scale(1.1);
  }
}

Choosing Between Them

Use transitions for straightforward changes between interface states. Use animations when a sequence requires keyframes, repetition, or more detailed timeline control.

Interaction Between Both Features

CSS animations can take precedence over transitions for the same property while the animation is active. Avoid conflicting declarations when predictable results are important.

Inheritance

The transition property is not inherited. Child elements do not automatically receive the transition settings of their parents.

Parent Transition

.parent {
  transition: opacity 0.3s ease;
}

Child Transition

.child {
  transition: transform 0.3s ease;
}

Explicit Inheritance

.child {
  transition: inherit;
}

Restore Initial Values

.child {
  transition: initial;
}

Using unset

.child {
  transition: unset;
}

Because the transition longhand properties are not inherited, unset restores their initial values.

Inherited Property Changes

A child may transition a property whose computed value changes because of inheritance, but the child still needs its own applicable transition settings.

Recommended Practice

Declare transitions on the elements whose property changes you want to animate.

Practical Examples

Button Hover

.button {
  background-color: #2563eb;
  transition: background-color 0.3s ease;
}
.button:hover {
  background-color: #1e40af;
}

Card Lift

.card {
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
}

Image Zoom

.image-container {
  overflow: hidden;
}
.image-container img {
  transition: transform 0.4s ease;
}
.image-container:hover img {
  transform: scale(1.08);
}

Link Underline

.link {
  text-decoration-color: transparent;
  transition: text-decoration-color 0.2s ease;
}
.link:hover,
.link:focus-visible {
  text-decoration-color: currentColor;
}

Focus Outline

.input {
  outline: 2px solid transparent;
  outline-offset: 2px;
  transition: outline-color 0.2s ease;
}
.input:focus-visible {
  outline-color: #2563eb;
}

Opacity Change

.image {
  opacity: 0.7;
  transition: opacity 0.3s ease;
}
.image:hover {
  opacity: 1;
}

Navigation Indicator

.nav-link {
  border-bottom: 2px solid transparent;
  transition: border-color 0.2s ease;
}
.nav-link:hover,
.nav-link:focus-visible {
  border-color: #2563eb;
}

Expandable Panel

.panel {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.4s ease;
}
.panel.is-open {
  max-height: 400px;
}

This technique uses a fixed maximum height and may not produce uniform timing for content of different heights. More advanced intrinsic-size transitions require additional CSS features.

Rotating Icon

.icon {
  transition: transform 0.3s ease;
}
.is-open .icon {
  transform: rotate(180deg);
}

Delayed Tooltip Appearance

.tooltip {
  opacity: 0;
  transition: opacity 0.2s ease 0.3s;
}
.tooltip-trigger:hover .tooltip,
.tooltip-trigger:focus-within .tooltip {
  opacity: 1;
}

A complete tooltip implementation also needs appropriate semantics and visibility handling.

Reduced Motion Alternative

@media (prefers-reduced-motion: reduce) {
  .animated-control {
    transition: none;
  }
}

transition Example

This example demonstrates four transitions with different properties and timing functions. Hover over or keyboard-focus each button to see the effect.

HTML Example

<div class="transition-demo">
  <button class="demo-button demo-color" type="button">Color</button>
  <button class="demo-button demo-scale" type="button">Scale</button>
  <button class="demo-button demo-move" type="button">Move</button>
  <button class="demo-button demo-opacity" type="button">Opacity</button>
</div>

CSS Example

.transition-demo {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 20px;
  padding: 20px;
}
.demo-button {
  min-width: 100px;
  padding: 12px 18px;
  border: 0;
  border-radius: 4px;
  background: #2563eb;
  color: #fff;
  font: inherit;
  cursor: pointer;
}
.demo-button:focus-visible {
  outline: 3px solid #f59e0b;
  outline-offset: 3px;
}
.demo-color {
  transition: background-color 0.3s ease;
}
.demo-color:hover,
.demo-color:focus-visible {
  background-color: #1e40af;
}
.demo-scale {
  transition: transform 0.4s ease-in-out;
}
.demo-scale:hover,
.demo-scale:focus-visible {
  transform: scale(1.12);
}
.demo-move {
  transition: transform 0.4s ease-out;
}
.demo-move:hover,
.demo-move:focus-visible {
  transform: translateY(-8px);
}
.demo-opacity {
  transition: opacity 0.5s linear;
}
.demo-opacity:hover,
.demo-opacity:focus-visible {
  opacity: 0.5;
}
@media (prefers-reduced-motion: reduce) {
  .demo-button {
    transition: none;
  }
}
Browser Result

Understanding the Results

Each button changes a different CSS property when hovered or keyboard-focused.

Color Button

The background color transitions over 0.3 seconds using the ease timing function.

Scale Button

The button grows to 112% of its original size using ease-in-out.

Move Button

The button moves upward by 8 pixels using ease-out.

Opacity Button

The button becomes partially transparent over 0.5 seconds using linear.

Experiment With Duration

.demo-scale {
  transition: transform 1s ease-in-out;
}

Increasing the duration makes the scaling effect slower.

Experiment With Delay

.demo-move {
  transition: transform 0.4s ease-out 0.3s;
}

The movement begins after a 0.3-second delay.

Experiment With Timing Functions

.demo-opacity {
  transition: opacity 0.5s steps(4, end);
}

The opacity changes in four discrete steps instead of progressing continuously.

Accessibility Considerations

CSS transitions can improve visual feedback, but they should not interfere with usability or accessibility.

Respect Reduced Motion Preferences

Some users prefer reduced motion because animated effects may cause discomfort.

@media (prefers-reduced-motion: reduce) {
  .interactive-element {
    transition: none;
  }
}

Maintain Keyboard Focus Indicators

Transitions should not hide or significantly delay visible keyboard focus feedback.

Avoid Delayed Essential Feedback

Important state changes should remain clear without requiring users to wait for lengthy transitions.

Support Multiple Input Methods

Do not rely exclusively on :hover. Provide appropriate focus, active, and touch interaction behavior.

Maintain Sufficient Contrast

Ensure that text and controls remain readable throughout color and opacity transitions.

Avoid Excessive Movement

Large movements and scaling effects can be distracting, especially when repeated frequently.

Keep Interactive Controls Available

Visual transitions do not automatically manage keyboard focus, visibility, or accessibility semantics.

Provide Immediate Alternatives

When motion is reduced, preserve clear state changes even if transitions are removed.

Common Mistakes

Forgetting the Duration

.example {
  transition: opacity;
}

The duration defaults to 0s, so no gradual change is visible.

Using a Negative Duration

.example {
  transition: opacity -0.5s;
}

Negative durations are invalid.

Omitting Time Units

.example {
  transition: opacity 300;
}

Use 300ms or another valid time value.

Expecting a Transition Without a Value Change

A transition declaration does not animate anything until an eligible property value changes.

Using display Without Discrete Transition Support

.example {
  transition: display 0.3s;
}

To permit an eligible discrete transition, use allow-discrete and ensure the browser supports the feature.

Expecting all Properties to Interpolate

The all keyword does not make every CSS property continuously animatable.

Using transition: all Unnecessarily

Broad transitions can animate unintended property changes and may increase rendering work.

Applying the Transition Only on Hover

.button:hover {
  transition: background-color 0.3s ease;
  background-color: #1e40af;
}

Place the transition on the base selector when you want consistent entry and exit behavior.

Confusing Duration and Delay

The first time value is the duration, and the second is the delay.

Overwriting Longhand Values

The shorthand resets omitted transition components to their initial values.

Expecting auto Values to Always Interpolate

Some transitions involving intrinsic sizes require additional CSS features and browser support.

Ignoring Initial Rendering

Ordinary CSS transitions generally require an established before-change style. Newly inserted or previously non-rendered elements may need features such as @starting-style.

Ignoring Reduced Motion Preferences

Provide a reduced-motion alternative for nonessential movement.

Important Considerations

Keep the following points in mind when using transition:

  • The property is shorthand for five transition-related longhand properties.
  • The property is not inherited.
  • The default transition duration is 0s.
  • The default timing function is ease.
  • The default delay is 0s.
  • The default transition property is all.
  • The default transition behavior is normal.
  • A transition requires an eligible property value change.
  • Time values use units such as s and ms.
  • Negative durations are invalid.
  • Negative delays are allowed.
  • The first time value represents duration.
  • The second time value represents delay.
  • Multiple transitions are separated by commas.
  • Different properties can use different durations and timing functions.
  • Omitted shorthand components reset to their initial values.
  • Transitions can be triggered by pseudo-classes, class changes, and other style changes.
  • The all keyword does not make every property continuously animatable.
  • Discrete transitions may require allow-discrete.
  • Transitions involving newly rendered elements may require @starting-style.
  • The transform and opacity properties are often suitable for efficient visual effects.
  • Transitions affecting layout may require additional rendering work.
  • Use explicit property names when practical.
  • Do not rely exclusively on hover for essential interactions.
  • Maintain visible keyboard focus indicators.
  • Respect reduced motion preferences.
  • Test transition behavior across supported browsers.

Browser Support

The CSS transition property is widely supported across modern browsers. Traditional transitions involving colors, opacity, transforms, and other interpolable properties are broadly available. Newer features such as allow-discrete and transitions from an initial rendering state may have different compatibility requirements.

For current compatibility information, visit Can I Use? and search for CSS transitions.

Basic Transition

.example {
  transition: opacity 0.3s ease;
}

Multiple Transitions

.example {
  transition: opacity 0.3s ease, transform 0.4s ease-out;
}

Discrete Transition

.example {
  transition: display 0.3s allow-discrete;
}

Feature Detection

@supports (transition: opacity 0.3s ease) {
  .example {
    transition: opacity 0.3s ease;
  }
}

Discrete Transition Feature Detection

@supports (transition-behavior: allow-discrete) {
  .example {
    transition: display 0.3s allow-discrete;
  }
}

Testing Recommendations

Test timing functions, transition delays, multiple-property transitions, and discrete transitions in the browsers your website supports. Feature detection confirms syntax support but does not guarantee identical rendering of every transition scenario.

Specifications

The transition property is defined in the CSS Transitions specifications.

CSS Transitions Level 1 - The transition Shorthand Property

CSS Transitions Level 2

These specifications define transition timing, property matching, shorthand syntax, and newer discrete transition behavior.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: transition