Skip to content
Tutorials Center
My learning

Topics

Lesson 101 of 139 ยท Transforms and Animation

Motion Path: offset-path and offset-distance

Move elements along a curve with CSS motion path. Learn offset-path, offset-distance, offset-rotate and offset-anchor, and animate them with keyframes.

Most CSS animation moves things in straight lines: translate slides an element from A to B. But real motion is often curved. A paper plane swoops, a loading dot circles a button, a bee follows a looping trail. You could fake that with many keyframes, but it is tedious and hard to change.

CSS Motion Path solves it directly. You draw a path, tell an element to ride on it, and then animate a single number: how far along the path the element is. The browser works out the position, and even the direction the element should face.

The four properties

Motion path is controlled by a handful of offset-* properties. (The name "offset" is historical; they have nothing to do with the offset of position.)

Property Purpose
offset-path The path to follow: path(), circle(), ellipse(), ray(), url()...
offset-distance How far along the path, as a length or a percentage (0% start, 100% end)
offset-rotate Whether and how the element turns to face the direction of travel
offset-anchor Which point of the element sits on the path (default: its center)

An element with offset-path set is placed on that path. offset-distance then slides it along. Without any animation you can already position things with it:

CSS
.dot {
  offset-path: path("M 20 80 C 80 0, 160 160, 220 80");
  offset-distance: 50%;   /* halfway along the curve */
}

Drawing the path with path()

The path() function takes the same syntax as the d attribute of an SVG path (you may know it from SVG): M moves the pen, L draws a line, C and Q draw curves, A an arc and Z closes the path. Coordinates are in pixels, measured from the top-left corner of the element's containing block, not from the element itself.

A few commonly used commands:

Command Meaning
M x y Move to a point (start here)
L x y Straight line to a point
C x1 y1, x2 y2, x y Cubic Bรฉzier curve with two control points
Q x1 y1, x y Quadratic curve with one control point
Z Close the path

Lowercase letters mean relative coordinates. If you are not comfortable writing paths by hand, draw one in any vector editor and copy its d attribute.

Your first animation along a path

Because offset-distance is an ordinary property, you can animate it with @keyframes or a transition. In this demo a dot rides a wavy line. To show the road, the same path is drawn as an SVG outline behind the dot.

A dot riding a curve

Notice what is not in the keyframes: no top, no left, no transform. The browser derives the position from the path and one number. alternate makes the dot swing back and forth, and the prefers-reduced-motion rule gives people who asked for less movement a static dot (read Motion and Performance for more on motion preferences).

The dot is position: absolute at left: 0; top: 0, because the path coordinates are measured from the containing block's corner. If you leave the element in normal flow, its own position is added to the path's, which is a common source of "why is it in the wrong place" confusion.

Facing the direction of travel: offset-rotate

By default (offset-rotate: auto) the element turns so that its "forward" direction (its right edge, the positive x-axis) points along the path. For a round dot you cannot see this, but for an arrow or a car it matters a lot.

Value Effect
auto Face the direction of travel (the default)
reverse Face backwards
<angle> A fixed rotation, no turning (0deg keeps it upright)
auto 90deg Face the direction of travel, plus an extra 90 degrees

Design your element so that, un-rotated, it points to the right. If your artwork points up, add 90deg: offset-rotate: auto 90deg.

A plane that banks into the curve

The inner <span> carries the artwork correction (the emoji points up-right, so we rotate it back by 45 degrees). Keeping that correction on a child means the path-driven rotation on the parent stays clean. Try offset-rotate: reverse and auto 90deg to see how the facing changes.

offset-anchor: which point rides the path

By default the center of the element sits on the path. offset-anchor changes that. It takes the same values as transform-origin or background-position: keywords or percentages. For a car, offset-anchor: 50% 100% puts the bottom center (the wheels) on the road instead of the middle of its body. For a pointer-shaped element, anchoring at the tip makes the tip trace the path exactly.

CSS
.car {
  offset-path: path("M 0 120 Q 150 0 300 120");
  offset-anchor: 50% 100%;   /* bottom center on the road */
}

Other path shapes

path() is the most flexible, but other functions are shorter for common shapes:

  • circle(r at x y) and ellipse(rx ry at x y): orbits. offset-path: circle(60px at center) moves the element around a circle with the given radius, starting at the top.
  • inset(), polygon() and rect(): straight-edged shapes (the same shape functions used by clip-path).
  • ray(45deg): an infinite straight line leaving a starting point at an angle. Animate offset-distance to shoot elements outward, as in a burst or confetti effect.
  • url(#id): reuse an SVG shape from the document.

An orbit needs no path data at all:

A moon orbiting a planet

The circle is centered at 130px 130px, the middle of the 260 px square, with a 90 px radius. Add a second moon with animation-delay: -2.5s and a bigger radius to build a tiny solar system.

Scrubbing the path with other triggers

Because offset-distance is just a number, anything that drives a property can drive it:

  • A transition on hover: .icon:hover { offset-distance: 100%; } with transition: offset-distance 0.4s.
  • Scroll: link the animation to scroll progress with animation-timeline (see Scroll-driven Animations), so the element travels along the path as the reader scrolls.
  • JavaScript: set element.style.offsetDistance = progress + "%" from a slider or game loop.

Tips and pitfalls

  • Coordinates: a path() is measured from the containing block's top-left. Use position: absolute with top: 0; left: 0 for predictable results, or put the element in a sized wrapper.
  • Scaling: path() uses fixed pixel units. For a responsive path, use an SVG with viewBox, or set the element's offset-path through a custom property updated in a media query.
  • Performance: animating offset-distance works on the compositor in modern browsers for many cases, but test on low-end devices when many elements move at once.
  • Accessibility: decorative motion should respect prefers-reduced-motion, and meaningful motion needs a non-animated alternative.
  • Support: motion path is supported in all current browsers. ray() and path() are the most reliable; older browsers simply show the element without moving it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which single property do you animate to move an element along its offset-path?

    offset-distance is the position along the path, from 0% to 100%. Animating it moves the element.
  2. 2. What does offset-rotate: auto do?

    With auto the element rotates to follow the path's tangent. Use 0deg or another angle to prevent turning.
  3. 3. Where do the coordinates in offset-path: path("M 10 10 ...") start?

    Path coordinates are relative to the containing block, so an absolutely positioned element at left 0 and top 0 follows the path exactly as drawn.
  4. 4. Which shape would you use for the simplest circular orbit?

    circle() defines a circular path, and animating offset-distance from 0% to 100% moves the element once around it.

Summary

  • Motion path places an element on a curve with offset-path and moves it with offset-distance (0% to 100%).
  • path() accepts SVG path syntax; circle(), ellipse(), ray() and shape functions cover common cases.
  • offset-rotate controls facing (auto, reverse, or a fixed angle), and offset-anchor chooses which point of the element rides the path.
  • Animate offset-distance with keyframes, transitions, scroll timelines or JavaScript.
  • Path coordinates are relative to the containing block; respect prefers-reduced-motion.

Next lesson: Scroll-driven Animations, where scrolling itself becomes the animation timeline.