Align Rotated DOM Elements to a Circular Path
If I have an imanginary circle with a given radius, I want to align a group of elements around that
circle so each element is positioned outside the circle but touching a point on the circle.
There are two alignment options I'm interested around a this circle:
- Attaching a fixed point on an element to the circle. This requires the element to be rotated to maintain this relative position around the circle. This implies that the browser can support CSS3 transforms.
- Keeping a moving point attached to te circle. This method doesn't require a CSS3 transform to rotate the elements but still maintains the imaginary circle.
In the first example, we need to find a point on the circle and then align the element relative to
its top/left DOM position to that point. Additionally, the element's bottom/middle point should
appear to be aligned to the circle. To acheive this effect, we can use a CSS transform to both
rotate the element and then translate it so the middle/bottom point is at tranform axis' origin which
will be the same as the top/left DOM corner. Below, the reference circle is drawn and the top/left
corners are plotted in green. You can turn off the reference points and transforms to see how each
component affects the alignment.
Aligning an element to a circle without using a fixed point is a bit tricky. The top/left corner needs to
be positioned relative to a point on the circle such that some point on the element appears to be
properly touching the circle. You could acheive this with some fun math or you can split the circle into
four arcs and insert sides that are the same height/width of the element. This creates a rectangle with
rounded corners aligned to the circle in a way that you can use it as a guild to align the elements.