CSS Donut Chart

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

  • conic-gradient
  • pie chart
  • data
  • legend
  • mask
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
.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;
}

About this CSS Donut Chart

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. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.

CSS features used

Customize it

Change these custom properties to restyle the donut chart without touching the rest of the CSS:

PropertyDefault
--a45%
--b30%

How to use this donut chart

  1. Copy the HTML and paste it where you want the element to appear.
  2. Copy the CSS into your stylesheet, or inside a <style> tag.
  3. Keep the .donut-chart class name, or rename it in both places. Then tweak colors, sizes and timing to match your design.
  4. Using React or Next.js? Change class to className in the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).

Building with an AI tool?

“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.

Feedback