Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Keyframe Animations | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 22 of 50

·css

Keyframe Animations

12 mins

Course progress44%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • `animation-fill-mode: forwards` keeps the element in its final keyframe state.
  • `animation-direction: alternate` makes the animation play forward then backward.
  • For performance, animate only `transform` and `opacity`.

Keyframe animations describe a timeline of style changes. They differ from transitions because you can define multiple intermediate steps and repeat the animation indefinitely.

Anatomy of an Animation

@keyframes <name> {
  from { … }           /* same as 0% */
  25%  { … }           /* quarter‑way */
  50%  { … }           /* half‑way */
  to   { … }           /* same as 100% */
}

.selector {
  animation: <name> <duration> <timing-function> <delay> <iteration-count> <direction> <fill-mode>;
}
  • animation-name – Reference to the @keyframes block.
  • animation-duration – Length of one cycle.
  • animation-timing-function – Acceleration curve.
  • animation-delay – Wait before the first cycle.
  • animation-iteration-count – Number of repeats (infinite).
  • animation-direction – normal, reverse, alternate, alternate-reverse.
  • animation-fill-mode – none, forwards, backwards, both (controls style before/after the animation).

Example – Pulsing Loader

@keyframes pulse {
  0%   { opacity:0.3; }
  50%  { opacity:1; }
  100% { opacity:0.3; }
}
.loader {
  width:40px; height:40px; border-radius:50%;
  background:#0066cc; animation:pulse 1.2s infinite;
}

The element fades in and out, mimicking a loading indicator.


Performance Advice

  • Animate only transform and opacity whenever possible – they stay on the compositor layer and avoid layout/repaint.
  • Keep the keyframe list short. Modern browsers can interpolate complex values, but an excessively long list may hurt performance.
  • Use prefers-reduced-motion to disable non‑essential animations for users who request them.

Real‑World Use‑Case – Text Reveal on Scroll

/* initial hidden state */
.reveal { opacity:0; transform:translateY(20px); }
/* when the .visible class is added (via IntersectionObserver) */
.reveal.visible {
  animation: slideIn 0.6s forwards ease-out;
}

@keyframes slideIn {
  to { opacity:1; transform:translateY(0); }
}

Pairing CSS animations with a tiny JavaScript IntersectionObserver gives you a smooth, performant scroll‑reveal effect.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonTransformsContinue to lesson 23Responsive Design – Media QueriesBack to CSS Basics to Advanced curriculum