{"id":243521,"date":"2016-07-19T07:47:23","date_gmt":"2016-07-19T14:47:23","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=243521"},"modified":"2019-12-04T07:50:01","modified_gmt":"2019-12-04T14:50:01","slug":"offset-path","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/properties\/o\/offset-path\/","title":{"rendered":"offset-path"},"content":{"rendered":"
This property began life as The This property cannot be animated, rather it defines the path for animation. We use the See the Pen Simple Example of Animating Along a Path<\/a> by CSS-Tricks (@css-tricks<\/a>) on CodePen<\/a>.<\/p>\n In this demo, the orange circle is being animated along the Say we drew a funky path like this in some SVG editing software:<\/p>\n We would find a path like:<\/p>\n The See the Pen zEpLRo<\/a> by CSS-Tricks (@css-tricks<\/a>) on CodePen<\/a>.<\/p>\n 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 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 As best as we can tell, The Clippy<\/a> is an awesome tool for generating Basic Shape values, by the way.\n <\/li>\n Here’s some tests:<\/p>\n See the Pen motion-path values test<\/a> by CSS-Tricks (@css-tricks<\/a>) on CodePen<\/a>.<\/p>\n Even telling an SVG element to reference a path definied the same SVG via Dan Wilson explored some of this in Future Use: CSS Motion Paths<\/a>. You have access to all this same stuff in JavaScript through the Web Animations API. For example, say you’ve defined a See the Pen CSS MotionPath<\/a> by CSS-Tricks (@css-tricks<\/a>) on CodePen<\/a>.<\/p>\n 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.<\/p>\n See the Pen Whoosh!<\/a> by Merih Akar (@merih<\/a>) on CodePen<\/a>.<\/p>\n See the Pen pJarJO<\/a> by Eric Willigers (@ericwilligers<\/a>) on CodePen<\/a>.<\/p>\n See the Pen scalextric car on motion-path<\/a> by Kseso (@Kseso<\/a>) on CodePen<\/a>.<\/p>\n See the Pen CSS Motion Path Airplane<\/a> by Ali Klein (@AliKlein<\/a>) on CodePen<\/a>.<\/p>\n See the Pen CSS Animate on SVG Path<\/a> by \u4e00\u4e1d (@yisi<\/a>) on CodePen<\/a>.<\/p>\n See the Pen Motion Path Infinity<\/a> by Dan Wilson (@danwilson<\/a>) on CodePen<\/a>.<\/p>\n See the Pen CSS Motion Path Spiral<\/a> by Dan Wilson (@danwilson<\/a>) on CodePen<\/a>.<\/p>\n See the Pen zGzJYd<\/a> by \u4e00\u4e1d (@yisi<\/a>) on CodePen<\/a>.<\/p>\n The This browser support data is from Caniuse<\/a>, which has more detail. A number indicates that browser supports the feature at that version and up.<\/p><\/div>motion-path<\/code>. This, and all other related motion-* properties, are being renamed offset-* in the spec<\/a>. We’re changing the names here in the almanac. If you want to use it right now, probably best to use both syntaxes.<\/p>\n
offset-path<\/code> property in CSS defines a movement path for an element to follow during animation. Here’s an example using the SVG path syntax:<\/p>\n
.thing-that-moves {\r\n \/* \"Old\" syntax. Available in Blink browsers as of ~October 2015 *\/\r\n 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\");\r\n \r\n \/* Currently spec'd syntax. Should be in stable Chrome as of ~December 2016 *\/\r\n 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\");\r\n}<\/code><\/pre>\n
motion-offset<\/code><\/a> property to create the animation. Here’s a simple example of animating motion-offset with a @keyframes animation:<\/p>\n
.thing-that-moves {\r\n 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');\r\n animation: move 3s linear infinite;\r\n}\r\n\r\n@keyframes move {\r\n 100% { \r\n motion-offset: 100%; \/* Old *\/\r\n offset-distance: 100%; \/* New *\/\r\n }\r\n}<\/code><\/pre>\n
offset-path<\/code> we set in CSS. We actually drew<\/em> that path in SVG with the exact same
path()<\/code> data, but that’s not necessary to get the motion. <\/p>\n
<path d=\"M18.45,58.46s52.87-70.07,101.25-.75,101.75-6.23,101.75-6.23S246.38,5.59,165.33,9.08s-15,71.57-94.51,74.56S18.45,58.46,18.45,58.46Z\" \/><\/code><\/pre>\n
d<\/code> attribute value is what we’re after, and we can move it straight to CSS and use it as the
offset-path<\/code>:<\/p>\n
viewBox<\/code>. If you’re applying the motion to some other HTML element, those values will be pixels. <\/p>\n
offset-rotate<\/code>:<\/p>\n
.mover {\r\n offset-rotate: auto; \/* default, faces forward *\/\r\n offset-rotate: reverse; \/* faces backward *\/\r\n offset-rotate: 30deg; \/* set angle *\/\r\n offset-rotate: auto 30deg; \/* combine auto behavior with a set rotation *\/\r\n}<\/code><\/pre>\n
Values<\/h3>\n
path()<\/code> and
none<\/code> are the only working<\/em> values for
offset-path<\/code>.<\/strong><\/p>\n
offset-path<\/code> property is supposed to accept all the following values.<\/p>\n
\n
path()<\/code>: Specifies a path in the SVG coordinates syntax<\/li>\n
url<\/code>: References the ID of an SVG element to be used as a movement path<\/li>\n
basic-shape<\/code>: Specifies a shape in accordance to the CSS Shapes specification<\/a>, which includes:\n
\n
circle()<\/code><\/li>\n
ellipse()<\/code><\/li>\n
inset()<\/code><\/li>\n
polygon()<\/code><\/li>\n<\/ul>\n
none<\/code>: Specifies no motion path at all<\/li>\n<\/ul>\n
url()<\/code> doesn’t seem to work<\/a>. <\/p>\n
With the Web Animations API<\/h3>\n
offset-path<\/code> in CSS, you can still control the animation through JavaScript:<\/p>\n
More Examples<\/h3>\n
Browser support<\/h3>\n
offset-path<\/code> property is still considered an experimental feature at the time of this writing. If the current lack of browser support makes you hesitant to use it on a project, then you might want to consider using GSAP<\/a> for this level of animation, which Sarah also covers in her post<\/a>. This will offer you the widest browser support at the moment.<\/p>\n
Desktop<\/h4>