CSS offset-rotate in SVG Path Animations
The offset-rotate CSS property defines the orientation
and rotational angle of an element as it travels along a defined motion
path, such as an SVG path. By controlling whether an object naturally
faces the direction of travel, maintains a fixed angle, or applies an
angular offset, offset-rotate ensures visual assets align
properly with complex curves. This property is a core component of the
CSS Motion Path module, eliminating the need for complex JavaScript
calculations or manual keyframe rotations during path animations.
How offset-rotate Works
When animating an element along an SVG path using
offset-path, the browser calculates the element’s
coordinates over time. However, positioning alone does not dictate which
way the element points. The offset-rotate property
determines the element’s tangent angle relative to the path curve at any
given point during the animation.
Key Values and Syntax
The offset-rotate property accepts keywords, specific
degree angles, or a combination of both:
auto: The default behavior. It rotates the element to match the tangent of the path, ensuring the element dynamically faces forward in the direction of travel (similar to a car following turns in a road).auto <angle>: Rotates the element to follow the path’s direction while adding a constant rotational offset. For example,offset-rotate: auto 90deg;is useful when an SVG icon was originally drawn pointing upward instead of to the right (the standard 0-degree baseline).reverse: Directs the element to face 180 degrees away from the direction of travel, effectively moving it backward along the path. Likeauto, it can also take an additional angle modifier (e.g.,reverse 45deg).<angle>(Fixed Rotation): Sets a constant angle (e.g.,0deg,45deg,180deg). The element maintains this exact rotation regardless of how the path curves or turns.
Practical Use Cases in SVG Animations
- Correcting Asset Orientation: SVG assets often have
arbitrary default orientations depending on how they were exported from
vector design tools. Using
offset-rotate: auto [angle]corrects the initial facing direction so the asset aligns seamlessly with the path without altering the original SVG markup. - Simulating Realistic Movement: Objects like
vehicles, airplanes, paper planes, or arrows require their front edge to
track the curve of a path dynamically. Setting
offset-rotate: autohandles this calculation automatically. - Maintaining Upright Elements: When moving UI
elements like tooltips, avatar badges, or labels along a curved path,
the text or icon must often remain upright. Applying
offset-rotate: 0degallows the element to translate along the path without tilting or turning upside down.