CSS Properties
CSS offset-rotate Property
Photo Credit to CodeToFun
🙋 Introduction
The offset-rotate
property in CSS is used to specify the rotation of an element along the motion path defined by the offset-path property.
This property allows you to control the orientation of an element as it moves along the path, enabling complex animations and dynamic visual effects.
It is particularly useful for animating elements along curved paths where maintaining a specific orientation is crucial.
💡 Syntax
The syntax for the offset-rotate
property is as follows:
element {
offset-rotate: auto | []?
}
- auto: The element rotates along the path, aligning its top side with the path's direction.
- <angle>: A specified angle for the rotation, which can be in degrees (deg), radians (rad), or gradians (grad).
🎛️ Default Value
The default value of the offset-rotate
property is auto, which means the element's rotation aligns automatically with the direction of the motion path.
🏠 Property Values
Value | Description |
---|---|
auto | Automatically aligns the element's rotation with the direction of the path. |
<angle> | Specifies a fixed rotation angle. For example, 45deg or 0.5rad. |
[<angle>] | An optional second angle that can be used to specify additional rotation relative to the element's current position. |
📄 Example
In this example, we animate a square along a circular path and use offset-rotate
to control its rotation.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS offset-rotate Example</title>
<style>
.path {
position: relative;
width: 200px;
height: 200px;
border: 1px solid #000;
border-radius: 50%;
margin: 50px auto;
}
.box {
position: absolute;
width: 50px;
height: 50px;
background-color: blue;
offset-path: path('M 100 0 A 100 100 0 1 1 100 -0.1'); /* Circular path */
animation: move 5s infinite linear;
}
@keyframes move {
to {
offset-distance: 100%;
offset-rotate: auto;
}
}
</style>
</head>
<body>
<h1>Square Moving Along a Circular Path</h1>
<div class="path">
<div class="box"></div>
</div>
</body>
</html>
In this example, the blue square moves along a circular path, and the offset-rotate: auto property ensures that the square's top side always aligns with the path's direction.
🖥️ Browser Compatibility
The offset-rotate
property is supported in most modern browsers, including Chrome, Firefox, and Edge. However, it may not be fully supported in older browsers. As always, testing across different browsers is recommended to ensure compatibility.
🎉 Conclusion
The offset-rotate
property provides fine-grained control over the orientation of elements as they move along a path.
This is especially useful for creating complex animations and ensuring that elements maintain a consistent orientation relative to their motion. Experiment with different values and combine them with other CSS properties to create dynamic and engaging animations on your web pages.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS offset-rotate Property), please comment here. I will help you immediately.