Categories: softare development

Create Pie Charts in CSS

Pie charts are circular charts divided into slices to illustrate numerical proportions. Each slice represents a part of a whole — commonly used to visualize data distribution (e.g., market share, budget percentage, survey results).

Start learning CSS

Even though CSS is not designed primarily for data visualization, you can create pie-chart-like shapes using just CSS — mainly by leveraging bordersclip-path, and conic-gradient.

What a Pie Chart Is

A pie chart:

  • Represents 100% total divided into parts
  • Each slice = percentage
  • Circular shape split into visual sections
  • Often labeled or colored for clarity

Example:

CategoryPercentage
A40%
B30%
C20%
D10%

Creating Pie Charts in CSS

Method 1: Using conic-gradient() (Modern & Simple)

This is the easiest and most modern way to create a pie chart.

<div class="pie"></div>

<style>
.pie {
  width: 200px;
  height: 200px;
  border-radius: 50%;
  background: conic-gradient(
    #4caf50 0 40%,   /* 40% */    #2196f3 40% 70%,/* next 30% */    #ff9800 70% 90%,/* next 20% */    #f44336 90% 100% /* last 10% */  );
}
</style>

✔️ Best method
✔️ Pure CSS
✔️ Easy to adjust slices
❌ Not supported in very old browsers

Result:

Method 2: CSS clip-path

You can manually build slices and rotate them.

<div class="chart">
  <div class="slice slice1"></div>
  <div class="slice slice2"></div>
</div>

<style>
.chart {
  width: 200px;
  height: 200px;
  border-radius: 50%;
  position: relative;
  overflow: hidden;
}
.slice {
  position: absolute;
  width: 100%;
  height: 100%;
  clip-path: polygon(50% 50%, 100% 0, 100% 100%);
}
.slice1 {
  background: orange;
  transform: rotate(0deg);
}
.slice2 {
  background: blue;
  transform: rotate(120deg);
}
</style>

✔️ Works without gradients
❌ Hard to calculate angles manually

Result:

Method 3: Border Trick (Old School)

Uses thick borders to simulate pie slices. Rarely used now but good to know:

<div class="pie"></div>

<style>
.pie {
  width: 0;
  height: 0;
  border: 100px solid;
  border-color: #4caf50 #2196f3 transparent transparent;
  border-radius: 50%;
}
</style>

✔️ Legacy technique
❌ Limited slices
❌ Hard to customize

Result:

When to Use Which Method

MethodBest Use Case
conic-gradient()Real pie charts, dashboards, modern apps
clip-pathControlled custom slices, animations
Border trickLearning / old browsers / simple shapes

Summary

ApproachEaseNotes
conic-gradient⭐⭐⭐⭐⭐Best & easiest
clip-path⭐⭐⭐⭐More control
Border hack⭐⭐⭐Old method
Share
Published by
codeflare

Recent Posts

The Golden Ratio (φ)

1. What Is the Golden Ratio? The Golden Ratio, represented by the Greek letter φ (phi), is…

1 week ago

CSS Combinators

In CSS, combinators define relationships between selectors. Instead of selecting elements individually, combinators allow you to target elements based…

1 week ago

Boolean Algebra

Below is a comprehensive, beginner-friendly, yet deeply detailed guide to Boolean Algebra, complete with definitions, laws,…

2 weeks ago

Why It’s Difficult to Debug Other People’s Code (And what Can be Done About it)

Debugging your own code is hard enough — debugging someone else’s code is a whole…

2 weeks ago

Complete Git Commands

Git is a free, open-source distributed version control system created by Linus Torvalds.It helps developers: Learn how to…

2 weeks ago

Bubble Sort Algorithm

Bubble Sort is one of the simplest sorting algorithms in computer science. Although it’s not…

2 weeks ago