Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
The CSS rotate() Function
The CSS rotate() function is used to rotate an element around a fixed point. It is applied through the transform property and accepts angle values in degrees, radians, or other angle units.
Syntax
transform: rotate(angle);
Possible Values
| Value | Description |
|---|---|
deg |
Degrees (360deg = full rotation) |
rad |
Radians (2? rad = full rotation) |
grad |
Gradians (400grad = full rotation) |
turn |
Turns (1turn = full rotation) |
Example: Rotate an Element by 45 Degrees
The following example rotates a text element by 45 degrees −
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 200px;
height: 100px;
background-color: #4CAF50;
color: white;
display: flex;
align-items: center;
justify-content: center;
margin: 50px;
transform: rotate(45deg);
}
</style>
</head>
<body>
<div class="box">Rotated 45°</div>
</body>
</html>
A green box with white text "Rotated 45°" appears rotated 45 degrees clockwise from its original position.
Example: Rotate an Element by 90 Degrees
The following example rotates an element by 90 degrees −
<!DOCTYPE html>
<html>
<head>
<style>
.box-90 {
width: 150px;
height: 80px;
background-color: #ff6b6b;
color: white;
display: flex;
align-items: center;
justify-content: center;
margin: 100px;
transform: rotate(90deg);
}
</style>
</head>
<body>
<div class="box-90">Rotated 90°</div>
</body>
</html>
A red box with white text "Rotated 90°" appears rotated 90 degrees clockwise, making it vertical.
Example: Rotate with Transform-Origin
The transform-origin property changes the rotation point. By default, elements rotate around their center −
<!DOCTYPE html>
<html>
<head>
<style>
.rotate-origin {
width: 120px;
height: 60px;
background-color: #3498db;
color: white;
display: flex;
align-items: center;
justify-content: center;
margin: 80px;
transform-origin: top left;
transform: rotate(45deg);
}
</style>
</head>
<body>
<div class="rotate-origin">Top-Left Origin</div>
</body>
</html>
A blue box with white text rotates 45 degrees around its top-left corner instead of its center.
Conclusion
The rotate() function provides an easy way to rotate elements using the transform property. Combine it with transform-origin to control the rotation point for more precise positioning effects.
Advertisements
