CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Transition Shorthand


The CSS transition shorthand property is a powerful tool for animating changes to CSS properties. It combines four individual transition properties—transition-property, transition-duration, transition-timing-function, and transition-delay—into a single, concise declaration, making your code cleaner and more efficient.


Example 1: Simple Background Color Fade

/* This class defines the initial state of our button. */
.button-one {
  background-color: #3498db; /* Blue background */
  color: white;
  padding: 15px 32px;
  text-align: center;
  font-size: 16px;
  /* Shorthand for a 0.5-second transition on the background-color property with an ease-in-out timing function. */
  transition: background-color 0.5s ease-in-out;
}

/* This defines the style of the button when a user hovers over it. */
.button-one:hover {
  background-color: #2980b9; /* Darker blue background on hover */
}

Explanation: This code creates a smooth color fade effect when a user hovers over the button. The transition property is set to change the background-color over a period of 0.5s using an ease-in-out function, which creates a natural-looking acceleration and deceleration.


Example 2: Width and Opacity Transition with Delay

/* This class styles a simple div element. */
.box-two {
  width: 100px;
  height: 100px;
  background-color: #e74c3c; /* Red background */
  opacity: 1;
  /* This shorthand transitions both the width and opacity properties. */
  /* width: 1s duration, ease timing, 0s delay */
  /* opacity: 0.5s duration, ease timing, 0.5s delay */
  transition: width 1s ease, opacity 0.5s ease 0.5s;
}

/* On hover, the box will expand and fade out. */
.box-two:hover {
  width: 200px;
  opacity: 0;
}

Explanation: This example demonstrates transitioning multiple properties with different durations and delays. When hovered, the width immediately starts transitioning over 1s, while the opacity transition begins after a 0.5s delay and completes in 0.5s.


Example 3: Transform and Color Change on a Link

/* Styling for our link. */
.link-three {
  color: #1abc9c; /* Teal color */
  text-decoration: none;
  font-size: 24px;
  display: inline-block;
  /* A transition is applied to the transform and color properties. */
  transition: transform 0.3s linear, color 0.3s linear;
}

/* The link will scale up and change color on hover. */
.link-three:hover {
  transform: scale(1.1); /* Enlarge the link */
  color: #16a085; /* Darker teal color */
}

Explanation: Here, the transition property is used to animate a transform (scaling) and a color change simultaneously. Both transitions have a duration of 0.3s and a linear timing function, resulting in a constant speed for the animation.


Example 4: All Properties Transition with Cubic Bezier

/* The initial state of the circle. */
.circle-four {
  width: 50px;
  height: 50px;
  background-color: #9b59b6; /* Purple background */
  border-radius: 50%;
  /* The 'all' keyword applies the transition to any animatable property that changes. */
  /* The cubic-bezier function creates a custom easing effect. */
  transition: all 0.7s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

/* On hover, the circle changes its size and background color. */
.circle-four:hover {
  transform: scale(1.5);
  background-color: #8e44ad; /* Darker purple */
}

Explanation: This code uses the all keyword to apply the transition to every animatable property that changes on hover. The cubic-bezier timing function provides a unique bounce effect, creating a more dynamic and engaging user experience than standard easing functions.


Example 5: Multiple Property Transitions with Different Timings

/* Styling for the animated panel. */
.panel-five {
  width: 200px;
  height: 150px;
  background-color: #f1c40f; /* Yellow background */
  border: 2px solid #f39c12; /* Orange border */
  padding: 10px;
  /* Multiple transitions are defined, separated by commas. */
  transition: background-color 1s ease-out, border 0.5s linear, transform 0.8s ease-in-out;
}

/* The panel transforms and changes its background and border on hover. */
.panel-five:hover {
  background-color: #f39c12; /* Orange background */
  border: 2px solid #e67e22; /* Darker orange border */
  transform: rotate(10deg); /* Rotates the panel */
}

Explanation: This example showcases how to define multiple, distinct transitions on a single element. The background-color, border, and transform properties each have their own duration and timing function, allowing for complex and layered animations.