Skip to content

How to continuously rotate an image using CSS

New Course Coming Soon:

Get Really Good at Git

How to use CSS Animations to continuously rotate an image

While building the React Handbook landing page, I had to search 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 line, 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.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching May 21, 2024. Join the waiting list!
→ Get my CSS Handbook
→ Read my CSS Tutorial on The Valley of Code

Here is how can I help you: