Course

Web Development
  • 1.1 Introduction to the Web
  • 2.1 HTML Basics
  • 2.2 Text & Content
  • 2.3 Layout & Grouping
  • 2.4 Lists & Tables
  • 2.5 Media & Embedding
  • 2.6 Forms
  • 2.7 Semantic HTML
  • 2.8 Accessibility & SEO
  • 3.1 CSS Introduction
  • 3.2 CSS Syntax
  • 3.3 CSS Selectors
  • 3.4 Typography
  • 3.5 Colors & Backgrounds
  • 3.6 Text Styling
  • 3.7 Box Model
  • 3.8 Units
  • 3.9 Display & Positioning
  • 3.10 Layout Systems
  • 3.11 Responsive Design
  • 3.12 Animations
  • 3.13 Modern CSS
  • 3.14 Performance & Accessibility
  • 3.15 Tailwind CSS Basics
  • 4.1 Introduction to JavaScript
  • 4.2 Variables & Scope
  • 4.3 Data Types
  • 4.4 Type Conversion
  • 4.5 Operators
  • 4.6 Control Flow
  • 4.7 Loops
  • 4.8 Functions
  • 4.9 Advanced Functions
  • 4.10 Objects & Arrays
  • 4.11 The this Keyword

3.12 Animations

Updated Jul 20, 2026

3.12 Animations

Updated Jul 20, 2026

Animations bring websites to life by adding movement and visual effects to elements. They make websites feel more interactive, responsive, and modern.

CSS provides four major animation-related features:

  1. Transform
  2. Transition
  3. Keyframes
  4. Animation

These features are commonly used for:

  • Buttons
  • Cards
  • Navigation Menus
  • Modals
  • Hover Effects
  • Loading Indicators

Why Use Animations?

Animations improve:

  • Better User Experience (UX)
  • Visual Feedback
  • Interactivity
  • Modern Design
  • Smooth User Interfaces

Example:

Without Animation

Button

With Animation

Hover
   ↓
Button Grows Smoothly

Transform

The transform property changes an element's appearance without affecting the document layout.

Using transform, you can:

  • Move elements
  • Rotate elements
  • Scale elements
  • Skew elements

Translate

The translate() function moves an element.

Syntax

transform: translate(x, y);

Example

.box {
    transform: translate(50px, 20px);
}

Result:

  • Move Right → 50px
  • Move Down → 20px

Horizontal Move

transform: translateX(100px);

Vertical Move

transform: translateY(50px);

Scale

The scale() function changes the size of an element.

Syntax

transform: scale(value);

Example

transform: scale(1.2);

Result:

  • 20% Larger

Shrink

transform: scale(0.8);

Result:

  • 20% Smaller

Rotate

The rotate() function rotates an element.

Syntax

transform: rotate(angle);

Example

transform: rotate(45deg);

Result:

  • Rotated 45°

Full Rotation

transform: rotate(360deg);

Skew

The skew() function tilts an element.

Syntax

transform: skew(x-angle, y-angle);

Example

transform: skew(20deg);

Result:

  • Slanted Appearance

Multiple Transforms

Multiple transform functions can be combined.

transform: translateY(-10px) scale(1.05);

Example

.card:hover {

    transform: translateY(-10px) scale(1.02);

}

This creates a popular card hover effect.


Transition

Transitions create smooth changes between two states.

Without Transition

Hover
 ↓
Instant Change

With Transition

Hover
 ↓
Smooth Change

Syntax

transition: property duration;

Example

button {

    transition: background-color 0.3s;

}

Hover Example

button {

    background: blue;
    transition: background 0.3s;

}

button:hover {

    background: red;

}

Result:

Blue
 ↓
Smoothly turns Red

Transition Components

A transition can include four values.

transition:
property
duration
timing-function
delay;

Example

transition: all 0.3s ease 0s;

Duration

Controls how long the transition takes.

transition-duration: 0.5s;

or

transition: all 500ms;

Timing Functions

Timing functions control the speed of an animation.

Linear

transition-timing-function: linear;

Constant speed.


Ease

transition-timing-function: ease;

Starts slowly, speeds up, then slows down.

(Default)


Ease-In

transition-timing-function: ease-in;

Starts slowly.


Ease-Out

transition-timing-function: ease-out;

Ends slowly.


Ease-In-Out

transition-timing-function: ease-in-out;

Slow start and slow end.


Common Hover Example

.card {

    transition: transform 0.3s ease;

}

.card:hover {

    transform: translateY(-10px);

}

Keyframes

Keyframes define the stages of an animation.

They describe:

Start
   ↓
Middle
   ↓
End

Syntax

@keyframes animation-name {

}

Basic Example

@keyframes fadeIn {

    from {

        opacity: 0;

    }

    to {

        opacity: 1;

    }

}

Alternative Syntax

@keyframes fadeIn {

    0% {

        opacity: 0;

    }

    100% {

        opacity: 1;

    }

}

Multiple Stages

@keyframes pulse {

    0% {

        transform: scale(1);

    }

    50% {

        transform: scale(1.1);

    }

    100% {

        transform: scale(1);

    }

}

Animation

The animation property applies a keyframe animation to an element.


Syntax

animation: name duration;

Example

.box {

    animation: fadeIn 1s;

}

Animation Properties

Property Purpose
animation-name Keyframe name
animation-duration Animation speed
animation-delay Delay before starting
animation-iteration-count Number of repetitions
animation-direction Direction of animation
animation-fill-mode Keeps styles before or after animation
animation-timing-function Motion style

Animation Duration

animation-duration: 2s;

Animation lasts 2 seconds.


Animation Delay

animation-delay: 1s;

Waits 1 second before starting.


Animation Iteration Count

Run once

animation-iteration-count: 1;

Run forever

animation-iteration-count: infinite;

Animation Direction

Normal

animation-direction: normal;

Reverse

animation-direction: reverse;

Alternate

animation-direction: alternate;

Runs forward, then backward.


Animation Fill Mode

Controls styles before and after the animation.

Forwards

animation-fill-mode: forwards;

Keeps the final state.

Backwards

animation-fill-mode: backwards;

Applies starting styles immediately.


Fade In Example

@keyframes fadeIn {

    from {

        opacity: 0;

    }

    to {

        opacity: 1;

    }

}

.hero {

    animation: fadeIn 1s ease;

}

Slide Up Example

@keyframes slideUp {

    from {

        transform: translateY(50px);
        opacity: 0;

    }

    to {

        transform: translateY(0);
        opacity: 1;

    }

}

Apply Animation

.card {

    animation: slideUp 0.5s ease;

}

Spinning Loader

@keyframes spin {

    from {

        transform: rotate(0deg);

    }

    to {

        transform: rotate(360deg);

    }

}

.loader {

    animation: spin 1s linear infinite;

}

Pulse Animation

@keyframes pulse {

    0% {

        transform: scale(1);

    }

    50% {

        transform: scale(1.1);

    }

    100% {

        transform: scale(1);

    }

}

.button {

    animation: pulse 2s infinite;

}

Complete Example

HTML

<button class="btn">
    Click Me
</button>

CSS

.btn {

    background: blue;
    color: white;

    transition: transform 0.3s ease;

}

.btn:hover {

    transform: scale(1.05);

}

Real-World Card Hover

.card {

    transition: all 0.3s ease;

}

.card:hover {

    transform: translateY(-8px);

    box-shadow: 0 10px 30px rgba(0,0,0,0.1);

}

Commonly used in:

  • SaaS Dashboards
  • Landing Pages
  • E-commerce Websites

Modern Animation Best Practices

Prefer Transform

Good

transform: translateY(-10px);

Avoid

top: -10px;

Transforms are more performant because they don't trigger layout recalculations.


Keep Animations Fast

Recommended durations:

  • 0.2s
  • 0.3s
  • 0.5s

Avoid long animations like 5s or 10s for user interactions.


Use Ease Timing

transition: all 0.3s ease;

This creates natural-looking motion.


Avoid Overusing Animations

Animations should improve the user experience, not distract users.


Summary

Feature Purpose
transform Move, rotate, scale, or skew elements
translate() Move an element
scale() Resize an element
rotate() Rotate an element
skew() Tilt an element
transition Smooth changes between states
@keyframes Define animation stages
animation Apply keyframes
animation-duration Animation speed
animation-delay Delay before starting
animation-iteration-count Repeat animation

Key Takeaway

Modern CSS animations usually combine transform and transition for smooth interactions.

transform: translateY(-10px);

transition: all 0.3s ease;

For more advanced effects, use @keyframes and the animation property.

@keyframes fadeIn {

    from {

        opacity: 0;

    }

    to {

        opacity: 1;

    }

}

.hero {

    animation: fadeIn 1s ease;

}

Remember these four concepts:

  • Transform → Moves or changes an element.
  • Transition → Makes changes smooth.
  • Keyframes → Defines the animation timeline.
  • Animation → Plays the keyframe animation.

Together, they form the foundation of modern, interactive web interfaces.

3.11 Responsive Design3.13 Modern CSS