# Donut Chart

> A donut chart with a legend, drawn with one conic gradient and a radial mask, no SVG or JavaScript.

- Category: [CSS Gradients & Patterns](https://copy-css.com/category/gradients)
- Tags: conic-gradient, pie chart, data, legend, mask
- Live preview: https://copy-css.com/snippets/donut-chart
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```html
<figure class="donut-chart">
  <div class="donut-chart-ring" role="img" aria-label="Traffic sources: search 45%, social 30%, direct 25%"></div>
  <figcaption>
    <ul class="donut-chart-legend">
      <li>Search 45%</li>
      <li>Social 30%</li>
      <li>Direct 25%</li>
    </ul>
  </figcaption>
</figure>
```

## CSS

```css
.donut-chart {
  /* Segment sizes; the last one fills the rest */
  --a: 45%;
  --b: 30%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 20px;
  margin: 0;
  font: 500 14px/1.2 system-ui, sans-serif;
  color: #27272a;
}

.donut-chart-ring {
  width: 120px;
  aspect-ratio: 1;
  border-radius: 50%;
  background: conic-gradient(
    #6366f1 0 var(--a),
    #ec4899 0 calc(var(--a) + var(--b)),
    #f59e0b 0
  );
  -webkit-mask: radial-gradient(farthest-side, #0000 58%, #000 59%);
  mask: radial-gradient(farthest-side, #0000 58%, #000 59%);
}

.donut-chart-legend {
  display: grid;
  gap: 10px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.donut-chart-legend li {
  display: flex;
  align-items: center;
  gap: 8px;
}

.donut-chart-legend li::before {
  content: "";
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background: #6366f1;
}

.donut-chart-legend li:nth-child(2)::before {
  background: #ec4899;
}

.donut-chart-legend li:nth-child(3)::before {
  background: #f59e0b;
}
```

## Notes

- This CSS Donut Chart is 56 lines of pure CSS (1.0 KB) with no JavaScript and no dependencies. It uses radial gradients, conic gradients, masks and CSS Grid.
- Customize with: `--a`, `--b`.
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
