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 Property


The CSS transition-property is a fundamental aspect of creating smooth animations. It specifies which CSS property or properties will be animated when their values change, such as on a hover effect. By default, its value is all, meaning any animatable property will transition; however, you can list specific properties to optimize performance and control the effect.


Example 1: Transitioning a Single Property

/* This class defines the initial state of our element. */
.box {
  width: 150px;
  height: 150px;
  background-color: #3498db;
  transition-duration: 0.5s;
  /* Only the 'width' property will animate over the transition duration. */
  transition-property: width;
}

/* On hover, the width changes, triggering the specified transition. */
.box:hover {
  width: 300px;
  background-color: #e74c3c; /* This change happens instantly. */
}

Explanation This code sets up a transition exclusively for the width property. When you hover over the .box element, only its width will animate smoothly to 300px, while the background color changes instantly because it is not included in transition-property.


Example 2: Transitioning Multiple Properties

/* This class sets up the initial state. */
.button {
  padding: 15px 30px;
  background-color: #2ecc71;
  color: white;
  border: none;
  transition-duration: 0.4s;
  /* We specify two properties to animate: background-color and color. */
  transition-property: background-color, color;
}

/* On hover, both specified properties will animate smoothly. */
.button:hover {
  background-color: #27ae60;
  color: #f1c40f;
}

Explanation Here, transition-property is given a comma-separated list of properties: background-color and color. This tells the browser to animate both the button's background and text color simultaneously when the user hovers over it.


Example 3: Using the 'all' Keyword

/* Initial state of the element. */
.card {
  width: 200px;
  padding: 20px;
  background-color: white;
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
  transform: scale(1);
  transition-duration: 0.3s;
  /* 'all' is the default, animating any animatable property that changes. */
  transition-property: all;
}

/* On hover, all changing properties will animate. */
.card:hover {
  transform: scale(1.05);
  box-shadow: 0 8px 16px rgba(0,0,0,0.2);
}

Explanation The all keyword is a shorthand to animate every property that can be transitioned. In this example, both the transform (scaling the card up) and the box-shadow will animate smoothly on hover, creating a fluid lifting effect.


Example 4: Using the 'none' Keyword

/* Initial styling for the input field. */
.input-field {
  border: 2px solid #ccc;
  padding: 10px;
  transition-duration: 0.5s; /* Duration is set, but... */
  /* 'none' explicitly prevents any property from animating. */
  transition-property: none;
}

/* On focus, the changes are applied instantly without animation. */
.input-field:focus {
  border-color: #3498db;
  box-shadow: 0 0 5px rgba(52, 152, 219, 0.5);
}

Explanation By setting transition-property to none, we disable all transitions for the element. Even though a transition-duration is defined, the border-color and box-shadow will change instantly when the input field receives focus.


Example 5: Transitioning the 'transform' Property

/* Initial state of the icon. */
.icon {
  font-size: 40px;
  color: #9b59b6;
  transition-duration: 0.4s;
  /* We only want the transform (rotation) to be animated. */
  transition-property: transform;
}

/* On hover, the icon rotates smoothly. */
.icon:hover {
  transform: rotate(360deg);
}

Explanation This example isolates the transform property for animation. When hovering over the icon, it will complete a full 360-degree rotation smoothly, demonstrating how to animate movement or orientation changes specifically.


Example 6: Animating Opacity and Visibility

/* Initially, the tooltip is hidden. */
.tooltip {
  opacity: 0;
  visibility: hidden;
  background-color: #333;
  color: white;
  padding: 5px;
  border-radius: 4px;
  transition-duration: 0.3s;
  /* We specify 'opacity' for a fade effect. */
  transition-property: opacity;
}

/* The parent container for the tooltip. */
.tooltip-container:hover .tooltip {
  opacity: 1;
  visibility: visible;
}

Explanation This code creates a fade-in effect for a tooltip by transitioning the opacity property. While visibility changes instantly, the smooth change in opacity from 0 to 1 makes the tooltip appear gradually on hover.


Example 7: Animating Font Size

/* Initial style for the link. */
.nav-link {
  color: #16a085;
  font-size: 18px;
  text-decoration: none;
  transition-duration: 0.25s;
  /* Animate only the font-size property. */
  transition-property: font-size;
}

/* Make the text larger on hover. */
.nav-link:hover {
  font-size: 22px;
  color: #1abc9c; /* This color change will be instant. */
}

Explanation This example focuses the transition on the font-size property. When a user hovers over the navigation link, the text will grow smoothly, providing clear visual feedback, while the color change is immediate.