日本語

How to Move an Element Along a Line with CSS offset-path

​​CSS​Animation,Playwright,SVG

With offset-path, you set the shape of a line (path), and when you animate offset-distance from 0% to 100%, the element moves along that line. You can build motion like a paper plane flying along a curve or a loading icon circling a ring, without JavaScript.

css
.plane {
  offset-path: path("M 20 100 L 380 100");
  animation: move 4s linear infinite;
}

@keyframes move {
  from { offset-distance: 0%; }
  to   { offset-distance: 100%; }
}
Note

I tested on Ubuntu 24.04, Node.js 22.23.5, Playwright 1.63.0, and Chromium 158. I opened the page with Playwright and measured element positions with getBoundingClientRect(). Positions are in px, measured from the top-left corner of the parent element as (0, 0), and refer to the center of the element. I did not test Firefox or Safari. The browser versions in the support table come from the compatibility data (@mdn/browser-compat-data 8.1.11).

Use offset-distance to decide where the element is on the line

What you write in offset-path is the shape of the line the element travels along. You write the line in path() using the same syntax as the d attribute of an SVG <path>. offset-distance decides where the element sits on the line. 0% is the start of the line and 100% is the end.

I placed a 20px square on a straight line of length 360px, path("M 20 100 L 380 100"), and changed offset-distance.

offset-distance Element center
0% (20, 100)
25% (110, 100)
50% (200, 100)
100% (380, 100)
90px (110, 100)

25% and 90px were the same position. A percentage is a fraction of the line's length, and px is the distance from the start of the line.

To animate the element, animate offset-distance with @keyframes. With the CSS above (4 seconds, linear), I paused the animation and measured it: after 1 second it was at 25%, after 2 seconds at 50%, and after 3 seconds at 75%. When I changed linear to ease-in-out, it was at 12.9162% after 1 second and 87.0838% after 3 seconds, so the element moved slowly at both ends and faster in the middle.

Use offset-rotate to decide the element's direction

When you move an element along a curve, it rotates to follow the direction of the line without any extra CSS. This is because the initial value of offset-rotate is auto. If you don't want the element to rotate, use offset-rotate: 0deg.

Arrows move from left to right along two identical wavy dotted lines as offset-distance increases from 0%. The arrow on top, with offset-rotate: auto, turns up and down to follow the slope of the line. The arrow on the bottom, with 0deg, faces right the whole time.
Elements with offset-rotate set to auto and 0deg moving along the same curve

I placed a 20px square on a straight line that slopes down at 45 degrees to the right. I measured the element's direction as the angle of the line between two small dots attached to its left and right edges. The angle is measured clockwise, with the rightward direction as 0deg.

offset-rotate Element direction
Not written (auto) 45deg (same as the line)
0deg 0deg (no rotation)
reverse -135deg (opposite to the line)
auto 90deg 135deg (the line's direction plus 90 degrees)
30deg 30deg (fixed, unrelated to the line)

Use auto 90deg when you have an arrow icon drawn pointing up and you want it to face the direction of travel. With auto, the right side of the element faces the direction of travel. An icon drawn pointing up is 90 degrees off from that, so adding 90 degrees makes it match.

On the curved path path("M 20 180 Q 200 -60 380 180"), the auto direction was -53.1deg at 0%, 0deg at 50% (the peak), and 53.1deg at 100%.

Change the point that sits on the line with offset-anchor

By default, the center of the element sits on the line. The initial value of offset-anchor is auto, and according to MDN, when it is auto, the same point as transform-origin is used. The initial value of transform-origin is the center of the element.

I placed a 20px square at the start of the line, (20, 100). The center ended up at these positions:

CSS written Element center
None (20, 100)
offset-anchor: left top (30, 110). The top-left corner sits on the line
offset-anchor: right bottom (10, 90). The bottom-right corner sits on the line
transform-origin: left top (30, 110)

Changing only transform-origin also changed the anchor point. If an element uses transform-origin for rotation or scaling and looks offset from the line, add offset-anchor: center. When I wrote it together with transform-origin: left top, the center returned to (20, 100).

Coordinates in path() are measured from the element's original position

The coordinates in path() are not measured from the top-left of the page or of the parent element. They are measured with the element's original position (its top-left corner, where it sits without offset-path) as (0, 0).

I kept the same path("M 20 100 L 380 100") at 0% and changed only where the element was placed:

How the element is placed Element center
position: absolute; top: 0; left: 0 (20, 100)
Changed to top: 50px; left: 100px (120, 150)
position: static; margin-left: 100px (120, 100)

If you want the element's path to line up with a line drawn in SVG inside the parent element, place the element at the top-left of its parent with position: absolute; top: 0; left: 0. Then you can write the same coordinates in path() and in the SVG d attribute.

On the other hand, when you use a shape function such as circle(80px at 200px 100px), the at position is measured from the parent element (the containing block). Setting the element to top: 50px; left: 100px did not change its position. For circle(), 0% was the right edge of the circle at (280, 100), 25% was the bottom, and 50% was the left edge. The motion went clockwise.

On closed paths, values beyond 100% wrap back to the start

How movement behaves when offset-distance is greater than 100% or negative depends on whether the line is closed.

Line Value above 100% -10%
Straight line (not closed) 150% stops at the end point Stops at the start point
Square closed with Z 120% is at the 20% position At the 90% position
Same square drawn back to the start point without Z 120% stops at the end point Stops at the start point

The squares look the same, but when I drew the line back to the start point without Z, it was not treated as a closed path.

Point

If you want to move offset-distance beyond 100% so that the element keeps circling the same line, close the path() with Z.

To match the parent's size, use shape()

The coordinates in path() are fixed in px, so the line does not stretch or shrink when the parent's width changes. When I tested with a parent width of 400px and then 600px, the 100% position of path("M 20 100 L 380 100") was (380, 100) in both cases.

If you want the line to scale with the parent, shape() lets you write coordinates as percentages. The 50% position of shape(from 5% 50%, line to 95% 50%) was (200, 100) with a parent width of 400px, and (300, 100) with a width of 600px.

However, in Chromium 158, the environment I tested, offset-distance: 100% on a shape() straight line was at the start point, not the end point (at (20, 100) with a width of 400px). At 99%, the element was near the end, at (376.4, 100). If you want the element to stop at the end of the line when the animation finishes, use path(), or make the final value slightly less than 100%.

Follow a line in the page's SVG or the border of a parent element

You can also use a line drawn in an SVG on the page. When I pointed offset-path: url(#track) at <path id="track" d="M 20 100 L 380 100"/>, the element was at the same position as when I wrote the same coordinates in path(). If the id was mistyped and the reference was missing, the element moved to (0, 0). MDN also says that when the reference is missing, the value becomes path("M0,0").

If you write only a box value, such as offset-path: border-box, the element moves along the border of its parent element. With a parent 400px wide and 200px tall, 0% was the top-left corner (0, 0), 25% was (300, 0) on the top edge, and 50% was the bottom-right corner (400, 200). The motion went clockwise from the top-left.

For people who reduce motion, stop the animation

Some people set their OS to reduce on-screen motion. For them, you can stop the animation with prefers-reduced-motion.

css
@media (prefers-reduced-motion: reduce) {
  .plane {
    animation: none;
  }
}

With Playwright, I set reduce and waited 1 second. No animations were running, and the element was at the start of the line, (20, 100). Without the setting, after the same 1 second, the element had moved to (108.5, 100).

Browser support by syntax

path() in offset-path, along with offset-distance and offset-rotate, has been supported for a long time. Shapes such as circle(), url(), and box values are newer. The compatibility data lists the versions in which each was supported as follows. In every row, Safari on iOS has the same version as Safari. The shape() row covers support for the shape() function itself, not only its use in offset-path.

Syntax Chrome Edge Firefox Safari
offset-path: path() 64 79 72 16
offset-distance 55 79 72 16
offset-rotate 56 79 72 16
offset-anchor 116 116 72 16
url() 116 116 122 17
Shapes such as circle(), and box values such as border-box 116 116 122 18
shape() 135 135 148 18.4

You can also combine offset-path, offset-distance, and offset-rotate into the shorthand offset, for example offset: path("M 20 100 L 380 100") 50% 0deg;. In my test environment, this gave the same position as writing the three properties separately.

​Posted by 86