Fylgja Donut Chart
A donut chart shows how a whole splits into a few parts. The data is a plain list
of labels and percentages inside a <figure>, and each list item paints its own
slice of the ring with a conic-gradient(). The list doubles as the legend, so the
numbers are always on screen as text, not only as arc lengths.
Preview
- Europe 38%
- North America 31%
- Asia 18%
- South America 8%
- Other 5%
<figure class="donut-chart" style="--m: 0">
<figcaption class="font-bold" style="--my: 0 1rem">Visitors by region</figcaption>
<ul class="relative grid" style="--m: 0">
<li style="--from: 0; --to: 38">Europe <data class="font-bold" value="38">38%</data></li>
<li style="--from: 38; --to: 69">North America <data class="font-bold" value="31">31%</data></li>
<li style="--from: 69; --to: 87">Asia <data class="font-bold" value="18">18%</data></li>
<li style="--from: 87; --to: 95">South America <data class="font-bold" value="8">8%</data></li>
<li style="--from: 95; --to: 100">Other <data class="font-bold" value="5">5%</data></li>
</ul>
</figure>The donut chart CSS
.donut-chart {
--donut-size: 10rem;
--donut-hole: 58%;
--donut-gap: 1deg;
container-type: inline-size;
ul {
align-content: center;
min-block-size: var(--donut-size);
padding-inline-start: calc(var(--donut-size) + 3rem);
}
li {
--slice-color: oklch(68% calc(0.19 * var(--chroma, 1)) var(--hue));
&:nth-child(5n + 1) { --hue: var(--hue-blue); }
&:nth-child(5n + 2) { --hue: var(--hue-teal); }
&:nth-child(5n + 3) { --hue: var(--hue-orange); }
&:nth-child(5n + 4) { --hue: var(--hue-pink); }
&:nth-child(5n + 5) { --hue: var(--hue-blue); --chroma: 0.1; }
&::marker {
color: var(--slice-color);
}
&::before {
--_from: calc(var(--from) * 3.6deg + var(--donut-gap) / 2);
--_to: calc(var(--to) * 3.6deg - var(--donut-gap) / 2);
content: "";
position: absolute;
inset-block: 0;
inset-inline-start: 0;
inline-size: var(--donut-size);
block-size: var(--donut-size);
margin-block: auto;
border-radius: 50%;
background: conic-gradient(
transparent var(--_from),
var(--slice-color) 0 var(--_to),
transparent 0
);
mask: radial-gradient(closest-side, transparent var(--donut-hole), #000 calc(var(--donut-hole) + 1px));
}
}
@container (inline-size < 22rem) {
ul {
padding-block-start: calc(var(--donut-size) + 1rem);
padding-inline-start: 1.5rem;
}
li::before {
inset-block-end: auto;
}
}
}
Usage
- Wrap a
<ul class="relative grid">in a<figure class="donut-chart">, with a<figcaption>for the title. The ring is positioned against the list. - Give each item a label and its share in a
<data>element, for example<data value="38">38%</data>. - Set
--fromand--toon each item, the running total before and after it, in percent. The first item starts at 0 and the last ends at 100. - Add the CSS from under the preview.
Anatomy
| Part | Responsibility |
|---|---|
<figure> / <figcaption> |
Groups the chart and gives it a title. |
<li> |
One slice, and its row in the legend. |
<data> |
The share as readable text, with the raw number in value. |
--from / --to |
Where the slice starts and ends on the ring, in percent. |
li::before |
The slice, drawn as a conic gradient on top of the others. |
li::marker |
The legend swatch, colored with the slice’s --slice-color. |
Size the ring with --donut-size, the hole with --donut-hole and the space
between slices with --donut-gap. The list’s start padding makes room for the ring
and the legend markers. When the figure is narrower than 22rem, a container query
moves that room to the top, so the legend drops below the ring.
Accessibility
- The list and its
<data>values are the content, so a screen reader reads every label and share in order. The slices are pseudo elements and stay silent. - The
<figcaption>names the figure. Without it the list has no context. - Every share is written next to its label, so no one relies on telling the slice colors apart. Keep that text even when the chart looks self explanatory.
Related: Bar Chart, Gauge, Meter.
FAQ
Why does each item need both --from and --to?
CSS cannot add up the items before the current one, so each slice is told where it starts and ends, as a percentage of the whole. The --to of one item is the --from of the next, and the last one ends at 100.
How do I change the slice colors?
The first five items get a color from the --hue-* tokens and the sixth starts over. Set --hue on an item to move it to another token hue, such as --hue-red, or set --slice-color to pick any color. The legend marker follows.
Can I make it a full pie instead of a donut?
Yes. Set --donut-hole to 0% and the mask leaves the whole circle. A donut is easier to read though, since the eye compares the arc lengths instead of the angles.
Why does the legend move below the ring at 22rem?
A container query on the figure moves the legend once the chart is narrower than 22rem, which fits the default 10rem ring with room for the labels. Container queries cannot read custom properties, so the breakpoint does not follow --donut-size. When you make the ring larger, raise the 22rem in the @container rule by the same amount.
How many slices can a donut chart hold?
Keep it to about five. Past that the small slices get too thin to tell apart, so group them into an Other slice, or switch to a Bar Chart.