How to continuously rotate an image using CSS
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.
→ I wrote 17 books to help you become a better developer:
- C Handbook
- Command Line Handbook
- CSS Handbook
- Express Handbook
- Git Cheat Sheet
- Go Handbook
- HTML Handbook
- JS Handbook
- Laravel Handbook
- Next.js Handbook
- Node.js Handbook
- PHP Handbook
- Python Handbook
- React Handbook
- SQL Handbook
- Svelte Handbook
- Swift Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025