In previous posts, we have explained about how to rotate image 360 using HTML, or about useful CSS animations, so in this article using CSS property transform, we will show how we can rotate image using CSS and HTML.

Image rotate using CSS

If you are creating a web-application, you may need to transform or rotate image using CSS, then you can use CSS property "transform", which can help you in rotating image, here is the basic example in which we are creating class to rotate image by 90 degree

.RotateImageBy90Degree {
    -webkit-transform: rotate(90deg);
    -moz-transform: rotate(90deg);
    -o-transform: rotate(90deg);
    -ms-transform: rotate(90deg);
    transform: rotate(90deg);
}

So, take a look at the below sample, where we have given HTML code with an image

<img src="https://via.placeholder.com/150" class="RotateImageBy90Degree ">

then you will se image rotated by 90 degree, as shown below

image-rotate-in-html-css-min.png

Here is the fiddle sample of the above

If you want to rotate image by 180 degree, you can change your CSS as below

.RotateImageBy180Degree {
    -webkit-transform: rotate(180deg);
    -moz-transform: rotate(180deg);
    -o-transform: rotate(180deg);
    -ms-transform: rotate(180deg);
    transform: rotate(180deg);
}

and for 270 degree as below

.RotateImageBy270Degree {
    -webkit-transform: rotate(270deg);
    -moz-transform: rotate(270deg);
    -o-transform: rotate(270deg);
    -ms-transform: rotate(270deg);
    transform: rotate(270deg);
}

Considering we have HTML as below

<img src="https://via.placeholder.com/150" class="RotateImageBy180Degree">

<img src="https://via.placeholder.com/150" class="RotateImageBy270Degree">

So, if you apply changes on the above image, you will see output as below

rotate-image-html-css.png

Continously rotate image using CSS

If you want to rotate image continously, you can use CSS transform with animation property with key-frames.

Consider the below CSS

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

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

In the above CSS, we are using rotation with infinte transform, with speed of 2s per rotation.

If we apply above CSS on the below HTML image

<img src="https://via.placeholder.com/150" class="rotate">

It will keep on rotating (as we have used infinite loop) while taking 2s per loop. (transformation time from 0 -> 360 degree is 2 seconds)

Here is the fiddle sample

Flip an image

You can also flip image, using CSS transform with scaleX, consider using below code, where we are using single image of an arrow, but showing both, left and right arrow, with the help of CSS transform

 <img
    style="transform: scaleX(-1); width:50px"
    src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-90/left-arrow.png'
    alt=''
  >
  
   <img
    style=" width:50px"
    src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-90/left-arrow.png'
    alt=''
  >

Output using Fiddle: