DataPremium

Donut Chart CSS

A donut chart with no SVG: one conic gradient computed from the legend, pierced by a mask, that enters with a quarter turn while fading in.

JSCSSGradient

Updated

This effect is part of Effect.Labs — 846 vanilla effects, some free, some premium. Data has 28 effects, including 1 free. Explore the category →

3 usage examples

Repairs done, session by session — a repair café's page — Donut Chart CSS example 1

① Repairs done, session by session — a repair café's page

WhenThree tabs: March, April, May. Each panel holds its own ring (appliances, bikes, clothes, computers) and the session's results. On every tab, the ring of the displayed panel arrives turning.
WhyThe chart does not update, so one is prepared per session. The quarter turn on each tab change signals that the figures are no longer the same.
SettingsThree roots, one per panel; the panels not chosen carry hidden. Values in number of items (19, 12, 9, 7), --dnx-size:126px, --dnx-thickness:25px, four colours from --dnx-c1 to --dnx-c4, transparent background, flex-wrap:nowrap on the root. In the scene, a click outside the tabs moves to the next session.
Tree species of a village forest — header of the village's page — Donut Chart CSS example 2

② Tree species of a village forest — header of the village's page

WhenAt the top of the "forest" page of a town hall website, a large green ring shows the share of oak, beech, hornbeam and pine. In the hole, the area: 412 hectares. The legend with figures sits below.
WhyThe transparent hole earns its place: it holds what every share chart lacks, the size of the whole. A one-block entrance suits a header image, seen once.
Settings--dnx-size:178px, --dnx-thickness:30px, greens and ochre (#d8ecae, #7fbf6a, #e0a646, #4f8f8b), root set to a column. The ring is wrapped in a position:relative block that carries "412 hectares" centred. For the capture, the block is shown again once when the pointer first moves over it.
Fibre blend of each yarn — catalogue of an online haberdashery — Donut Chart CSS example 3

③ Fibre blend of each yarn — catalogue of an online haberdashery

WhenIn a list of yarns, each row starts with a 46 px ring: merino, alpaca, silk, linen, cotton, each always in the same colour. The composition is written next to it.
WhyOne look down the column of rings is enough to spot the all-wool yarns or the summer blends. A fibre keeps its colour from row to row because the five slices are always listed in the same order, at 0 when the fibre is absent.
Settingsdata-legend="hidden", --dnx-size:46px, --dnx-thickness:12px, five <li> per ring, some with data-value="0", root padding set to 0. For the capture, the list is shown again once when the pointer first moves over it; on a page, each ring enters when it scrolls into view.

How it works

The ring is an empty <div class="dnx-donut">. Its background is a conic-gradient: a series of coloured sectors whose boundaries the script writes into the --dnx-stops variable. A radial mask makes the centre transparent, except for a band --dnx-thickness wide: through the hole, you see whatever is behind.

The data is the legend: each <li data-value="…"> is one slice. The script adds the values up and computes each share (items, hectares, minutes: any unit works), then adds a colour dot and the percentage to every row, written in the page language with one decimal at most.

Until it is 35% visible, the ring is transparent. The root then receives the dnx-on class: the ring makes a clockwise quarter turn while appearing, in 1 s, once only. Slices are not drawn one by one; the whole block arrives.

After that the chart is frozen. Changing a data-value once the ring is displayed redraws nothing: for other data, plan another block (one per tab, for example).

Accessibility

  • The legend is the content: a real <ul> named by aria-label, each row reading "Writing 40%". The ring gets aria-hidden: it says nothing more.
  • Hidden legend: with data-legend="hidden" on the root, the list disappears from view but is still read by screen readers. Handy for a mini ring; give the figures as text next to it for everyone else.
  • Reduced motion: no rotation, no fade, the ring is there from the start.
  • Colours: six original hues, designed for a dark background; the written percentage saves telling two neighbours apart. From the 7th slice on, colours start over.
  • No interaction: nothing to hover or click, so nothing to reach with the keyboard.

Browser compatibility

conic-gradient, mask and its -webkit-mask form, aspect-ratio (it gives the ring its height), IntersectionObserver. In Chromium 149, Firefox 151 and WebKit 26.5: ring transparent off screen, mid-rotation 0.4 s after it arrives (opacity between 0.65 and 0.69), legend "40%, 25%, 20%, 15%" and the same cut-out hole in all three.

Chrome 88+✓ Full (measured)
Firefox 89+✓ Full (measured)
Safari 15+✓ WebKit engine measured, not checked in real Safari
Edge 88+✓ Full (Chromium engine)
Safari iOS 15+✓ Not checked on a real device
Chrome Android 88+✓ Full (Chromium engine)

Without JavaScript the ring stays invisible (its starting opacity is 0) and the legend shows its labels without percentages: write the figures in the text if the page must read without scripts. Without aspect-ratio, the ring has no height and cannot be seen.

The code

HTML structure to paste into your page (the CSS + JS come with your purchase):

index.html — structure
<div class="fx-0236-root">
  <div class="dnx-donut"></div>
  <ul class="dnx-legend" aria-label="Time spent per task">
    <li data-value="40">Writing</li>
    <li data-value="25">Proofreading</li>
    <li data-value="20">Layout</li>
    <li data-value="15">Sending</li>
  </ul>
</div>

One-time payment, no subscription — the code is yours forever

Customize

Options passed to the API or data-* attributes:

Option / propertyDefaultEffect
data-value on each <li> (HTML) 40, 25, 20, 15 Slice value, in whatever unit you like. Read on display, not afterwards.
Number of <li> (HTML) 4 One slice per row, no limit; six colours, which repeat from the 7th.
data-legend="hidden" on the root (HTML) absent Takes the legend off the screen while leaving it to screen readers.
--dnx-c1 to --dnx-c6 (CSS) #6366f1, #d946ef, #06b6d4, #10b981, #f59e0b, #ec4899 Slice and dot colours, in list order.
--dnx-size (CSS) 150px Diameter; the ring never exceeds the width of its container.
--dnx-thickness (CSS) 30px Width of the band; the rest of the disc is the hole.
--dnx-text, --dnx-muted (CSS) white at 80% and at 50% Colour of the legend labels and percentages.
--dnx-bg, --dnx-min-height (CSS) #0a0a0f, 300px Block background and minimum height; transparent and 0 to drop it into a card.
lang on the page or a parent (HTML) browser language How percentages are written: "40.4%" or "40,4 %".

FAQ

Donut Chart is drawn in SVG: its slices lengthen one after another, separated by a small gap, with the total written in the centre and values in euros or your own unit. Donut Chart CSS is a conic gradient: no SVG, touching slices, a legend in percentages, an empty transparent hole, a one-block entrance. Less code to read, less information on screen.
The hole is truly transparent. Wrap .dnx-donut in a position: relative block and add your content there with position: absolute; inset: 0, centred. Do not put it inside .dnx-donut: the mask would cut it out and it would turn with the ring.
It does not redraw: the script reads the data-value attributes on load, then one last time when the ring becomes visible. For states known in advance, place one block per state and show only one. For slices that glide from one value to another, see Animated Donut Chart.