#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS3 Animations

CSS animations let an element change its styles gradually through a sequence of steps you define, without any JavaScript. You use them for loading spinners, attention-grabbing pulses, slide-in banners and any motion that should run on its own, loop, or have more than a simple start and end state.

Defining the motion with @keyframes

An animation has two parts. First, a @keyframes rule describes what the element looks like at different points, using percentages or the words from (0%) and to (100%). Second, you attach that rule to an element with the animation properties.

@keyframes slide-in {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.toast {
  animation: slide-in 0.4s ease-out both;
}

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

.spinner {
  width: 32px;
  height: 32px;
  border: 4px solid #ccc;
  border-top-color: #0d6efd;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

The spinner only defines the end state. When a keyframe is missing, the browser uses the element's normal styles for it.

The animation properties explained

PropertyWhat it controls
animation-nameWhich @keyframes rule to run.
animation-durationHow long one cycle takes, such as 0.5s.
animation-timing-functionThe speed curve: ease, linear, ease-in-out or cubic-bezier().
animation-delayWait time before starting.
animation-iteration-countA number or infinite.
animation-directionnormal, reverse or alternate for back-and-forth.
animation-fill-modeWhether first or last keyframe styles apply before or after running.

The animation shorthand accepts these in almost any order. The one rule is that the first time value is the duration and the second is the delay.

Why the element jumps back at the end

A very common surprise: an element fades in, then snaps back to invisible when the animation finishes. That happens because, by default, keyframe styles apply only while the animation runs. Setting animation-fill-mode: forwards keeps the final keyframe, and both also applies the first keyframe during any delay so the element does not flash before starting.

Keeping animations smooth

Animating transform and opacity is cheap for the browser, since it can usually handle them on the compositor without recalculating layout. Animating width, height, top or margin forces layout work on every frame and can stutter on slower phones. To move something, prefer translate; to grow it, prefer scale.

Respecting users who prefer less motion

Some people feel dizzy or distracted by movement and turn on a "reduce motion" setting in their operating system. CSS exposes this through a media query, and it is good practice to tone down or switch off decorative animations there:

  • Wrap large or looping effects in @media (prefers-reduced-motion: no-preference) { ... }.
  • Or, inside @media (prefers-reduced-motion: reduce), set animation: none on those elements.
  • Keep essential feedback, such as a short fade, but drop bouncing and parallax.

Animations versus transitions

Transitions react to a change in state, such as hover, and only go from A to B. Animations run on their own, can have many steps, and can repeat. If you just want a button colour to change smoothly on hover, a transition is simpler.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course