Motion Path
Animating an element across the screen with transform: translateX() moves it in a straight line. If you want it to arc, curve, or follow a complex route, you need to combine multiple transforms with precise timing and easing to fake the curve. CSS Motion Path replaces that approximation with a direct declaration: define the path, set how far along it the element sits, and animate that distance. The element follows the curve exactly, and can even rotate automatically to face the direction it is traveling.
The Offset Properties
Motion path is controlled by five properties that work together. You will use the first three on nearly every motion path animation.
-
offset-path: Defines the path the element follows. Acceptspath()with an SVG path string, basic shapes likecircle()orellipse(), or aurl()reference to an SVG element in the document. -
offset-distance: How far along the path the element is positioned, from0%(start) to100%(end). This is the property you animate. -
offset-rotate: Controls the element's rotation as it travels.autorotates the element to face forward along the path. A fixed angle like0degkeeps the element upright regardless of path direction. You can combine both:auto 45degadds a fixed offset to the auto rotation. -
offset-anchor: Which point on the element is pinned to the path. Defaults to the element's transform origin. Useful when you want an arrow's tip rather than its center to follow the line. -
offset: Shorthand for all four properties above.
@keyframes follow-path {
to { offset-distance: 100%; }
}
.mover {
offset-path: path('M 20 100 C 100 20, 300 20, 380 100');
offset-distance: 0%;
offset-rotate: auto;
animation: follow-path 2s ease-in-out infinite alternate;
}
Extracting a Path from SVG
The most reliable workflow for complex motion paths is to draw the path in a vector editor (Figma, Illustrator, Inkscape) and extract the d attribute value from the exported SVG. That value drops directly into path() in CSS.
Open the exported SVG file in a text editor and find the <path> element. The d attribute contains the path data:
<!-- Inside your exported SVG -->
<svg viewBox="0 0 400 200" xmlns="http://www.w3.org/2000/svg">
<path d="M 20 100 C 100 20, 300 20, 380 100" fill="none" stroke="#ccc"/>
</svg>
/* Paste the d attribute value directly into path() */
.mover {
offset-path: path('M 20 100 C 100 20, 300 20, 380 100');
}
When you use path() on an HTML element, the coordinates are in pixels relative to the element's containing block, not the SVG's viewBox. If you place the same SVG in the page as a background reference and use its d value in offset-path, the moving element will follow the path correctly only if the SVG is rendered at the same size as the coordinate space the path was drawn in. The safest approach is to size the SVG container to match the path's bounding box and use a viewBox that matches your path coordinates exactly.
You can also reference an SVG <path> element directly using url(#path-id), which keeps the path data in one place and automatically inherits the SVG coordinate system. This approach requires the SVG to be inline in the HTML document, not loaded as an external file.
<svg viewBox="0 0 400 200" style="position: absolute; width: 400px; height: 200px;">
<path id="curve" d="M 20 100 C 100 20, 300 20, 380 100" fill="none"/>
</svg>
.mover {
offset-path: url(#curve);
offset-distance: 0%;
}
Motion Path vs. Transform-Only Motion
Before reaching for offset-path, consider whether the motion genuinely needs a curve. Many effects that look curved can be achieved by combining two transforms with different easing curves on each axis, a technique sometimes called arc motion.
/* Fake arc: x moves with ease-out, y with ease-in */
@keyframes arc-x { to { translate: 300px 0; } }
@keyframes arc-y { to { translate: 0 120px; } }
.mover {
animation:
arc-x 0.8s ease-out forwards,
arc-y 0.8s ease-in forwards;
}
This approach works well for simple throws, drops, and parabolic arcs. It falls apart when the path changes direction more than once, when the element needs to rotate to face its direction of travel, or when the path is drawn by a designer and must be followed exactly. Those are the cases where offset-path is the right tool.
- Use transform-only motion for simple arcs, bounces, and throws where approximate curves are fine and the element does not need to rotate along the path.
-
Use
offset-pathwhen the path changes direction, when the element must face forward, when the path was designed in a vector tool and needs to be followed precisely, or when you want to animate along a circle or ellipse without trigonometry.
Seeing the Difference
The demo below shows an element following a cubic bezier curve using offset-path. The dashed stroke and the HTML movers share one coordinate space that scales to fit the preview, so the triangle tracks the curve from start to finish. Toggle offset-rotate between auto and 0deg to see how automatic rotation changes the feel of the motion. Toggle the straight-line comparison to see the same timing along a chord from start to end.
Watch the triangle follow the dashed curve. Toggle straight-line comparison to see the same motion along the chord from start to end.
Reduced Motion
Motion path animations are often large, sweeping movements that are high-priority targets for reduced-motion handling. Apply the standard guard: either wrap the animation in a no-preference query or override it in a reduce block. For a motion path animation, a sensible fallback is to place the element at its final position (offset-distance: 100%) without any animation, so it is visible and in the right place without moving.
@media (prefers-reduced-motion: no-preference) {
@keyframes follow-path {
from { offset-distance: 0%; }
to { offset-distance: 100%; }
}
.mover {
animation: follow-path 2s ease-in-out forwards;
}
}
/* Element sits at its destination without animating */
.mover {
offset-path: path('M 20 100 C 100 20, 300 20, 380 100');
offset-distance: 100%;
}
Build Your Demo
Create a new directory at unit-6/motion-path/ in your portfolio with index.html and motion-path.css. Your demo must include:
-
At least one element animated along a non-trivial
offset-path: a curve or multi-segment path, not a straight line. Draw the path in a vector tool or write it by hand, and include the visible SVG path in the page so viewers can see the route. -
offset-rotate: autoso the element faces its direction of travel. Your teaching video should show what happens when you remove or change this value. -
A
prefers-reduced-motionguard that places the element at its destination without animating for users who prefer reduced motion.
Add a link to your demo on your portfolio homepage and in your README.md.
Teaching Video Checklist
When you record your teaching video, make sure your teammates come away understanding these concepts:
-
What
offset-pathandoffset-distancedo together, and why animatingoffset-distancefrom0%to100%is all that is needed to move an element along the path. -
The workflow for extracting a path from an SVG file: where to find the
dattribute, how to paste it intopath(), and why the coordinate system matters. -
What
offset-rotate: autodoes visually, demonstrated by toggling it on and off on your demo so teammates can see the difference clearly. - When to use motion path instead of transform-only arc motion, and when the simpler approach is good enough.
- Your portfolio demo: show the element traveling the path, explain the CSS driving it, and show the reduced-motion fallback by toggling the emulation in DevTools.
Start by opening the SVG source in a text editor and pointing out the d attribute before showing any CSS. The path-to-CSS workflow is the hardest concept for teammates to picture without seeing it happen.
Resources
-
MDN: offset-path — Full property reference including
path(),url(), and basic shape values - MDN: offset-distance — The animatable property that drives motion along the path
- MDN: offset-rotate — Auto rotation, fixed angles, and combined values
- MDN: CSS Motion Path — Module overview with guides and browser compatibility