← Back to all tutorials

Keyframes

Create multi-step animations with @keyframes — define animation stages and apply them to elements.

Keyframes

Transitions handle simple A → B changes. For multi-step, complex animations, you need @keyframes. Keyframe animations let you define multiple stages and animate through them automatically.

Defining a Keyframe Animation

@keyframes slideIn {
    from {
        transform: translateX(-100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

Applying the Animation

.element {
    animation-name: slideIn;
    animation-duration: 0.6s;
}

from/to vs Percentages

/* from/to — simple two-stage animation */
@keyframes fadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Percentages — multi-stage animation */
@keyframes bounce {
    0%   { transform: translateY(0); }
    30%  { transform: translateY(-30px); }
    50%  { transform: translateY(0); }
    70%  { transform: translateY(-15px); }
    100% { transform: translateY(0); }
}

from = 0% and to = 100%. Use percentages when you need more than two stages.

Animation Properties

PropertyWhat It ControlsExample Values
animation-nameWhich @keyframes to useslideIn, bounce
animation-durationHow long one cycle takes0.5s, 2s
animation-delayWait before starting0.3s, 1s
animation-timing-functionSpeed curveease, linear
animation-iteration-countHow many times to repeat1, 3, infinite
animation-directionForwards, backwards, or alternatingnormal, alternate
animation-fill-modeState before/after animationforwards, both
animation-play-statePause or playrunning, paused

Practical Examples

Fade In & Slide Up

@keyframes fadeSlideUp {
    from {
        opacity: 0;
        transform: translateY(30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.card {
    animation: fadeSlideUp 0.5s ease;
}

Pulse Effect

@keyframes pulse {
    0%   { transform: scale(1); }
    50%  { transform: scale(1.05); }
    100% { transform: scale(1); }
}

.notification-badge {
    animation: pulse 2s ease-in-out infinite;
}

Loading Spinner

@keyframes spin {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #eee;
    border-top-color: #3498db;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

Transitions vs Keyframes

FeatureTransitionsKeyframes
StagesTwo (start → end)Unlimited (0% → 100%)
TriggerRequires state changeRuns automatically or on class add
LoopingNo built-in loopinfinite looping
ComplexitySimpleComplex multi-stage
Use caseHover effects, togglesLoading animations, entrance effects

Key Takeaways

  • @keyframes define named animations with multiple stages
  • Use from/to for simple two-stage or percentages for multi-stage
  • Apply with animation-name and animation-duration at minimum
  • Keyframes can loop, alternate, and be paused — far more flexible than transitions
  • Ideal for entrance effects, loading spinners, and continuous animations