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 Duration


The transition-duration property is a fundamental part of CSS transitions that defines the length of time an animation should take to complete. You can specify the duration in seconds (s) or milliseconds (ms). This property is crucial for creating smooth and paced visual effects on a webpage.


Example 1: Basic Duration on Hover

/* Style for the element */
.box {
  width: 100px;
  height: 100px;
  background-color: steelblue;
  /* Define the duration for the transition */
  transition-duration: 2s;
}

/* Change the background color on hover */
.box:hover {
  background-color: orange;
}

Explanation: This code applies a 2-second transition duration to the .box element. When you hover over the box, the background color will gradually change from steelblue to orange over the course of two seconds.


Example 2: Milliseconds for Faster Transitions

/* Style for the button */
.button {
  padding: 10px 20px;
  border: 1px solid black;
  /* Use milliseconds for a quick transition */
  transition-duration: 300ms;
}

/* Scale the button up on hover */
.button:hover {
  transform: scale(1.1);
  background-color: lightgray;
}

Explanation: Here, the transition-duration is set to 300 milliseconds. This creates a much faster, almost instant, scaling and background color effect when the user hovers over the button.


Example 3: No Transition with Zero Duration

/* Style for the link */
a {
  color: blue;
  /* Setting duration to 0s means the change is immediate */
  transition-duration: 0s;
}

/* Change color on hover without a transition */
a:hover {
  color: red;
}

Explanation: By setting transition-duration to 0s, you effectively disable the transition effect. The color of the link will change from blue to red instantly upon hover, with no gradual animation.


Example 4: Using the 'transition' Shorthand Property

/* Style for the interactive panel */
.panel {
  opacity: 0.5;
  /* Shorthand: property name, then duration */
  transition: opacity 1.5s;
}

/* Make the panel fully opaque on hover */
.panel:hover {
  opacity: 1;
}

Explanation: This example uses the transition shorthand property to set both the transition-property (opacity) and the transition-duration (1.5s) in a single declaration. This is a common and more concise way to write transitions.


Example 5: Focus Transition for Form Inputs

/* Style for the input field */
input[type="text"] {
  border: 2px solid #ccc;
  /* Set a half-second duration for the focus effect */
  transition-duration: 0.5s;
}

/* Change border color when the input is focused */
input[type="text"]:focus {
  border-color: dodgerblue;
}

Explanation: This code enhances user experience on forms. When a user clicks into the text input, the transition-duration of 0.5s creates a smooth color change for the border, indicating the field is active.


Example 6: Different Durations for Multiple Properties

/* Style for the shape */
.shape {
  width: 100px;
  height: 100px;
  background-color: purple;
  /* Assign different durations to different properties */
  transition-property: transform, background-color;
  transition-duration: 2s, 500ms;
}

/* Apply transformations on hover */
.shape:hover {
  transform: rotate(180deg);
  background-color: gold;
}

Explanation: You can set multiple durations for different properties. In this case, the rotation (transform) takes 2 seconds to complete, while the background-color change happens more quickly in 500 milliseconds.


Example 7: Active State Transition on a Button

/* Style for a clickable button */
.click-me {
  background-color: #28a745;
  color: white;
  padding: 15px;
  border-radius: 5px;
  /* A quick duration for the active (click) state */
  transition-duration: 0.1s;
}

/* Style for when the button is being pressed */
.click-me:active {
  transform: translateY(2px);
  background-color: #218838;
}

Explanation: This example applies a very short transition-duration of 0.1s to a button's active state. This provides immediate visual feedback to the user when they click down on the button, making the interface feel more responsive.