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

Animation Timing Function


The animation-timing-function CSS property sets the speed curve of an animation. It dictates how an animation progresses over time, allowing it to accelerate, decelerate, or maintain a constant speed. This control over the animation's pace is crucial for creating smooth, natural, and engaging user experiences.


Example 1: ease

/* This is a basic animation setup */
.box {
  width: 100px;
  height: 100px;
  background-color: #3498db;
  position: relative;
  animation-name: move;
  animation-duration: 4s;
  animation-iteration-count: infinite;
  /* ease: The animation starts slow, accelerates, then slows down at the end. */
  animation-timing-function: ease;
}

@keyframes move {
  from {left: 0px;}
  to {left: 300px;}
}

Explanation

The ease value is the default. It creates a realistic-looking motion by starting the animation slowly, speeding it up in the middle, and then slowing it down again before it concludes.


Example 2: linear

/* This is a basic animation setup */
.box {
  width: 100px;
  height: 100px;
  background-color: #e74c3c;
  position: relative;
  animation-name: move;
  animation-duration: 4s;
  animation-iteration-count: infinite;
  /* linear: The animation maintains a constant speed from start to finish. */
  animation-timing-function: linear;
}

@keyframes move {
  from {left: 0px;}
  to {left: 300px;}
}

Explanation

With the linear timing function, the animation progresses at a single, consistent speed throughout its entire duration. This is ideal for effects like continuous rotations or background scrolling.


Example 3: ease-in

/* This is a basic animation setup */
.box {
  width: 100px;
  height: 100px;
  background-color: #2ecc71;
  position: relative;
  animation-name: move;
  animation-duration: 4s;
  animation-iteration-count: infinite;
  /* ease-in: The animation begins slowly and gradually accelerates until it ends. */
  animation-timing-function: ease-in;
}

@keyframes move {
  from {left: 0px;}
  to {left: 300px;}
}

Explanation

The ease-in value causes the animation to have a slow start, gradually picking up speed until the end. This effect can be used to simulate an object accelerating into motion.


Example 4: ease-out

/* This is a basic animation setup */
.box {
  width: 100px;
  height: 100px;
  background-color: #f1c40f;
  position: relative;
  animation-name: move;
  animation-duration: 4s;
  animation-iteration-count: infinite;
  /* ease-out: The animation starts at full speed and then decelerates to a stop. */
  animation-timing-function: ease-out;
}

@keyframes move {
  from {left: 0px;}
  to {left: 300px;}
}

Explanation

Contrary to ease-in, the ease-out timing function makes the animation begin quickly and then gradually slow down as it approaches its final state. This is effective for elements that are arriving or coming to a rest.


Example 5: ease-in-out

/* This is a basic animation setup */
.box {
  width: 100px;
  height: 100px;
  background-color: #9b59b6;
  position: relative;
  animation-name: move;
  animation-duration: 4s;
  animation-iteration-count: infinite;
  /* ease-in-out: The animation starts slowly, accelerates, and then slows down at the end. */
  animation-timing-function: ease-in-out;
}

@keyframes move {
  from {left: 0px;}
  to {left: 300px;}
}

Explanation

The ease-in-out value provides a slow start and a slow end, similar to ease, but with a more symmetrical acceleration and deceleration curve. It is often perceived as a smoother and more gentle effect.


Example 6: cubic-bezier()

/* This is a basic animation setup */
.box {
  width: 100px;
  height: 100px;
  background-color: #1abc9c;
  position: relative;
  animation-name: move;
  animation-duration: 4s;
  animation-iteration-count: infinite;
  /* cubic-bezier(): Defines a custom timing curve with four control points. */
  animation-timing-function: cubic-bezier(0.1, 0.7, 1.0, 0.1);
}

@keyframes move {
  from {left: 0px;}
  to {left: 300px;}
}

Explanation

The cubic-bezier() function offers complete control over the animation's speed by allowing you to define your own timing curve. The four values specify the P1 and P2 points of the curve, enabling a wide range of custom acceleration profiles.


Example 7: steps()

/* This is a basic animation setup */
.box {
  width: 100px;
  height: 100px;
  background-color: #e67e22;
  position: relative;
  animation-name: move;
  animation-duration: 4s;
  animation-iteration-count: infinite;
  /* steps(): Divides the animation into a specified number of discrete steps. */
  animation-timing-function: steps(5, end);
}

@keyframes move {
  from {left: 0px;}
  to {left: 300px;}
}

Explanation

The steps() function creates a stepped, or jerky, animation by breaking it into a defined number of intervals. This is perfect for creating sprite sheet animations or effects that should not be smooth.