user3597950 user3597950 - 3 months ago 22
CSS Question

Spin or rotate an image on hover

I want to find out how to make a spinning or rotating image when it is hovered. I would like to know how to emulate that functionality with CSS on the following code :



img {
border-radius: 50%;
}

<img src="http://i.imgur.com/3DWAbmN.jpg" />




Answer

You can use CSS3 transitions to spin the image on hover.

Rotating image :

DEMO

img {
  border-radius: 50%;
  -webkit-transition: -webkit-transform .8s ease-in-out;
          transition:         transform .8s ease-in-out;
}
img:hover {
  -webkit-transform: rotate(360deg);
          transform: rotate(360deg);
}
<img src="http://i.imgur.com/3DWAbmN.jpg" />

More info and references :

  • a guide about CSS transitions on MDN
  • a guide about CSS transforms on MDN
  • browser support table for 2d transforms on canIuse
  • browser support table for transitions on canIuse
Comments