How to rotate an HTML div element 90 degrees using JavaScript ?
An element can be rotated 90 degrees by using the transform property. This property is used to move, rotate, scale and others to perform various kinds of transformation to elements.
The rotate() transformation function can be used as the value to rotate the element. It takes one parameter which defines the rotation angle. The rotation angle consists of two parts, the value of the rotation followed by the unit of rotation. The unit can be defined in degrees (deg), gradient (grad), radians (rad) and turns.
To rotate by 90 degrees any of the units can be used with their corresponding values. 90 degrees would equal to 100 gradient or 0.25 turns.
Applying this property to the required element would rotate it by 90 degrees.
Syntax:
// Using CSS transform: rotate(90deg); // Using JavaScript element.style.transform = 'rotate(90deg)';
Example: The example uses a rectangle and one side of a border to explain the rotation.
<!DOCTYPE html> <html> <head> <title> How to rotate an HTML div element 90 degrees using JavaScript ? </title> <style> .box { height: 250px; width: 150px; border-right: 5px solid; background-color: lightgreen; } </style> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to rotate an HTML div element in 90 degrees? </b> <p> Click on the button below to rotate the element by 90 degress. </p> <div class="box"></div> <button onclick="rotateElem()"> Rotate by 90 degrees </button> <script type="text/javascript"> function rotateElem() { document.querySelector('.box').style.transform = 'rotate(90deg)'; } </script> </body> </html> |
Output:
- Before clicking the button:

- After clicking the button:

Recommended Posts:
- How to remove an HTML element using JavaScript ?
- How to add/update an attribute to an HTML element using JavaScript?
- Set the focus to HTML form element using JavaScript
- Check whether HTML element has scrollbars using JavaScript
- HTML canvas rotate() Method
- p5.js | degrees() function
- How to set the value of a select box element using JavaScript?
- How to get the type of DOM element using JavaScript?
- How to add a class to DOM element in JavaScript?
- Change an element class JavaScript
- Print the content of a div element using JavaScript
- How to get the data attributes of an element using JavaScript ?
- How to append data to <div> element using JavaScript ?
- How to get the child element of a parent using JavaScript ?
- Find the min/max element of an Array using JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



