AI Workshop: learn to build apps with AI →
Animations: How to continuously rotate an image using CSS

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


I had to search for how to rotate an image. I wanted to rotate an SVG image, but this works for any image type. Or any HTML element, actually.

Add this CSS instruction to the element you want to rotate:

animation: rotation 2s infinite linear;

You can also choose to add a rotate class to an element, instead of targeting it directly:

.rotate {
  animation: rotation 2s infinite linear;
}

Tweak the 2s to slow down or speed up the rotation period.

Then add this keyframes block, outside of any selector:

@keyframes rotation {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(359deg);
  }
}

That’s it! Your elements should now rotate.

Check out the CSS Animations and CSS Transitions guides

Here is the result shown in CodePen:

See the Pen How to use CSS Animations to continuously rotate an image by Flavio Copes (@flaviocopes) on CodePen.

Lessons in this unit:

0: Introduction
1: How to Use CSS Transforms (2D and 3D)
2: How to Create Smooth CSS Transitions
3: How to Create CSS Animations with Keyframes
4: ▶︎ How to continuously rotate an image using CSS
5: Compare the options for Animations on the Web