Moving an Element in a Circle by kirupa (https://www.kirupa.com/me/index.htm) | filed under Web Animation (https://www.kirupa.com/html5/learn_animation.htm) TL;DR: To move an element in a circle with CSS, we give it offset-path: circle(100px at 50% 50%) and animate offset-distance from 0% to 100%. A rotate, translate, and counter-rotate transform does the same job in older browsers, and JavaScript can place it every frame at cx + r × cos(angle) and cy + r × sin(angle). - A circle() path starts at its rightmost point and runs clockwise, and its at 50% 50% center is measured against the containing block, so the parent needs position: relative. - offset-rotate: auto turns the element to face the way it's moving, which leaves it upside down at the bottom of the circle, while offset-rotate: 0deg keeps it upright. - Adding 1.5 degrees every frame runs twice as fast on a 120Hz screen as on a 60Hz one, so the JavaScript angle comes from elapsed time instead. - circle() inside offset-path works in Chrome and Edge 116, Firefox 122, and Safari 18, and the transform trick covers older browsers. Every so often, something on our page needs to go around in circles. It might be a loading indicator, a moon orbiting a planet in a game, or a ring of icons circling a logo. Moving an element along a circle sounds like it should take one line of CSS, and these days, it almost does. There are actually three good ways to pull it off, and each one is the right pick in a different situation. We are going to look at all three: CSS Motion Path, a classic trick with CSS transforms, and JavaScript with a little bit of trigonometry (the friendly kind, I promise). By the end, we will know how each one works and when to reach for it. Onwards! Three Ways to Go Around in Circles Before we look at any code, let's play with what we are building towards. In this example, three labels orbit the same point, and each label is moved by a different technique: Interactive example: Three labels orbit one point on a dashed circle (https://www.kirupa.com/animations/examples/circular_path_lab.htm) Try it: 1. Drag the Radius and One lap sliders 2. Check Keep upright 3. Press Pause, and then Play All three labels follow the same circle at the same speed, 120 degrees apart. What is different is how each one gets there: - The offset-path label rides a path that we describe in CSS. - The transforms label gets spun out from the center by a stack of CSS transforms. - The JS + cos & sin label is placed by our own JavaScript on every frame. We will look at all three, starting with the one that takes the least code. How Do We Move an Element in a Circle With CSS? We give our element offset-path: circle(100px at 50% 50%) and animate its offset-distance from 0% to 100%. The browser will then move our element's center around a circle with a 100px radius, one lap for every run of our animation. These properties are part of CSS Motion Path, and here they are moving a 40px dot around inside a 300px by 300px .orbit div: .orbit { position: relative; width: 300px; height: 300px; } .dot { position: absolute; width: 40px; height: 40px; border-radius: 50%; background-color: #e0605a; offset-path: circle(100px at 50% 50%); offset-rotate: 0deg; animation: orbit 4s linear infinite; } @keyframes orbit { from { offset-distance: 0%; } to { offset-distance: 100%; } } The offset-path property is the road our dot travels on, and offset-distance is how far along that road our dot is. Here is where our percentages land: [Image: A circle with a radius of 100px and a dot at four offset-distance values. 0% is the rightmost point, where the path starts, and the path runs clockwise: 25% is the bottom, 50% is the left, and 75% is the top. The circle is 2 × π × 100 = 628.3px around, so 0% is 0px along the path, 25% is 157.1px, 50% is 314.2px, and 75% is 471.2px.] (https://www.kirupa.com/animations/images/orbit_offset_distance.png) (https://www.kirupa.com/animations/images/orbit_offset_distance.png?v=b6468f615f14) A circle() path starts at its rightmost point and heads clockwise. Our circle is 628.3px around, so every 25% is about 157px of travel. The linear timing keeps our dot at a steady speed the whole way around. Finding the Circle's Center Where does the center of our circle come from? The at 50% 50% part is measured against our dot's containing block, which is the box an element positions itself against. For our absolutely positioned dot, that's the nearest ancestor that isn't position: static, and this is why .orbit has position: relative: [Image: The .orbit box with a circle of radius 100px centered 150px from its left and 150px from its top. circle(100px at 50% 50%) is measured inside .orbit, so the center is (150, 150). A dashed outline in the top-left corner marks the dot's normal spot, which doesn't move the circle.] (https://www.kirupa.com/animations/images/orbit_reference_box.png) (https://www.kirupa.com/animations/images/orbit_reference_box.png?v=2ddf734e5cf7) Our circle's center lands at (150, 150), the middle of .orbit, and the spot where our dot would normally sit doesn't matter at all. If we forget position: relative, though, our dot measures against the whole page and happily orbits the middle of the browser window instead. Keeping Our Element Upright By default, offset-rotate is auto, which turns our element to face the way it is moving, like a car on a racetrack. For a round dot, nobody can tell. For anything with a top and a bottom, it's pretty noticeable: [Image: Two copies of the same circle with a smiley at 0%, 25%, 50%, and 75%. On the left, with offset-rotate: auto, the smiley faces the way it's moving, so it lies on its side at the right and left and is upside down at the bottom. On the right, with offset-rotate: 0deg, the smiley stays upright the whole way around.] (https://www.kirupa.com/animations/images/orbit_offset_rotate.png) (https://www.kirupa.com/animations/images/orbit_offset_rotate.png?v=6ecdb147a859) With auto, our smiley lies on its side at the left and right and is upside down at the bottom. Setting offset-rotate: 0deg keeps our element upright for the whole trip. A related property, offset-anchor, picks which point of our element rides on the path: [Image: The top of a circle path with a 60 by 60 square on the path point. With offset-anchor: auto, the square's center sits on the path point. With offset-anchor: 0 0, the square's top-left corner sits on the path point instead.] (https://www.kirupa.com/animations/images/orbit_offset_anchor.png) (https://www.kirupa.com/animations/images/orbit_offset_anchor.png?v=090f44e3b6c2) The default of auto uses our element's transform-origin, which is its center unless we change it. For orbiting things, that's what we want. Other Paths: path() and ray() The circle() function isn't our only option. The path() function takes SVG path commands, and ray() describes a line that shoots out from a point at an angle. Both can make the same 100px circle: /* Two arcs that make a 100px circle. Our orbit animation stays the same. */ .dot { offset-path: path("M 250 150 A 100 100 0 1 1 50 150 A 100 100 0 1 1 250 150"); } /* A 100px line that sweeps all the way around */ .dot { offset-path: ray(0deg); offset-distance: 100px; animation: sweep 4s linear infinite; } @keyframes sweep { to { offset-path: ray(360deg); } } The numbers in path() are plain pixels measured from where our dot would normally sit (the top-left corner of .orbit here), so we do the math ourselves. In return, it can trace any shape, like a figure eight. With ray(), our dot stays 100px out while the ray's angle animates. Ray angles start at the top, so that version starts at 12 o'clock. Which Browsers Support offset-path: circle()? Using circle() inside offset-path works in Chrome and Edge 116, Firefox 122, and Safari 18. That puts it in every major browser since Safari 18 shipped in September 2024. Here are the versions for the pieces we have seen, as of December 2, 2026: Feature | Chrome and Edge | Firefox | Safari offset-path: path() | 64 | 72 | 16 offset-path: ray() | 116 | 122 | 16 offset-path: circle() | 116 | 122 | 18 offset-anchor | 116 | 72 | 16 A browser that doesn't understand circle() ignores our whole offset-path line, so our dot just sits still. Nothing breaks. If that matters, we can wrap our motion path rules in @supports (offset-path: circle(1px)) and fall back to the transform trick we will look at next. MDN's offset-path (https://developer.mozilla.org/en-US/docs/Web/CSS/offset-path) page has the latest details. Spinning With rotate() and translate() Before CSS Motion Path, the usual way to move something in a circle was a CSS transforms trick that works in every browser from the last decade or so. We put our dot right on the center and chain three transforms together: [Image: Three steps for a smiley that starts at the circle's center. 1: rotate(60deg) turns the smiley and its x-axis by 60 degrees. 2: translateX(100px) slides it 100px along that turned x-axis, onto the circle. 3: rotate(-60deg) turns it back upright without moving it.] (https://www.kirupa.com/animations/images/orbit_transform_steps.png) (https://www.kirupa.com/animations/images/orbit_transform_steps.png?v=75764713cde1) Transforms apply in order, and each one works in the space the one before it left behind. Our rotate(60deg) turns our dot along with its x-axis. The translateX(100px) slides our dot along that turned x-axis and onto the circle. The last rotate(-60deg) undoes the first turn, so our dot is upright again. Animating the first angle to 360deg and the last one to -360deg will take our dot all the way around: .dot { left: 130px; top: 130px; animation: spin 4s linear infinite; } @keyframes spin { from { transform: rotate(0deg) translateX(100px) rotate(0deg); } to { transform: rotate(360deg) translateX(100px) rotate(-360deg); } } These lines replace the offset-path, offset-rotate, and animation lines from our earlier .dot. A left and top of 130px puts our 40px dot's center at (150, 150). Think of a tetherball. The pole is our center, the rope is our 100px translate, and the first rotate is how far around the ball has swung. Without the last rotate(), our dot turns as it goes, with its right side pointing away from the center. We go much deeper into this technique, including how to space out several items with negative delays, in Animating Items Around a Point (https://www.kirupa.com/animations/rotating_items_around_a_point.htm). How Do We Move an Element in a Circle With JavaScript? In JavaScript, we work out our element's position on every frame with x = cx + r × cos(angle) and y = cy + r × sin(angle), where (cx, cy) is the center and r is the radius. We move our element there with a transform, and requestAnimationFrame calls us again for the next frame. It's more work, but our code controls everything. From an Angle to an x and a y The Math.cos and Math.sin functions do the heavy lifting here. When we give both of them the same angle, they return a point on a circle whose radius is 1, with cos as the x and sin as the y: [Image: A circle with a radius of 1, drawn the way the screen does it, with y pointing down. Each point is labeled (cos, sin): 0° is (1, 0), 45° is (0.71, 0.71), 90° is (0, 1) at the bottom, 135° is (-0.71, 0.71), 180° is (-1, 0), 225° is (-0.71, -0.71), 270° is (0, -1) at the top, and 315° is (0.71, -0.71).] (https://www.kirupa.com/animations/images/orbit_unit_circle.png) (https://www.kirupa.com/animations/images/orbit_unit_circle.png?v=ee74a6a7d75b) At 0 degrees, we are at (1, 0) on the right, and at 45 degrees, we are about 0.71 along both. To get from this tiny circle to our real one, we multiply by our radius and add the center: [Image: Our 300 by 300 orbit with (0, 0) at its top-left corner and the circle's center at (150, 150). At an angle of 30° with r = 100, x = cx + r × cos(angle) = 150 + 86.6 = 236.6, and y = cy + r × sin(angle) = 150 + 50 = 200.] (https://www.kirupa.com/animations/images/orbit_xy_formula.png) (https://www.kirupa.com/animations/images/orbit_xy_formula.png?v=81796746eba0) At 30 degrees, r × cos(angle) is 86.6 and r × sin(angle) is 50, so our dot lands at (236.6, 200). There is one catch. Both functions want radians, where a full circle is 2 × π instead of 360, so we multiply our degrees by π / 180 first. Why Does Our Dot Go Clockwise? Our dot goes clockwise because the y-axis on a screen points down, so a bigger y is farther down the page. That's also why 90 degrees sits at the bottom of our circle instead of at the top, where it lives in math class: [Image: Two sets of axes. In math class, y points up, so going from 0° to 90° moves a point counterclockwise to the top. On the screen, y points down, so the same 0° to 90° moves the point clockwise to the bottom.] (https://www.kirupa.com/animations/images/orbit_screen_vs_math.png) (https://www.kirupa.com/animations/images/orbit_screen_vs_math.png?v=b72bc51286b2) On paper, a growing angle moves a point counterclockwise. On the screen, the very same formula moves our dot clockwise. That matches circle() and ray(), so all three techniques go the same way. Measuring Time Instead of Counting Frames Next, our angle needs to grow. The tempting approach is to add a little to it on every frame, but frames don't arrive at the same rate on every screen. Lots of phones and laptops draw 120 frames a second, while plenty of monitors draw 60: [Image: Four dials showing where the dot is after 1 second. Adding 1.5° every frame reaches 90° on a 60Hz screen but 180° on a 120Hz screen, which is 2x too fast. Adding 90° every second reaches 90° on both.] (https://www.kirupa.com/animations/images/orbit_frame_vs_time.png) (https://www.kirupa.com/animations/images/orbit_frame_vs_time.png?v=74214e5c31ba) Adding 1.5 degrees per frame gets our dot to 90 degrees after one second at 60Hz, but to 180 degrees at 120Hz. That's double the speed from the same code! If we work out the angle from the time that has passed instead, at 90 degrees per second, both screens get to 90 degrees. Luckily, requestAnimationFrame passes our function a timestamp in milliseconds: const orbit = document.querySelector(".orbit"); const dot = document.querySelector(".dot"); const radius = 100; const lapTime = 4000; const cx = orbit.clientWidth / 2; const cy = orbit.clientHeight / 2; let start = null; function move(timestamp) { if (start === null) { start = timestamp; } const elapsed = timestamp - start; const angle = (elapsed / lapTime) * 2 * Math.PI; const x = cx + radius * Math.cos(angle); const y = cy + radius * Math.sin(angle); dot.style.transform = `translate(${x - 20}px, ${y - 20}px)`; requestAnimationFrame(move); } requestAnimationFrame(move); Our .dot here sits at left: 0 and top: 0 with no CSS animation. On each frame, the elapsed milliseconds divided by our lapTime of 4000 is how many laps we have done, and multiplying by 2 × π turns laps into radians. We subtract 20 (half of our 40px dot) to center it on the point. Ensuring Consistent Animation Speeds (https://www.kirupa.com/animations/ensuring_consistent_animation_speeds.htm) goes deeper on timestamps, and Animating with requestAnimationFrame (https://www.kirupa.com/html5/animating_with_requestAnimationFrame.htm) covers the basics. Ellipses and More Than One Item Since we are doing the math, changing the shape is easy. An ellipse is a circle with one radius for x and another for y: [Image: An ellipse with rx = 140 and ry = 70. The x position uses x = cx + rx × cos(angle) and the y position uses y = cy + ry × sin(angle): the same angle with two radii.] (https://www.kirupa.com/animations/images/orbit_ellipse.png) (https://www.kirupa.com/animations/images/orbit_ellipse.png?v=d4991f9487cb) With an rx of 140 and an ry of 70, our dot travels twice as far sideways as it does up and down. To put several items on one circle, we give each one its own starting angle: [Image: Three dots on one circle, spaced with start = i × 360° / 3. Item 0 starts at 0°, item 1 at 120°, and item 2 at 240°.] (https://www.kirupa.com/animations/images/orbit_start_offsets.png) (https://www.kirupa.com/animations/images/orbit_start_offsets.png?v=df88e724b3dd) Our three items start at 0, 120, and 240 degrees. Every frame adds the same amount to all of them, so they keep their spacing forever. Here is what our move function will look like with both changes: const dots = document.querySelectorAll(".dot"); const rx = 140; const ry = 70; function move(timestamp) { if (start === null) { start = timestamp; } const angle = ((timestamp - start) / lapTime) * 2 * Math.PI; dots.forEach((dot, i) => { const offset = (i * 2 * Math.PI) / dots.length; const x = cx + rx * Math.cos(angle + offset); const y = cy + ry * Math.sin(angle + offset); dot.style.transform = `translate(${x - 20}px, ${y - 20}px)`; }); requestAnimationFrame(move); } In radians, those offsets are 0, 2π / 3, and 4π / 3. The JavaScript label in our demo runs this same math. What About Reduced Motion? We pause our orbit when the prefers-reduced-motion media query matches, because something circling forever is exactly the kind of motion that can make some people dizzy. People ask for less motion in their device's settings, and this media query is how our page finds out. For both CSS approaches, pausing looks like this: @media (prefers-reduced-motion: reduce) { .dot { animation-play-state: paused; } } Why pause instead of using animation: none? Without the animation, our transform version loses its translateX(100px), and our dot lands right on top of the center. In JavaScript, we check the same setting before starting our loop: const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)"); if (!reduceMotion.matches) { requestAnimationFrame(move); } Our demo starts paused in that case and offers a Play button instead. Respecting Reduced Motion (https://www.kirupa.com/animations/respecting_reduced_motion.htm) covers more patterns like this one. Which Approach Should You Use? For a simple loop that never changes, CSS offset-path is the one to beat. It's a few declarative lines, and the browser does the work. Its catch is that circle() needs Chrome 116, Firefox 122, or Safari 18. The rotate and translate trick works in every browser people still use, so it's the safe pick for older devices, even if stacked transforms are harder to read. JavaScript wins when the path or speed reacts to something, like a slider, the mouse, or game physics, but we own every frame, pausing included. Here is that comparison at a glance: Approach | Best for | Keeping it upright offset-path | Simple loops in modern browsers | offset-rotate: 0deg rotate and translate | The widest browser support | A last rotate() that undoes the first JavaScript with cos and sin | Paths or speeds that react to input | Upright unless we add a rotate() If our project already uses GSAP, its MotionPathPlugin can move an element along any SVG path. That's overkill for one circle, but handy when our orbit is part of a bigger animation. Conclusion Phew! We went around in circles quite a bit (sorry, I had to). With offset-path: circle(), we describe a circle and animate offset-distance. With the rotate and translate trick, we spin our element out from the center and turn it back upright. With JavaScript, cos and sin turn an angle into an x and a y, and growing that angle from elapsed time keeps our speed the same on every screen. If all you need is a never-ending loop, start with offset-path, and save JavaScript for the day your orbit has to react to something. Whichever one you pick, let people who asked for less motion make it stop. There is only so much spinning anybody wants to do! Before you go, here are a few things you can do: 1. Test your knowledge by taking the quiz below. 2. Read the next article in the series, Sliding / Scrolling Animation with Infinite Items (https://www.kirupa.com/animations/infinite_sliding_animation.htm), or check out some related content: - Animating Items Around a Point (https://www.kirupa.com/animations/rotating_items_around_a_point.htm). Orbit a whole ring of items with rotate and translate keyframes. - Ensuring Consistent Animation Speeds (https://www.kirupa.com/animations/ensuring_consistent_animation_speeds.htm). Keep animations moving at the same speed on any frame rate. - Spinning Circular Text (https://www.kirupa.com/animations/spinning_circular_text.htm). Spin text around a circle with HTML, CSS, and SVG. 3. Stuck on something, or curious about a detail we skipped? Ask on the forums (https://forum.kirupa.com/), and we'll help you out. [Mastery Quiz] Test your knowledge! Take the Moving an element in a circle Quiz: https://www.kirupa.com/quizzes/move_element_along_circular_path.htm Practice with 9 multiple-choice questions and an explanation after every answer. [/Mastery Quiz]