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

Cubic Bezier Function


The cubic-bezier() function is a powerful CSS timing function that allows for the creation of custom acceleration curves for animations and transitions. It is defined by four control points, P0, P1, P2, and P3. P0 and P3 are always set to (0,0) and (1,1) respectively, representing the start and end of the animation. The function's arguments specify the coordinates of P1 and P2, cubic-bezier(x1, y1, x2, y2), which dictate the shape of the curve and thus the speed of the animation over its duration. This enables fine-grained control over the easing, allowing for unique and natural-feeling motion.


Example 1: Ease-in-out-like Custom Curve

<!DOCTYPE html>
<html>
<head>
<title>Cubic Bezier Example 1</title>
<style>
  .box {
    width: 100px;
    height: 100px;
    background-color: #3498db;
    transition: transform 2s cubic-bezier(0.42, 0, 0.58, 1); /* Custom ease-in-out curve */
  }

  .box:hover {
    transform: translateX(400px); /* Move the box on hover */
  }
</style>
</head>
<body>

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

</body>
</html>

Explanation: This cubic-bezier(0.42, 0, 0.58, 1) function creates a custom timing that is very similar to the standard ease-in-out keyword. The animation starts slowly, accelerates through the middle part of its duration, and then slows down again towards the end, providing a smooth and symmetrical transition.


Example 2: Fast Start, Slow End

<!DOCTYPE html>
<html>
<head>
<title>Cubic Bezier Example 2</title>
<style>
  .circle {
    width: 80px;
    height: 80px;
    background-color: #e74c3c;
    border-radius: 50%;
    transition: opacity 2s cubic-bezier(0.1, 0.9, 0.9, 0.1); /* Fast start, slow end curve */
  }

  .circle:hover {
    opacity: 0; /* Fade out the circle on hover */
  }
</style>
</head>
<body>

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

</body>
</html>

Explanation: The curve defined by cubic-bezier(0.1, 0.9, 0.9, 0.1) creates a very energetic effect. The animation begins extremely quickly and then decelerates dramatically towards the end. This can be used for effects that need to grab attention immediately and then settle down.


Example 3: The "Bouncing" Effect

<!DOCTYPE html>
<html>
<head>
<title>Cubic Bezier Example 3</title>
<style>
  .shape {
    width: 120px;
    height: 120px;
    background-color: #9b59b6;
    transition: transform 1.5s cubic-bezier(0.68, -0.55, 0.27, 1.55); /* Bouncing effect curve */
  }

  .shape:hover {
    transform: scale(1.5); /* Scale up the shape on hover */
  }
</style>
</head>
<body>

<div class="shape"></div>

</body>
</html>

Explanation: By providing y-values that are outside the typical 0 to 1 range, such as in cubic-bezier(0.68, -0.55, 0.27, 1.55), we can create a "bouncing" or "overshooting" effect. The animated property will go beyond its final value before settling back, which simulates a physical bounce.


Example 4: Gentle Ease-out

<!DOCTYPE html>
<html>
<head>
<title>Cubic Bezier Example 4</title>
<style>
  .loader {
    width: 0px;
    height: 10px;
    background-color: #2ecc71;
    animation: load 3s cubic-bezier(0.25, 0.1, 0.25, 1) forwards; /* Gentle ease-out loading bar */
  }

  @keyframes load {
    to {
      width: 100%; /* Animate width to 100% */
    }
  }
</style>
</head>
<body>

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

</body>
</html>

Explanation: The function cubic-bezier(0.25, 0.1, 0.25, 1) provides a custom curve that starts relatively quickly and then gently eases to a stop. This is a common and visually pleasing effect for elements like loading bars or progress indicators, making the end of the animation feel smooth and polished.


Example 5: Linear with a Slight Ease

<!DOCTYPE html>
<html>
<head>
<title>Cubic Bezier Example 5</title>
<style>
  .mover {
    width: 50px;
    height: 50px;
    background-color: #f1c40f;
    position: relative;
    animation: move-right 4s cubic-bezier(0, 0, 1, 1) infinite alternate; /* This is equivalent to 'linear' */
  }

  @keyframes move-right {
    from {
      left: 0px;
    }
    to {
      left: 300px;
    }
  }
</style>
</head>
<body>

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

</body>
</html>

Explanation: The cubic-bezier(0, 0, 1, 1) timing function is mathematically equivalent to the linear keyword. It creates an animation that proceeds at a constant speed from start to finish with no acceleration or deceleration, useful for continuous, steady movements like marquees or background scrolls.