IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

offset-path

CSS-Tricks 2026-06-14 05:11:01 累计浏览 2 次
本机暂存

The offset-path property in CSS defines a movement path for an element to follow during animation.

This property began life as motion-path. This, and all other related motion-* properties, are being renamed offset-* in the spec. We’re changing the names here in the almanac. If you want to use it right now, probably best to use both syntaxes.

Here’s an example using the SVG path syntax:

.thing-that-moves {
  /* "Old" syntax. Available in Blink browsers as of ~October 2015 */
  motion-path: path("M 5 5 m -4, 0 a 4,4 0 1,0 8,0 a 4,4 0 1,0 -8,0");
 
  /* Currently spec'd syntax. Should be in stable Chrome as of ~December 2016 */
  offset-path: path("M 5 5 m -4, 0 a 4,4 0 1,0 8,0 a 4,4 0 1,0 -8,0");
}

This property cannot be animated, rather it defines the path for animation. We use the offset-path property to create the animation. Here’s a simple example of animating motion-offset with a @keyframes animation:

.thing-that-moves {
  offset-path: path('M 5 5 m -4, 0 a 4,4 0 1,0 8,0 a 4,4 0 1,0 -8,0');
  animation: move 3s linear infinite;
}

@keyframes move {
  100% { 
    motion-offset: 100%;   /* Old */
    offset-distance: 100%; /* New */
  }
}
CodePen Embed Fallback

In this demo, the orange circle is being animated along the offset-path we set in CSS. We actually drew that path in SVG with the exact same path() data, but that’s not necessary to get the motion.

Say we drew a funky path like this in some SVG editing software:

We would find a path like:

The d attribute value is what we’re after, and we can move it straight to CSS and use it as the offset-path:

CodePen Embed Fallback

Note the unitless values in the path syntax. If you’re applying the CSS to an element within SVG, those coordinate values will use the coordinate system set up within that SVG’s viewBox. If you’re applying the motion to some other HTML element, those values will be pixels.

Also note we used a graphic of a finger pointing to show how the element is automatically rotated so it kinda faces forward. You can control that with offset-rotate:

.mover {
  offset-rotate: auto; /* default, faces forward */
  offset-rotate: reverse; /* faces backward */
  offset-rotate: 30deg; /* set angle */
  offset-rotate: auto 30deg; /* combine auto behavior with a set rotation */
}

Values

As best as we can tell, path() and none are the only working values for offset-path.

The offset-path property is supposed to accept all the following values.

Here’s some tests:

CodePen Embed Fallback

Even telling an SVG element to reference a path definied the same SVG via url() doesn’t seem to work.

With the Web Animations API

Dan Wilson explored some of this in Future Use: CSS Motion Paths. You have access to all this same stuff in JavaScript through the Web Animations API. For example, say you’ve defined a offset-path in CSS, you can still control the animation through JavaScript:

CodePen Embed Fallback

More Examples

Heads up! A lot of these were created before the change from motion-* naming to offset-*. Should be pretty easy to fix them if you’re so inclined.

CodePen Embed Fallback
CodePen Embed Fallback
CodePen Embed Fallback
CodePen Embed Fallback
CodePen Embed Fallback
CodePen Embed Fallback
CodePen Embed Fallback

Browser support

Is There Another Way to Do This?

Our own Sarah Drasner wrote about SMIL, SVG’s native method for animations, and how animateMotion is used to animate objects along a SVG path. It looks like:

GreenSock is another way though. Sarah talks about this in GSAP + SVG for Power Users: Motion Along A Path (SVG not required). Example:

CodePen Embed Fallback

Related Properties


offset-path originally handwritten and published with love on CSS-Tricks. You should really get the newsletter as well.

同分类推荐文章

  1. Why Isn’t My 3D View Transition Working? (2026-06-12 20:53:41)
  2. 全新的CSS border-shape属性简介 (2026-06-10 11:00:06)
  3. Scroll-Driven, Scroll-Triggered, Scroll States, and View Transitions (2026-06-08 21:00:34)

查看更多 前端 文章 →

建议继续学习

  1. 50个活力和动感的网页设计-颜色的灵感 (累计阅读 34,380)
  2. 视觉设计前瞻实用性研究(PNVD) 第二期 (累计阅读 12,921)
  3. 各公司对前端开发的职位描述 (累计阅读 10,360)
  4. iframe大小自适应 (累计阅读 9,996)
  5. 浏览器的渲染原理简介 (累计阅读 8,302)
  6. 解决IE6从Nginx服务器下载图片不Cache的Bug (累计阅读 8,295)
  7. 程序员眼里IE浏览器是什么样的 (累计阅读 7,956)
  8. 2010网页设计趋势 (累计阅读 7,754)
  9. Web前端工程师编程能力飞升之路 (累计阅读 7,639)
  10. 颜色的代码表达式 (累计阅读 7,619)