Why CSS Alone Struggles With Pie Charts

Juan Diego Rodríguez's earlier CSS-Tricks article set out three priorities for a CSS pie chart: it must be semantic, HTML-customizable, and minimal on JavaScript. The first two goals were met. The third is where this fork picks up, because the case for dropping JavaScript entirely isn't just about fun.

The division of labor matters here: JavaScript handles state and logic; CSS styles the markup. A pie chart is styling, so CSS should be able to carry it. To test that, the fork stayed as close to the original Pen as possible — preserving the semantic approach and HTML-side customizability — and rebuilt the calculation layer in pure CSS.

The Sibling Problem

A bar chart is simple in this regard: each bar's size or position is independent of every other bar. A pie chart is not. Every slice's placement depends on where the previous slice ended, which is exactly why JavaScript entered the picture. In Juan's version, a script looped over the children and wrote an --accum value — the sum of all preceding percentages — onto each slice. Without that accumulated value, neither the slice nor its label can be positioned.

CSS has no mechanism for one child to inspect another child's state, and property inheritance only flows downward. An attempt at niche techniques (animation-based tricks, for instance, along the lines of what Roman Komarov has demonstrated) was considered but not pursued. The real resolution is architectural: find a surrounding entity that can see all the slices.

Moving Values to the Parent

The markup change is modest. Instead of storing percentages on each <li>, the fork puts all of them on the parent <ul>, with each item given a dedicated name — effectively an index. This same indexing workaround was used previously to compensate for the absence of sibling-index() and sibling-count(), for example when generating random numbers across siblings. Note that both functions are approaching Baseline.

The attributes are relocated rather than added, so everything stays together in one place. Scale isn't a serious concern either, since a pie chart is rarely the right visualization for data with a huge number of entries. Optionally, data-label attributes can be added to pair labels and values visually.

Getting Each Percentage to Its Slice

The CSS layer consists of two repetitive but plain rule sets. The first pulls each data-percentage value into CSS via the upgraded attr() function, and the second reattaches it to the matching slice with nth-child().

The fork keeps this kind of incremental code on single lines without carriage returns — an acceptable exception to normal formatting rules, since it makes typos easier to spot and makes adding support for more slices easier. For repetitive blocks like this, a preprocessor such as Sass or Less could reduce the work, and a future native @function might remove the need entirely.

At the pie level, each percentage is captured through its index into an indexed variable — the fourth element gets --p-100-4, the fifth gets --p-100-5, and so on. Each slice then receives a locally scoped --p-100. At this point the values are available at two levels:

  • On the pie, via indexed variables: --p-100-1, --p-100-2, --p-100-3
  • On each slice, via the --p-100 variable

Accumulating

The second rule set builds --accum by summing percentages progressively. The first slice is the special case: there is nothing before it, so its accumulation is 0. For slice n, the accumulation equals the accumulation at n−1 plus the value at n−1. As with percentages, computation happens first at the pie level into per-slice variables (--accum-4, --accum-5, …), which are then mapped onto the local --accum inside each slice:

  • On the pie, via indexed variables: --accum-1, --accum-2, --accum-3
  • On each slice, via the --accum variable

Colors and Accessibility

The original article asked for improvements to default styling, and the fork addresses two of them:

  • data-color can be omitted, in which case colors are generated.
  • Colors can be set on the parent or on individual children — both are supported.

The generated-hue snippet relies on sibling-index() and sibling-count(), since computing hue is much cleaner with them; they were reserved for this spot because they are not quite Baseline yet. The fallback is a simple but repetitive "CSS-only polyfill."

Accessibility is handled as in the original, with one change: counter-reset/counter() supply the percentage to content in place of attr(), which should behave just as well alongside a screen reader. The label markup also switches from <strong> to <span>. A further alternative considered is using <label> directly: although <label> is usually associated with <form>, the spec permits it wherever phrasing content is expected, so nothing requires a form context.

Prior Art and Open Questions

JavaScript charting libraries are plentiful but excluded by the constraint. Among CSS-only options, Chart-CSS stands out: it advertises semantic structure, accessibility, and raw data in the markup, alongside using HTML tables — a sensible choice given that source data usually arrives as a table. It doesn't, however, compute slice start and end angles from values alone; that remains manual. Broader accessibility resources, such as Vitaly Friedman's "2022 Guide to Accessible Front-End Components" and Sarah L. Fossheim's writing on data visualization accessibility from 2020 to 2024, explain how chart accessibility should work but don't offer a simple HTML interface or a pure CSS implementation.

Some ideas were noted but not explored, to keep the original code unchanged: using <label> and <meter> for labels and values, or using a <table> for data typically extracted as [label, value] rows.

The same foundation extends to other chart types — a bar chart mode exists in the fork as proof of concept. There is also a case for packaging this as a web component. In a sense it already is one: a JavaScript-free component living in the light DOM, not fundamentally different from <div class="pie chart" attributes...> or <div pie chart attributes...>. Progressive enhancement is where that gets interesting — a chart fetching live data and refreshing itself — though that would require JavaScript, which this exercise deliberately avoids.