An offset-path lets you animate an element along a custom path, and the path data uses the same syntax as an SVG path. That similarity is misleading. Inside offset-path, path values behave like pixel units rather than relative SVG units, so a path that carries an element 600px will carry it 600px whether the viewport is 300px or 3440px wide. SVG paths scale with the viewbox; motion paths do not.
.block {
offset-path: path('M20,20 C20,100 200,0 200,100');
}
Two properties control the effect. offset-distance determines where along the path the element sits, and animating it moves the element. offset-rotate controls rotation; its default value, auto, makes the element follow the path's direction.
Why hardcoded paths break
Resize a demo with both types side by side and the split is obvious: the SVG and its contained path scale, while the offset-path stays fixed and the element leaves its intended course.
.element {
--path: 'M20,20 C20,100 200,0 200,100';
offset-path: path(var(--path));
}
Short paths can be patched by multiplying values per breakpoint, but even then it is unclear whether scaling every number produces correct geometry at the new size.
@media(min-width: 768px) {
.element {
--path: 'M40,40 C40,200 400,0 400,200'; // ????
}
}
Paths drawn in vector tools make the problem worse. Every container variation means reopening the application, rescaling, exporting, and re-integrating into CSS. Workable, but not a maintenance burden worth adopting.
.element {
--path: 'M40,228.75L55.729166666666664,197.29166666666666C71.45833333333333,165.83333333333334,102.91666666666667,102.91666666666667,134.375,102.91666666666667C165.83333333333334,102.91666666666667,197.29166666666666,165.83333333333334,228.75,228.75C260.2083333333333,291.6666666666667,291.6666666666667,354.5833333333333,323.125,354.5833333333333C354.5833333333333,354.5833333333333,386.0416666666667,291.6666666666667,401.7708333333333,260.2083333333333L417.5,228.75';
offset-path: path(var(--path));
}
@media(min-width: 768px) {
.element {
--path: 'M40,223.875L55.322916666666664,193.22916666666666C70.64583333333333,162.58333333333334,101.29166666666667,101.29166666666667,131.9375,101.29166666666667C162.58333333333334,101.29166666666667,193.22916666666666,162.58333333333334,223.875,223.875C254.52083333333334,285.1666666666667,285.1666666666667,346.4583333333333,315.8125,346.4583333333333C346.4583333333333,346.4583333333333,377.1041666666667,285.1666666666667,392.4270833333333,254.52083333333334L407.75,223.875';
}
}
@media(min-width: 992px) {
.element {
--path: 'M40,221.625L55.135416666666664,191.35416666666666C70.27083333333333,161.08333333333334,100.54166666666667,100.54166666666667,130.8125,100.54166666666667C161.08333333333334,100.54166666666667,191.35416666666666,161.08333333333334,221.625,221.625C251.89583333333334,282.1666666666667,282.1666666666667,342.7083333333333,312.4375,342.7083333333333C342.7083333333333,342.7083333333333,372.9791666666667,282.1666666666667,388.1145833333333,251.89583333333334L403.25,221.625';
}
}
Generating paths with D3 instead of hand-tuning them
GreenSock's MotionPath plugin can scale SVG paths, but that confines the animation to an SVG. The alternative is a charting library: D3.js accepts a set of coordinates and returns a generated path string, with configurable curves and sizing. A function built this way can scale a path against a defined coordinate system.
The catch: nobody wants to encode paths as coordinate sets. The practical workflow is to draw a path, export it, optimize it, and let JavaScript handle the rest. For the demo, the path came from Inkscape and was optimized with Jake Archibald's SVGOMG.

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 79.375 79.375" height="300" width="300"><path d="M10.362 18.996s-6.046 21.453 1.47 25.329c10.158 5.238 18.033-21.308 29.039-18.23 13.125 3.672 18.325 36.55 18.325 36.55l12.031-47.544" fill="none" stroke="#000" stroke-width=".265"/></svg>
Only path and viewBox matter from the output. The scaling function takes the path string plus bounds — typically the third and fourth values of the viewBox — to know what to scale against.
const path =
"M10.362 18.996s-6.046 21.453 1.47 25.329c10.158 5.238 18.033-21.308 29.039-18.23 13.125 3.672 18.325 36.55 18.325 36.55l12.031-47.544";
const height = 79.375 // equivalent to viewbox y2
const width = 79.375 // equivalent to viewbox x2
const motionPath = new ResponsiveMotionPath({
height,
width,
path,
});
From path string to data set
Reading the path segments is the core step, handled by the SVGGeometryElement API. Create an SVG element, put a path inside it, and assign the path string to the d attribute.
// To convert the path data to points, we need an SVG path element.
const svgContainer = document.createElement('div');
// To create one though, a quick way is to use innerHTML
svgContainer.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg">
<path d="${path}" stroke-width="${strokeWidth}"/>
</svg>`;
const pathElement = svgContainer.querySelector('path');
Then iterate over the path's total length and capture the point at each step.
convertPathToData = path => {
// To convert the path data to points, we need an SVG path element.
const svgContainer = document.createElement('div');
// To create one though, a quick way is to use innerHTML
svgContainer.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg">
<path d="${path}"/>
</svg>`;
const pathElement = svgContainer.querySelector('path');
// Now to gather up the path points.
const DATA = [];
// Iterate over the total length of the path pushing the x and y into
// a data set for d3 to handle 👍
for (let p = 0; p < pathElement.getTotalLength(); p++) {
const { x, y } = pathElement.getPointAtLength(p);
DATA.push([x, y]);
}
return DATA;
}
Scaling ratios and the container bounds
The viewBox values feed a ratio between the motion path and its container, mirroring the path-to-viewBox relationship, and those ratios go into D3.js scales. One function finds the maximum x and y values; another computes the ratios.
getMaximums = data => {
const X_POINTS = data.map(point => point[0])
const Y_POINTS = data.map(point => point[1])
return [
Math.max(...X_POINTS), // x2
Math.max(...Y_POINTS), // y2
]
}
getRatios = (maxs, width, height) => [maxs[0] / width, maxs[1] / height]
For the actual line, D3 turns the data set into a path string.
d3.line()(data); // M10.362000465393066,18.996000289916992L10.107386589050293, etc.
The raw points aren't tied to the container, so D3 scales act as interpolation functions that recalculate the path at the current size. For the x coordinates, the domain runs from 0 to the highest x value, and the range in most cases runs from 0 to container width multiplied by the width ratio.
const xScale = d3
.scaleLinear()
.domain([
0,
maxWidth,
])
.range([0, width * widthRatio]);
Handling mismatched aspect ratios
Ranges sometimes need padding. A path in a viewBox of 0 0 100 200 has a 1:2 aspect ratio; placed in a container of 20vmin by 20vmin, the container is 1:1. Calculating an offset keeps the path centered while preserving its aspect ratio.
const widthRatio = (height - width) / height
const widthOffset = (ratio * containerWidth) / 2
const xScale = d3
.scaleLinear()
.domain([0, maxWidth])
.range([widthOffset, containerWidth * widthRatio - widthOffset])
With both scales in hand, mapping the data points produces a fresh line.
const SCALED_POINTS = data.map(POINT => [
xScale(POINT[0]),
yScale(POINT[1]),
]);
d3.line()(SCALED_POINTS); // Scaled path string that is scaled to our container
Applying it is a single step: pass the generated path inline through the CSS property.
ELEMENT.style.setProperty('--path', `"${newPath}"`);
When to regenerate and reapply is the remaining decision, and one possible solution looks like this:
const setPath = () => {
const scaledPath = responsivePath.generatePath(
CONTAINER.offsetWidth,
CONTAINER.offsetHeight
)
ELEMENT.style.setProperty('--path', `"${scaledPath}"`)
}
const SizeObserver = new ResizeObserver(setPath)
SizeObserver.observe(CONTAINER)
The accompanying demo, best viewed full screen, shows three versions of the element: the unscaled SVG, a scaling container that isolates the non-scaling behavior of offset-path, and the JavaScript-scaled version. The visible paths make the difference easier to follow.
The approach holds up as a proof of concept, including with your own optimized SVG files dropped into the demo; it handles most aspect ratios. The code is published as a package named Meanderer on GitHub and npm, and it can be pulled from the unpkg CDN for use in CodePen.



