I am trying to make a circle like this. I was able to make it in the fiddle, but the problem is that I need each orange side to be a link and I can't do it with borders. If anyone can help me with this I will be really grateful.
#circle {
width: 200px;
height: 200px;
border-radius: 50%;
background: green;
}
#incircle {
width: 100px;
height: 100px;
border-radius: 50%;
border: 50px dotted orange;
}
<div id="circle">
<div id="incircle"></div>
</div>
This sounds like a job for SVG. It has its own type of
<a>
element that can contain arbitrary shapes.You could use a map, like this :
The key to creating a circle with segments is to find points along the circle which would be used in the SVG
path
elements as coordinates. Finding points on a circle can be done easily using trigonometric equations if we know the angles.The angles depend on the no. of segments that we have to create. The generic formula is (360 / no. of segments). So to create a circle with 6 segments, the angle covered by each of the segment would be 60 degrees. The first segment would cover from 0 to 60 degrees, second from 60 to 120 degrees and so on.
Demo of Circle with 6 Segments:
Below table shows how the points are calculated for a circle with 6 segments (where radius of circle is 50, center point is 55,55):
Once the points are calculated, coding the
path
itself is simple. The path should start and end at the center point (which is 50,50), from the center point, we should first draw a line to From Point and from there draw an arc to the To Point. Below is how a samplepath
would look like:Demo of Circle with 12 Segments:
For a circle with 12 segments, each segment would cover 30 degrees and so the points would be calculated as in the below table:
Circle with an unsegmented inner portion:
If it should look as though a portion of the circle (with a smaller radius) in center looks unsegmented and if that inner portion need not be transparent, just add an extra circle element at the end within the SVG.
Different Background for each Segment:
If each of the segments should have a different background to them then just add the
fill
attribute to eachpath
element.Demo with a transparent inner portion:
If the center portion cannot have a solid color then the whole thing becomes more complex because we can no longer start and end the path at the center point. In such cases, we have to find points on both the outer circle and the inner circle like below:
In this case, the
path
has to start from the "From(Inner)" and end at same point, from the start a line should be drawn to "From(Outer)", then an arc to "To(Outer)", a line to "To(Inner)" and an arc to "From (Inner)".Making each segment a clickable link:
This is pretty simple to do once the shape itself has been created. Like in chipChocolate.py's answer, just wrap each path within a SVG anchor tag (
<a xlink:href="#">
where#
should be replaced the URL of the linked page).Adding text within the shape:
Text addition in SVG is slightly more complex because again we have to specify the point where the text should be placed. If the text is reasonably small (say a few characters), then we can again find points on the circle such that the angle is exactly in the middle of the segment and use it. The radius could be set such that it is half the parent circle's radius (if there is no unsegmented portion) or such that it is half way between the inner circle and outer circle. The
text-anchor
,dominant-baseline
settings that are added via CSS would make sure that the text is positioned in such a way that both the horizontal and vertical center of the text matches with the specified point.If the text is large (and needs to wrap around), then extra handling should be done because contents within the SVG
text
tag won't get wrapped around automatically.Point calculation for circle with 6 segments and no central unsegmented area:
Point calculation for circle with 6 segments and central unsegmented area:
Dynamic creation with JavaScript:
Below is a rough JS based implementation in order to create the segments dynamically. The function takes four arguments - the X coordinate of the circle's center, the Y coordinate of its center, the radius of the circle and the no. of segments/slices.
JS sample doesn't cover the examples with an unsegmented inner circle but that can be achieved by extending this.
CSS-only approach
NOTE: Markup can be significantly reduced by using pseudoelements, which I haven't currently used.
You can use SVG, but this can be made with CSS and HTML alone.
What I did was create
12
semicircles (by addingoverflow: hidden;
to parent container). I then created separate groups of6
semicircles.The angles at the center should be
30deg
each (360/12
). To achieve this, we have to rotate the semicircles from their original circle's center. We can do this withtransform-origin: 50% 100%;
Now you just have to rotate/flip the second group of
6
semicircles to complete design.Finally, add a central green circle to complete the design.
Output on Firefox, Google Chrome and IE:
Try this pure css:
You could use
svg
'sarc
s to create the sections andsvg
's anchor(equivalent to HTML anchor tags) tags for the links.You could also stretch or resize the
svg
.