CSS Transitions

Create smooth animations between state changes with CSS Transition properties

What are CSS Transitions?

CSS Transitions allow you to smoothly animate changes to CSS properties over a specified duration. They provide control over how property changes occur, making interfaces feel more dynamic and polished.

Key Transition Properties:

  • transition-property - Specifies which properties to transition
  • transition-duration - Sets the length of the transition
  • transition-timing-function - Defines the acceleration curve
  • transition-delay - Sets a delay before the transition starts
  • transition - Shorthand for all transition properties

CSS Transitions Reference

PropertyDescriptionValues
transition-propertySpecifies which CSS properties to transitionnone | all | property-name
transition-durationSets the length of time a transition takestime (s or ms)
transition-timing-functionDefines the acceleration curve of the transitionease | linear | ease-in | ease-out | ease-in-out | cubic-bezier() | steps()
transition-delaySets a delay before the transition startstime (s or ms)
transitionShorthand for all transition propertiesproperty duration timing-function delay

CSS Transitions in Action

Example Code

/* CSS Transitions */

/* Basic Transition */
.transition-basic {
  transition: all 0.3s ease;
}

.transition-basic:hover {
  background-color: #3498db;
  transform: scale(1.1);
}

/* Transition with Multiple Properties */
.transition-multiple {
  transition: background-color 0.3s ease, transform 0.5s ease-in-out, opacity 0.4s linear;
}

.transition-multiple:hover {
  background-color: #e74c3c;
  transform: translateX(20px);
  opacity: 0.8;
}

/* Transition with Delay */
.transition-delay {
  transition: all 0.3s ease 0.2s;
}

.transition-delay:hover {
  background-color: #2ecc71;
  transform: rotate(15deg);
}

/* Transition with Different Timing Functions */
.transition-ease {
  transition: all 0.5s ease;
}

.transition-linear {
  transition: all 0.5s linear;
}

.transition-ease-in {
  transition: all 0.5s ease-in;
}

.transition-ease-out {
  transition: all 0.5s ease-out;
}

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

.transition-cubic-bezier {
  transition: all 0.5s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

/* Transition on Specific Properties */
.transition-specific {
  transition: transform 0.4s ease, box-shadow 0.3s linear;
}

.transition-specific:hover {
  transform: translateY(-10px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}

/* Transition with Steps */
.transition-steps {
  transition: all 0.5s steps(4, jump-end);
}

.transition-steps:hover {
  transform: translateX(100px);
}

/* Transition with Initial Value */
.transition-initial {
  transition: all 0.3s ease;
  background-color: initial;
}

.transition-initial:hover {
  background-color: #9b59b6;
}

/* Transition with Inherit */
.transition-inherit {
  transition: all 0.3s ease;
  color: inherit;
}

.transition-inherit:hover {
  color: #e67e22;
}

/* Transition with Unset */
.transition-unset {
  transition: all 0.3s ease;
  border-color: unset;
}

.transition-unset:hover {
  border-color: #34495e;
}

/* Transition with Custom Properties */
:root {
  --transition-duration: 0.3s;
  --transition-timing: ease;
  --main-color: #3498db;
}

.transition-custom {
  transition: all var(--transition-duration) var(--transition-timing);
}

.transition-custom:hover {
  background-color: var(--main-color);
}

/* Chained Transitions */
.transition-chain {
  transition: background-color 0.3s ease, transform 0.3s ease 0.3s;
}

.transition-chain:hover {
  background-color: #f39c12;
  transform: scale(1.2);
}

/* Transition on Focus */
.transition-focus {
  transition: all 0.3s ease;
}

.transition-focus:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.5);
  transform: scale(1.05);
}

/* Transition on Active */
.transition-active {
  transition: all 0.1s ease;
}

.transition-active:active {
  transform: scale(0.95);
}

/* Transition with JavaScript Classes */
.transition-js {
  transition: all 0.3s ease;
}

.transition-js.active {
  background-color: #e74c3c;
  transform: rotate(45deg);
}

/* Transition with Max-Height for Accordions */
.accordion {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
}

.accordion.open {
  max-height: 500px;
}

/* Transition with Width */
.transition-width {
  transition: width 0.5s ease;
}

.transition-width:hover {
  width: 100%;
}

/* Transition with Height */
.transition-height {
  transition: height 0.5s ease;
}

.transition-height:hover {
  height: 200px;
}

/* Transition with Opacity for Fade Effects */
.transition-opacity {
  transition: opacity 0.5s ease;
}

.transition-opacity:hover {
  opacity: 0.5;
}

/* Transition with Border */
.transition-border {
  transition: border 0.3s ease;
}

.transition-border:hover {
  border: 3px solid #3498db;
}

/* Transition with Box Shadow */
.transition-shadow {
  transition: box-shadow 0.3s ease;
}

.transition-shadow:hover {
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}

/* Transition with Color */
.transition-color {
  transition: color 0.3s ease;
}

.transition-color:hover {
  color: #e74c3c;
}

/* Transition with Background */
.transition-background {
  transition: background 0.3s ease;
}

.transition-background:hover {
  background: linear-gradient(45deg, #3498db, #9b59b6);
}

/* Transition with Filter */
.transition-filter {
  transition: filter 0.3s ease;
}

.transition-filter:hover {
  filter: blur(2px) grayscale(50%);
}

/* Transition with Backdrop Filter */
.transition-backdrop {
  transition: backdrop-filter 0.3s ease;
}

.transition-backdrop:hover {
  backdrop-filter: blur(5px);
}

/* Transition with Clip Path */
.transition-clip {
  transition: clip-path 0.5s ease;
}

.transition-clip:hover {
  clip-path: circle(50% at 50% 50%);
}

/* Transition with Gradient */
.transition-gradient {
  transition: background-image 0.5s ease;
  background-image: linear-gradient(to right, #3498db, #2980b9);
}

.transition-gradient:hover {
  background-image: linear-gradient(to right, #e74c3c, #c0392b);
}

/* Transition with Transform */
.transition-transform {
  transition: transform 0.5s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

.transition-transform:hover {
  transform: translateX(50px) rotate(15deg) scale(1.2);
}

/* Transition with Visibility */
.transition-visibility {
  transition: opacity 0.3s ease, visibility 0.3s ease;
  visibility: visible;
  opacity: 1;
}

.transition-visibility.hidden {
  visibility: hidden;
  opacity: 0;
}

/* Transition with Z-Index */
.transition-zindex {
  transition: z-index 0.3s ease, transform 0.3s ease;
}

.transition-zindex:hover {
  z-index: 10;
  transform: scale(1.2);
}

/* Transition with Overflow */
.transition-overflow {
  transition: all 0.3s ease;
  overflow: hidden;
}

.transition-overflow:hover {
  overflow: visible;
}

/* Transition with Position */
.transition-position {
  transition: top 0.3s ease, left 0.3s ease;
  position: relative;
}

.transition-position:hover {
  top: -10px;
  left: 10px;
}

/* Transition with Margin and Padding */
.transition-spacing {
  transition: margin 0.3s ease, padding 0.3s ease;
}

.transition-spacing:hover {
  margin: 20px;
  padding: 20px;
}

/* Transition with Font Properties */
.transition-font {
  transition: font-size 0.3s ease, font-weight 0.3s ease;
}

.transition-font:hover {
  font-size: 24px;
  font-weight: bold;
}

/* Transition with Text Properties */
.transition-text {
  transition: text-shadow 0.3s ease, letter-spacing 0.3s ease;
}

.transition-text:hover {
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
  letter-spacing: 2px;
}

/* Transition with Border Radius */
.transition-radius {
  transition: border-radius 0.3s ease;
}

.transition-radius:hover {
  border-radius: 50%;
}

/* Transition with Outline */
.transition-outline {
  transition: outline 0.3s ease;
}

.transition-outline:hover {
  outline: 2px solid #3498db;
}

/* Transition with Column Properties */
.transition-column {
  transition: column-count 0.3s ease, column-gap 0.3s ease;
  column-count: 1;
  column-gap: 20px;
}

.transition-column:hover {
  column-count: 2;
  column-gap: 30px;
}

/* Transition with Grid Properties */
.transition-grid {
  transition: grid-template-columns 0.3s ease, grid-gap 0.3s ease;
  display: grid;
  grid-template-columns: 1fr;
  grid-gap: 10px;
}

.transition-grid:hover {
  grid-template-columns: 1fr 1fr;
  grid-gap: 20px;
}

/* Transition with Flexbox Properties */
.transition-flex {
  transition: flex-direction 0.3s ease, justify-content 0.3s ease;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
}

.transition-flex:hover {
  flex-direction: row-reverse;
  justify-content: space-between;
}

/* Transition with Perspective */
.transition-perspective {
  transition: perspective 0.3s ease, transform 0.3s ease;
  perspective: 1000px;
}

.transition-perspective:hover {
  perspective: 500px;
  transform: rotateY(15deg);
}

/* Transition with Animation */
.transition-animation {
  transition: all 0.3s ease;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

.transition-animation:hover {
  animation: none;
  transform: scale(1.1);
}

/* Print Styles for Transitions */
@media print {
  .transition-print {
    transition: none !important;
  }
}

/* Reduced Motion Support */
@media (prefers-reduced-motion: reduce) {
  .transition-motion {
    transition: none !important;
  }
}

/* Dark Mode Transitions */
@media (prefers-color-scheme: dark) {
  .transition-dark {
    transition: background-color 0.3s ease, color 0.3s ease;
  }
  
  .transition-dark:hover {
    background-color: #2c3e50;
    color: #ecf0f1;
  }
}

/* High Contrast Mode */
@media (prefers-contrast: high) {
  .transition-contrast {
    transition: border-color 0.3s ease;
  }
  
  .transition-contrast:hover {
    border-color: #000;
  }
}

/* Performance Optimized Transitions */
.transition-performance {
  will-change: transform, opacity;
  transition: transform 0.3s ease, opacity 0.3s ease;
}

/* Transition Events with JavaScript */
.transition-events {
  transition: all 0.3s ease;
}

/* Nested Transitions */
.parent-transition {
  transition: all 0.3s ease;
}

.child-transition {
  transition: all 0.5s ease 0.1s;
}

.parent-transition:hover .child-transition {
  transform: translateX(20px);
}

/* Transition with Variables and Calc */
.transition-calc {
  --offset: 20px;
  transition: transform 0.3s ease;
}

.transition-calc:hover {
  transform: translateX(calc(var(--offset) * 2));
}

/* Transition with Viewport Units */
.transition-viewport {
  transition: width 0.3s ease, height 0.3s ease;
}

.transition-viewport:hover {
  width: 50vw;
  height: 50vh;
}

/* Transition with EMS/REMS */
.transition-ems {
  transition: padding 0.3s ease, margin 0.3s ease;
}

.transition-ems:hover {
  padding: 2em;
  margin: 1rem;
}

Transition Techniques

Timing Functions

Control the acceleration curve of your transitions.

Multiple Properties

Transition different properties with separate settings.

Delayed Transitions

Create sequenced animations with transition delays.

Performance Optimization

Use transforms and opacity for smooth 60fps animations.

Best Practices for Transitions

Effective Usage

  • Use transitions for subtle, meaningful animations
  • Keep durations between 200-500ms for most interactions
  • Use appropriate timing functions for different effects
  • Prefer transform and opacity for performance
  • Test transitions on various devices and browsers
  • Consider using will-change for complex animations

Common Mistakes to Avoid

  • Overusing transitions unnecessarily
  • Using transitions for large layout changes
  • Forgetting to test with reduced motion preferences
  • Using transitions on non-animatable properties
  • Creating transitions that are too slow or distracting
  • Not providing fallbacks for older browsers

Browser Support & Accessibility

  • CSS Transitions are supported in all modern browsers
  • Use feature queries (@supports) for progressive enhancement
  • Respect reduced motion preferences with media queries
  • Ensure transitions don't cause accessibility issues
  • Test with screen readers and keyboard navigation
  • Provide fallbacks for older browsers when necessary

Ready to Try It Yourself?

Experiment with CSS Transitions in our interactive editor. See your changes in real-time and build your understanding through practice.

< PreviousNext >