We can use jQuery to rotate an image easily by using the css() method. We can target the css transform property using the css() method and use that to rotate our image or div.

$("image").css("transform","rotate(20deg)");

In the code above, you can change “20deg” to any degree amount you want to rotate the image by.


Let’s say we have the following HTML:

If we want to rotate the image by 90 degrees we can use the jQuery css() method in the following JavaScript code.

$("#div1 img").css("transform","rotate(90deg)");

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div1 img").css("transform","rotate(90deg)");

Using jQuery to Rotate an Image with a Click

To rotate an image using jQuery, we can combine the css() method with a click event.

In this example, we will have an image that we will want to rotate by 30 degrees. We will have a button that will allow the user to rotate the image by 30 degrees each time.

Here is our simple HTML code:

Rotate image

We can utilize both the jQuery css() method and the jQuery click() method to rotate the image.

We can then use the css() method to target the css transform property and use it to rotate our image.

Below is the Javascript code which will allow the user to be able to rotate our image by 30degrees each time:

var rotateDegrees = 0;
$("#click-me").click(function(){
  rotateDegrees += 30;
  $("#div1 img").css("transform","rotate(" + rotateDegrees + "deg)");
});

The final code and output for this example of how to use jQuery to rotate an image is below:

Code Output:

Rotate image

Full Code:

Rotate image
<script> var rotateDegrees = 0; $("#click-me").click(function(){ rotateDegrees += 30; $("#div1 img").css("transform","rotate(" + rotateDegrees + "deg)"); }); </script>

Hopefully this article has been useful for you to understand how to rotate an image using jQuery.

Categorized in:

jQuery,

Last Update: March 11, 2024