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

Linear Timing Function


The linear timing function specifies that an animation or transition progresses at a constant speed from start to end. There is no acceleration or deceleration, making the effect uniform and steady throughout its duration.


Example 1: Linear Motion

/* This div will be our animated element. */
.box {
  width: 100px;
  height: 100px;
  background-color: #3498db;
  position: relative;
  /* Define the animation properties */
  animation-name: slide-right;
  animation-duration: 4s;
  /* Set the timing function to linear for constant speed */
  animation-timing-function: linear;
  animation-iteration-count: infinite;
}

/* Define the keyframes for the movement */
@keyframes slide-right {
  from {
    left: 0;
  }
  to {
    left: 300px;
  }
}

Explanation This code creates a blue square that moves 300 pixels to the right. The animation-timing-function: linear; ensures the box travels at a consistent, even speed without speeding up or slowing down.


Example 2: Constant Rotation

/* A simple loading spinner element. */
.spinner {
  width: 50px;
  height: 50px;
  border: 5px solid #f3f3f3;
  border-top: 5px solid #3498db;
  border-radius: 50%;
  /* Define the animation properties */
  animation-name: spin;
  animation-duration: 1.5s;
  /* The linear timing function creates a smooth, continuous spin. */
  animation-timing-function: linear;
  animation-iteration-count: infinite;
}

/* Define the rotation keyframes */
@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

Explanation The linear value causes the spinner to rotate at a steady pace, which is ideal for creating a seamless and non-jarring loading animation. The rotation speed never changes.


Example 3: Uniform Opacity Fade

/* Style for an image that will fade in on hover. */
.fade-in-image {
  width: 200px;
  opacity: 0.5;
  /* Define the transition for the opacity property */
  transition-property: opacity;
  transition-duration: 1s;
  /* Use linear to ensure the fade happens at a constant rate. */
  transition-timing-function: linear;
}

/* On hover, the image becomes fully opaque. */
.fade-in-image:hover {
  opacity: 1;
}

Explanation When you hover over the image, its opacity changes from 0.5 to 1. The transition-timing-function: linear; ensures the image becomes clearer at a constant, predictable rate.


Example 4: Steady Background Color Change

/* A button that changes color on hover. */
.color-change-btn {
  padding: 15px 30px;
  border: none;
  background-color: #2ecc71;
  color: white;
  /* Specify the transition effect for the background-color */
  transition: background-color 2s linear;
}

/* The new background color on hover. */
.color-change-btn:hover {
  background-color: #e74c3c;
}

Explanation This code transitions a button's background color from green to red upon hovering. Using linear, the color shifts at a steady, even pace through all the intermediate shades.


Example 5: Combined Linear Transitions

/* An element that will scale and fade out simultaneously. */
.grow-and-fade {
  width: 100px;
  height: 100px;
  background-color: #9b59b6;
  opacity: 1;
  /* Apply linear transition to both transform and opacity properties. */
  transition: transform 2s linear, opacity 2s linear;
}

/* Trigger the transition on hover. */
.grow-and-fade:hover {
  transform: scale(1.5);
  opacity: 0;
}

Explanation On hover, this purple square both grows in size and fades out. By applying a linear timing function to both transform and opacity, both effects proceed at a constant, synchronized speed.