How to rotate image with JavaScript

By Gulshan Saini
Published in JavaScript
January 04, 2023
1 min read

Here is a simple example of how you can use JavaScript to rotate an image.

First, you will need to have an image element in your HTML with an id attribute that you can use to access it in your JavaScript code. For example:

<img id="my-image" src="path/to/image.jpg" alt="My Image">

Next, you can use the following JavaScript code to rotate the image:

// Get the image element
var image = document.getElementById('my-image');

// Set the rotation angle
var angle = 45;

// Rotate the image
image.style.transform = 'rotate(' + angle + 'deg)';

This code will rotate the image by 45 degrees clockwise. You can adjust the value of the angle variable to change the amount of rotation.

You can also use other CSS transform functions, such as scale and translate, to manipulate the image in various ways.


Previous Article
How to scale image or element with JavaScript

Related Posts

How to move image or element with JavaScript
January 04, 2023
1 min
Gulshan Saini

Gulshan Saini

Fullstack Developer



Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Quick Links

Contact UsBrowserCSSPythonPuppeteer

Social Media