Arranging Elements on a Circle with sin() and cos() in CSS
With sin() and cos() in CSS, you can arrange elements at equal intervals on a circle. You don't need to compute coordinates in JavaScript or write a position for each element. Even when the number of elements changes, you don't need to rewrite the CSS.
.ring > li {
--angle: calc(360deg / sibling-count() * (sibling-index() - 1) - 90deg);
position: absolute;
left: 50%;
top: 50%;
translate: calc(cos(var(--angle)) * var(--r)) calc(sin(var(--angle)) * var(--r));
}
The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened the page with Playwright and measured the center position of each element with getBoundingClientRect(). I did not test rendering in browsers other than Chromium, such as Firefox or Safari. The browser versions for each browser are the values from compatibility data (@mdn/browser-compat-data 8.1.8).

- 1. The position on a circle is found by multiplying the radius by cos() and sin()
- 2. Stack all elements at the center, then use translate to shift them onto the circle
- 3. To place the first item at the top, subtract 90deg from the angle
- 4. Get the index and sibling count with sibling-index() and sibling-count()
- 5. For a semicircle, divide by one less than the count so that both ends get an item
- 6. Leave transform free, and the position will not shift when you scale an element
- 7. To rotate elements around the circle, register the angle with @property
- 8. Browser support
The position on a circle is found by multiplying the radius by cos() and sin()
A point at distance r from the center of a circle, in the direction of angle θ, is at horizontal position r × cos(θ) and vertical position r × sin(θ). CSS cos() and sin() are functions that return cos(θ) and sin(θ).
They return unitless numbers from −1 to 1. To turn them into lengths, multiply by a length such as px. The table below shows the results when I put these expressions in width in the test environment.
| Expression | Computed value of width |
|---|---|
calc(sin(30deg) * 100px) |
50px |
calc(cos(60deg) * 100px) |
50px |
calc(sin(0.25turn) * 100px) |
100px |
calc(sin(pi / 2) * 100px) |
100px |
calc(sin(1) * 100px) |
84.1406px |
You can write angles with turn or rad as well as deg. Unitless numbers are treated as radians. sin(1) is the sine of 1 radian (about 57 degrees), so it is about 0.841. pi is a keyword that represents the constant π, and sin(pi / 2) gave 1, the same as sin(90deg).
When I wrote width: sin(30deg); after width: 33px;, the second declaration was invalid and the width stayed at 33px. A unitless number is not a valid width value, so always multiply by a length.
Stack all elements at the center, then use translate to shift them onto the circle
The idea is to first stack all elements at the center of the circle, then shift each one to its position for its angle.
<ul class="ring">
<li>1</li>
<li>2</li>
<!-- … -->
<li>8</li>
</ul>
.ring {
--r: 120px; /* circle radius */
--size: 48px; /* size of each element */
position: relative;
width: 300px;
height: 300px;
}
.ring > li {
--angle: calc(360deg / sibling-count() * (sibling-index() - 1) - 90deg);
position: absolute;
left: 50%;
top: 50%;
width: var(--size);
height: var(--size);
margin: calc(var(--size) / -2);
translate: calc(cos(var(--angle)) * var(--r)) calc(sin(var(--angle)) * var(--r));
}
left: 50% and top: 50% place the top-left corner of each element at the center of the parent. The negative margin then moves it back by half its size, so the element's center overlaps the parent's center. Finally, translate shifts it horizontally by cos() × radius and vertically by sin() × radius.
With eight li elements, every element was placed 120.00px from the center, at 45-degree intervals. The table shows the position of each element's center as seen from the parent's center (right and down are positive).
| Item | Horizontal | Vertical |
|---|---|---|
| 1 | 0.00px | −120.00px |
| 2 | 84.85px | −84.85px |
| 3 | 120.00px | 0.00px |
| 4 | 84.85px | 84.85px |
| 5 | 0.00px | 120.00px |
| 6 | −84.85px | 84.85px |
| 7 | −120.00px | 0.00px |
| 8 | −84.85px | −84.85px |
To place the first item at the top, subtract 90deg from the angle
In math graphs, up is positive, but in CSS coordinates, down is positive. So an angle of 0deg is to the right (the 3 o'clock direction), and the angle increases clockwise.
When I arranged four items without - 90deg, the first went to the right, the second below, the third to the left, and the fourth above. If you want the first item directly above (the 12 o'clock direction), as on a clock face or in a menu, subtract 90deg from the angle as in the example above.
Get the index and sibling count with sibling-index() and sibling-count()
The angle between adjacent elements is 360deg divided by the number of elements. You can get each element's position with sibling-index(), and the total number of siblings with sibling-count(). sibling-index() counts from 1, so subtracting 1 makes the first item's angle 0.
When the number of elements changes, the spacing also changes automatically. I added one li with JavaScript to the eight-item list, and the nine items were rearranged at 40-degree intervals. Removing three more gave six items at 60-degree intervals. In both cases, the first item stayed directly above.
sibling-count() counts all elements that share the same parent. If you put a heading placed at the center of the circle in the same parent, it is also counted as one item, and the spacing shifts. In the test environment, I added one <p> after four arranged elements. The four were then placed at 72-degree intervals (five equal parts) instead of 90-degree intervals, leaving a large gap on the left side of the circle. Put only the elements to be arranged in one parent, and place other elements outside it.
In browsers that do not support sibling-index() and sibling-count(), you can pass each element's index and the total count as custom properties through the style attribute in the HTML. In this case, --i counts from 0.
<ul class="ring" style="--n: 8">
<li style="--i: 0">1</li>
<li style="--i: 1">2</li>
<!-- … -->
</ul>
.ring > li {
--angle: calc(360deg / var(--n) * var(--i) - 90deg);
}
In the test environment, the elements were placed at the same positions as with the sibling-index() version. When you add or remove elements, you also need to update --i and --n.
For a semicircle, divide by one less than the count so that both ends get an item
When arranging elements on part of a circle, divide the angle range by "the number of elements − 1", not by the number of elements. On a full circle, the item after the last one returns to the first, so you divide by the number of elements. On a semicircle, elements sit at both the start and the end, so there is one fewer gap than there are elements.
.half > li {
--angle: calc(180deg / (sibling-count() - 1) * (sibling-index() - 1) + 180deg);
}
The angle starts at 180deg (left) and goes clockwise to 360deg (right), so the elements fill the top half of the circle. With five items in the test, item 1 was at the left end, item 3 at the top, and item 5 at the right end, at 45-degree intervals.

Leave transform free, and the position will not shift when you scale an element
This article sets the position with the translate property rather than transform. The translate property can be specified separately from the transform property.
If you set the position with transform: translate(…), specifying transform: scale(1.5) on hover overwrites the position as well. In the test, the first item grew to 72px, and at the same time it returned to the center of the circle.
If you set the position with the translate property, you can use transform freely. Similarly, when I applied transform: scale(1.5) to the first item, its size went from 48px to 72px, and its center stayed at 120px directly above.
To rotate elements around the circle, register the angle with @property
Add an offset --offset to the angle, and change --offset from 0deg to 360deg with @keyframes. The elements will then rotate around the circle. Before doing this, register --offset as an angle with @property.
@property --offset {
syntax: "<angle>";
inherits: true;
initial-value: 0deg;
}
.ring {
animation: spin 8s linear infinite;
}
@keyframes spin {
to { --offset: 360deg; }
}
@media (prefers-reduced-motion: reduce) {
.ring { animation: none; }
}
.ring > li {
--angle: calc(360deg / sibling-count() * (sibling-index() - 1) - 90deg + var(--offset));
}
I measured the position of the first item by adding paused to the 8-second animation and using a negative animation-delay to stop it at a specific point in time. With @property registered, --offset was 45deg after 1 second, and the item had moved 45 degrees from directly above to the upper right. At 2 seconds, --offset was 90deg, and the item was on the right (the 3 o'clock direction).
Without @property, the elements did not move from directly above. An unregistered --offset is not interpolated, so it switched from 0deg to 360deg all at once at 4 seconds (halfway through). Since 360deg is one full turn and lands on the same position, it looked as if the elements had stopped.
When I reproduced the reduced-motion setting (prefers-reduced-motion: reduce) with Playwright, animation: none took effect, and the first item was directly above at every point in time.
Browser support
According to the compatibility data, support is as follows.
| Browser | sin() and cos() |
sibling-index() and sibling-count() |
@property |
|---|---|---|---|
| Chrome and Edge (including Chrome on Android) | From 111 | From 138 | From 85 |
| Firefox (including Android) | From 108 | From 154 | From 128 |
| Safari (including iOS) | From 15.4 | From 26.2 | From 16.4 |
Compared with sin() and cos(), sibling-index() and sibling-count() are newer features. If you need to support older browsers, pass --i and --n through the style attribute instead.





