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

CSS Animation Duration


The animation-duration property is a fundamental part of CSS animations. It specifies the length of time that a single cycle of an animation should take to complete. This value is defined in seconds (s) or milliseconds (ms).


Example 1: Simple Color Transition

/* style.css */
@keyframes colorChange {
  from { background-color: lightblue; }
  to { background-color: steelblue; }
}

.box {
  width: 100px;
  height: 100px;
  background-color: lightblue;
  /* This animation will take 4 seconds to complete one cycle */
  animation-name: colorChange;
  animation-duration: 4s;
  animation-iteration-count: infinite;
}

HTML

<div class="box"></div>

Explanation This code defines a keyframe animation named colorChange that transitions an element's background color. The .box class applies this animation and sets the animation-duration to 4s, meaning it takes four seconds to fade from lightblue to steelblue.


Example 2: Element Movement

/* style.css */
@keyframes moveRight {
  0% { transform: translateX(0); }
  100% { transform: translateX(200px); }
}

.mover {
  width: 50px;
  height: 50px;
  background-color: tomato;
  /* A very fast animation duration of 500 milliseconds */
  animation-name: moveRight;
  animation-duration: 0.5s; /* Equivalent to 500ms */
  animation-direction: alternate;
  animation-iteration-count: infinite;
}

HTML

<div class="mover"></div>

Explanation Here, the moveRight animation moves an element horizontally across the screen. The animation-duration is set to 0.5s, creating a very quick movement over half a second. The animation will move to the right and then alternate back to the left.


Example 3: Pulsing Effect

/* style.css */
@keyframes pulse {
  50% { transform: scale(1.1); }
}

.circle {
  width: 80px;
  height: 80px;
  background-color: gold;
  border-radius: 50%;
  /* The pulse effect completes one full cycle in 2 seconds */
  animation-name: pulse;
  animation-duration: 2s;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
}

HTML

<div class="circle"></div>

Explanation This example creates a "pulsing" effect by scaling an element up and then back down. The animation-duration of 2s controls the speed of the pulse, making one complete pulse (scaling up and returning to original size) last for two seconds.


Example 4: Slow Rotation

/* style.css */
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.loader {
  width: 60px;
  height: 60px;
  border: 5px solid #f3f3f3;
  border-top: 5px solid #3498db;
  border-radius: 50%;
  /* A slow and steady 10-second rotation for a loading spinner */
  animation-name: spin;
  animation-duration: 10s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}

HTML

<div class="loader"></div>

Explanation The .loader element continuously rotates 360 degrees, simulating a loading indicator. By setting the animation-duration to a lengthy 10s, we create a slow and steady spinning motion, which is often desirable for loaders.


Example 5: Multiple Durations (Conceptual)

/* style.css */
@keyframes slideAndFade {
  0% { transform: translateX(0); opacity: 1; }
  50% { transform: translateX(100px); opacity: 0.5; }
  100% { transform: translateX(0); opacity: 1; }
}

.multi {
  width: 70px;
  height: 70px;
  background-color: mediumpurple;
  /* You can't set multiple durations for one animation name directly */
  /* This example uses a single duration for the entire keyframe sequence */
  animation-name: slideAndFade;
  animation-duration: 3s;
  animation-iteration-count: infinite;
}

HTML

<div class="multi"></div>

Explanation This example demonstrates a combined animation of sliding and fading. While you apply a single animation-duration of 3s to the entire animation sequence, the timing of different effects is controlled by the percentages within the @keyframes rule itself. The full slideAndFade animation takes three seconds to complete its cycle.