🎬 CSS Animations

Keyframe Animations

CSS Animations

Create complex, multi-step animations with keyframes. Unlike transitions, animations can run automatically and loop indefinitely.

💻 Animation Basics

/* Define keyframes */
@keyframes slide-in {
    from {
        transform: translateX(-100%);
    }
    to {
        transform: translateX(0);
    }
}

/* Apply animation */
.element {
    animation: slide-in 1s;
}

/* With all properties */
.element {
    animation-name: slide-in;
    animation-duration: 1s;
    animation-timing-function: ease;
    animation-delay: 0s;
    animation-iteration-count: 1;
    animation-direction: normal;
    animation-fill-mode: none;
    animation-play-state: running;
}

🎯 Keyframes

/* from/to syntax */
@keyframes fade-in {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

/* Percentage syntax */
@keyframes bounce {
    0% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-30px);
    }
    100% {
        transform: translateY(0);
    }
}

/* Multiple steps */
@keyframes rainbow {
    0% { background: red; }
    20% { background: orange; }
    40% { background: yellow; }
    60% { background: green; }
    80% { background: blue; }
    100% { background: purple; }
}

/* Multiple properties per step */
@keyframes complex {
    0% {
        transform: translateX(0) scale(1);
        opacity: 1;
        background: red;
    }
    50% {
        transform: translateX(100px) scale(1.5);
        opacity: 0.5;
        background: blue;
    }
    100% {
        transform: translateX(0) scale(1);
        opacity: 1;
        background: red;
    }
}

/* Same styles for multiple keyframes */
@keyframes pulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.1);
    }
}

⚙️ Animation Properties

animation-name

/* Reference keyframes */
.element {
    animation-name: slide-in;
}

/* Multiple animations */
.element {
    animation-name: slide-in, fade-in;
}

/* No animation */
.element {
    animation-name: none;
}

animation-duration

/* Duration in seconds */
.fast {
    animation-duration: 0.5s;
}

.medium {
    animation-duration: 1s;
}

.slow {
    animation-duration: 3s;
}

/* Milliseconds */
.precise {
    animation-duration: 500ms;
}

/* Multiple durations */
.element {
    animation-name: slide-in, fade-in;
    animation-duration: 1s, 0.5s;
}

animation-timing-function

/* Predefined functions */
.ease {
    animation-timing-function: ease;  /* Default */
}

.linear {
    animation-timing-function: linear;
}

.ease-in {
    animation-timing-function: ease-in;
}

.ease-out {
    animation-timing-function: ease-out;
}

.ease-in-out {
    animation-timing-function: ease-in-out;
}

/* Custom cubic bezier */
.custom {
    animation-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

/* Steps (frame-by-frame) */
.steps {
    animation-timing-function: steps(10);
    animation-timing-function: steps(10, jump-start);
}

/* Per-keyframe timing */
@keyframes varied-timing {
    0% {
        transform: translateX(0);
        animation-timing-function: ease-in;
    }
    50% {
        transform: translateX(100px);
        animation-timing-function: ease-out;
    }
    100% {
        transform: translateX(0);
    }
}

animation-delay

/* Delay before animation starts */
.delayed {
    animation-delay: 1s;
}

.immediate {
    animation-delay: 0s;  /* Default */
}

/* Negative delay (start partway through) */
.head-start {
    animation-delay: -0.5s;
}

/* Staggered animations */
.item:nth-child(1) { animation-delay: 0s; }
.item:nth-child(2) { animation-delay: 0.1s; }
.item:nth-child(3) { animation-delay: 0.2s; }
.item:nth-child(4) { animation-delay: 0.3s; }

animation-iteration-count

/* Number of times to run */
.once {
    animation-iteration-count: 1;  /* Default */
}

.three-times {
    animation-iteration-count: 3;
}

/* Infinite loop */
.infinite {
    animation-iteration-count: infinite;
}

/* Fractional */
.half {
    animation-iteration-count: 0.5;  /* Runs halfway */
}

animation-direction

/* Direction of animation */
.normal {
    animation-direction: normal;  /* Default: 0% to 100% */
}

.reverse {
    animation-direction: reverse;  /* 100% to 0% */
}

.alternate {
    animation-direction: alternate;  /* Forward then back */
}

.alternate-reverse {
    animation-direction: alternate-reverse;  /* Back then forward */
}

/* Example: Smooth back-and-forth */
.pendulum {
    animation: swing 2s ease-in-out infinite alternate;
}

animation-fill-mode

/* How styles apply before/after animation */
.none {
    animation-fill-mode: none;  /* Default: no styles applied */
}

.forwards {
    animation-fill-mode: forwards;  /* Keep end state */
}

.backwards {
    animation-fill-mode: backwards;  /* Apply start state during delay */
}

.both {
    animation-fill-mode: both;  /* Both forwards and backwards */
}

/* Example: Fade in and stay */
.fade-in {
    opacity: 0;
    animation: fade-in 1s forwards;
}

@keyframes fade-in {
    to {
        opacity: 1;
    }
}

animation-play-state

/* Control animation playback */
.playing {
    animation-play-state: running;  /* Default */
}

.paused {
    animation-play-state: paused;
}

/* Example: Pause on hover */
.spinner {
    animation: spin 2s linear infinite;
}

.spinner:hover {
    animation-play-state: paused;
}

✍️ Animation Shorthand

/* Full syntax */
animation: name duration timing-function delay iteration-count direction fill-mode play-state;

/* Examples */
.element {
    animation: slide-in 1s ease 0s 1 normal forwards running;
}

/* Common shorthand */
.element {
    animation: slide-in 1s;
}

.element {
    animation: fade-in 0.5s ease-out forwards;
}

.element {
    animation: bounce 2s ease-in-out infinite;
}

/* Multiple animations */
.element {
    animation: 
        slide-in 1s ease-out,
        fade-in 0.5s linear;
}

🎨 Common Animations

Fade In/Out

/* Fade in */
@keyframes fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

.fade-in {
    animation: fade-in 0.5s;
}

/* Fade out */
@keyframes fade-out {
    from { opacity: 1; }
    to { opacity: 0; }
}

.fade-out {
    animation: fade-out 0.5s forwards;
}

Slide In/Out

/* Slide in from left */
@keyframes slide-in-left {
    from { transform: translateX(-100%); }
    to { transform: translateX(0); }
}

/* Slide in from right */
@keyframes slide-in-right {
    from { transform: translateX(100%); }
    to { transform: translateX(0); }
}

/* Slide in from top */
@keyframes slide-in-top {
    from { transform: translateY(-100%); }
    to { transform: translateY(0); }
}

/* Slide in from bottom */
@keyframes slide-in-bottom {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

Scale/Zoom

/* Zoom in */
@keyframes zoom-in {
    from { transform: scale(0); }
    to { transform: scale(1); }
}

/* Zoom out */
@keyframes zoom-out {
    from { transform: scale(1); }
    to { transform: scale(0); }
}

/* Pulse */
@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.1); }
}

Rotation

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

.spinner {
    animation: spin 2s linear infinite;
}

/* Wiggle */
@keyframes wiggle {
    0%, 100% { transform: rotate(0deg); }
    25% { transform: rotate(-10deg); }
    75% { transform: rotate(10deg); }
}

Bounce

/* Simple bounce */
@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-30px); }
}

/* Advanced bounce with easing */
@keyframes bounce-advanced {
    0%, 20%, 50%, 80%, 100% {
        transform: translateY(0);
    }
    40% {
        transform: translateY(-30px);
    }
    60% {
        transform: translateY(-15px);
    }
}

Shake

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-10px); }
    20%, 40%, 60%, 80% { transform: translateX(10px); }
}

🎯 Practical Examples

/* Loading spinner */
@keyframes spin {
    to { transform: rotate(360deg); }
}

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

/* Dots loading */
@keyframes dot-flashing {
    0%, 80%, 100% { opacity: 0; }
    40% { opacity: 1; }
}

.dot {
    width: 10px;
    height: 10px;
    background: #333;
    border-radius: 50%;
    display: inline-block;
    animation: dot-flashing 1.4s infinite;
}

.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }

/* Progress bar */
@keyframes progress {
    from { width: 0%; }
    to { width: 100%; }
}

.progress-bar {
    height: 4px;
    background: #007bff;
    animation: progress 2s ease-out;
}

/* Skeleton loading */
@keyframes skeleton-loading {
    0% { background-position: -200px 0; }
    100% { background-position: calc(200px + 100%) 0; }
}

.skeleton {
    background: linear-gradient(
        90deg,
        #f0f0f0 0px,
        #e0e0e0 40px,
        #f0f0f0 80px
    );
    background-size: 200px 100%;
    animation: skeleton-loading 1.5s infinite;
}

/* Floating animation */
@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-20px); }
}

.floating-element {
    animation: float 3s ease-in-out infinite;
}

/* Typing animation */
@keyframes typing {
    from { width: 0; }
    to { width: 100%; }
}

@keyframes blink {
    50% { border-color: transparent; }
}

.typewriter {
    overflow: hidden;
    border-right: 2px solid;
    white-space: nowrap;
    animation: 
        typing 3.5s steps(40, end),
        blink 0.75s step-end infinite;
}

/* Card flip */
@keyframes flip {
    from { transform: rotateY(0deg); }
    to { transform: rotateY(180deg); }
}

.card {
    transform-style: preserve-3d;
}

.card.flipped {
    animation: flip 0.6s forwards;
}

/* Notification slide */
@keyframes slide-in-notification {
    from {
        transform: translateX(400px);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

.notification {
    animation: slide-in-notification 0.5s ease-out;
}

/* Attention seeker */
@keyframes attention {
    0%, 100% {
        transform: scale(1);
    }
    10%, 20% {
        transform: scale(0.9) rotate(-3deg);
    }
    30%, 50%, 70%, 90% {
        transform: scale(1.1) rotate(3deg);
    }
    40%, 60%, 80% {
        transform: scale(1.1) rotate(-3deg);
    }
}

.attention {
    animation: attention 1s ease-in-out;
}

/* Wave */
@keyframes wave {
    0% { transform: rotate(0deg); }
    10% { transform: rotate(14deg); }
    20% { transform: rotate(-8deg); }
    30% { transform: rotate(14deg); }
    40% { transform: rotate(-4deg); }
    50% { transform: rotate(10deg); }
    60% { transform: rotate(0deg); }
    100% { transform: rotate(0deg); }
}

.wave {
    animation: wave 2s ease-in-out infinite;
    transform-origin: 70% 70%;
}

/* Gradient animation */
@keyframes gradient-shift {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

.animated-gradient {
    background: linear-gradient(
        -45deg,
        #ee7752, #e73c7e, #23a6d5, #23d5ab
    );
    background-size: 400% 400%;
    animation: gradient-shift 15s ease infinite;
}

/* Entrance animations */
@keyframes fade-in-up {
    from {
        opacity: 0;
        transform: translateY(30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.fade-in-up {
    animation: fade-in-up 0.6s ease-out;
}

/* Staggered list animation */
.list-item {
    opacity: 0;
    animation: fade-in-up 0.5s ease-out forwards;
}

.list-item:nth-child(1) { animation-delay: 0.1s; }
.list-item:nth-child(2) { animation-delay: 0.2s; }
.list-item:nth-child(3) { animation-delay: 0.3s; }
.list-item:nth-child(4) { animation-delay: 0.4s; }
.list-item:nth-child(5) { animation-delay: 0.5s; }

/* Heartbeat */
@keyframes heartbeat {
    0%, 100% { transform: scale(1); }
    10%, 30% { transform: scale(0.9); }
    20%, 40% { transform: scale(1.1); }
}

.heartbeat {
    animation: heartbeat 1.3s ease-in-out infinite;
}

💡 Best Practices

/* 1. Use transform and opacity for performance */
/* Good */
@keyframes slide {
    from { transform: translateX(-100%); }
    to { transform: translateX(0); }
}

/* Bad (causes layout recalculation) */
@keyframes slide-bad {
    from { left: -100%; }
    to { left: 0; }
}

/* 2. Use will-change for complex animations */
.animated {
    will-change: transform, opacity;
}

/* 3. Keep animations under 3 seconds */
/* Exception: Infinite looping animations */

/* 4. Respect user preferences */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
    }
}

/* 5. Use animation-fill-mode: forwards carefully */
/* It keeps element in end state */
.element {
    animation: fade-in 1s forwards;
}

/* 6. Avoid animating too many elements */
/* Bad: 100 elements animating simultaneously */
/* Good: Stagger animations or animate parent */

/* 7. Test on different devices */
/* Mobile devices have less power */

/* 8. Provide pause controls for long animations */
.animation-container {
    animation-play-state: running;
}

.animation-container.paused {
    animation-play-state: paused;
}

🎯 Key Takeaways