codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎨

HTML & CSS

28 / 59 topics
28CSS Animation29CSS Transitions30CSS Transforms31CSS Variables32CSS Pseudo-elements33CSS Pseudo-classes
Tutorials/HTML & CSS/CSS Animation
🎨HTML & CSS

CSS Animation

Updated 2026-04-20
3 min read

Introduction

CSS animations provide a powerful way to add dynamic visual effects to your web pages without relying on JavaScript. This tutorial will cover the basics of CSS animations, including keyframes, timing functions, and best practices for creating smooth and efficient animations.

Key Concepts

Keyframes

Keyframes define the stages of an animation. You can specify styles at different percentages (0%, 50%, 100%) to create a sequence of transformations or changes.

@keyframes example {
  from {background-color: red;}
  to {background-color: yellow;}
}

Animation Properties

CSS provides several properties to control the animation:

  • animation-name: Specifies the name of the @keyframes rule.
  • animation-duration: Defines how long an animation should take to complete one cycle.
  • animation-timing-function: Sets the speed curve of the animation.
  • animation-delay: Delays the start of an animation.
  • animation-iteration-count: Determines how many times an animation should run.
  • animation-direction: Specifies whether an animation should play in reverse on alternate cycles.
  • animation-fill-mode: Defines what styles to apply for the element before and after the animation plays.
  • animation-play-state: Controls whether an animation is running or paused.

Timing Functions

Timing functions determine how fast an animation progresses at different points. Common timing functions include:

  • ease: Starts slow, speeds up, then slows down again (default).
  • linear: Constant speed throughout.
  • ease-in: Starts slowly and accelerates towards the end.
  • ease-out: Starts quickly and decelerates towards the end.
  • ease-in-out: Combines ease-in and ease-out.

Creating a Simple Animation

Let's create a simple animation that changes the background color of an element over 3 seconds.

<div class="animated-box"></div>
.animated-box {
  width: 100px;
  height: 100px;
  background-color: red;
  animation-name: example;
  animation-duration: 3s;
}

@keyframes example {
  from {background-color: red;}
  to {background-color: yellow;}
}

Advanced Animation Techniques

Multiple Keyframes

You can define multiple keyframes to create more complex animations.

@keyframes complex-example {
  0%   {background-color: red; left: 0px; top: 0px;}
  25%  {background-color: yellow; left: 200px; top: 0px;}
  50%  {background-color: green; left: 200px; top: 200px;}
  75%  {background-color: blue; left: 0px; top: 200px;}
  100% {background-color: red; left: 0px; top: 0px;}
}

Using animation-iteration-count

To make the animation repeat indefinitely, use infinite.

.animated-box {
  /* ... */
  animation-iteration-count: infinite;
}

Animation Direction

Use animation-direction to reverse the animation on alternate cycles.

.animated-box {
  /* ... */
  animation-direction: alternate;
}

Best Practices

  1. Performance: Minimize the use of animations that can cause layout thrashing or reflows. Use hardware-accelerated properties like transform and opacity.

  2. Accessibility: Ensure animations do not interfere with accessibility features. Provide alternatives for users who cannot perceive animations.

  3. Prefixes: While modern browsers support unprefixed CSS animations, it's a good practice to include vendor prefixes for broader compatibility.

  4. Optimize Keyframes: Use only necessary keyframes to reduce the complexity of your animations.

  5. Use animation-fill-mode: Control the styles before and after an animation to ensure a smooth transition.

Real-World Example

Let's create a more complex example that combines multiple properties and best practices.

<div class="complex-animation"></div>
.complex-animation {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  animation-name: complex-example;
  animation-duration: 4s;
  animation-iteration-count: infinite;
  animation-timing-function: ease-in-out;
}

@keyframes complex-example {
  0%   {background-color: red; left: 0px; top: 0px; transform: rotate(0deg);}
  25%  {background-color: yellow; left: 200px; top: 0px; transform: rotate(90deg);}
  50%  {background-color: green; left: 200px; top: 200px; transform: rotate(180deg);}
  75%  {background-color: blue; left: 0px; top: 200px; transform: rotate(270deg);}
  100% {background-color: red; left: 0px; top: 0px; transform: rotate(360deg);}
}

Conclusion

CSS animations are a powerful tool for enhancing the visual appeal of your web pages. By understanding keyframes, animation properties, and best practices, you can create smooth and efficient animations that enhance user experience without compromising performance.

Remember to test your animations across different browsers to ensure compatibility and adjust your styles accordingly. Happy coding!


PreviousCSS Frameworks (Bootstrap, Tailwind CSS)Next CSS Transitions

Recommended Gear

CSS Frameworks (Bootstrap, Tailwind CSS)CSS Transitions