How do you make a pie chart using html and css?

Welcome to a tutorial on how to create a simple pie chart using pure HTML and CSS. Need to display a pie chart in your project? But don’t want to introduce loading bloat with third-party libraries? Here’s a really simple version – Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

TABLE OF CONTENTS

DOWNLOAD & DEMO

How do you make a pie chart using html and css?

Firstly, here is the download link to the example code as promised.

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

PIE CHART DEMO

All right, let us now get into the steps of building a simple pie chart using HTML and CSS only.

PART 1) PIE CHART

1A) THE HTML

pie-chart.html


Yes, a 

is all the HTML we need to generate the pie.

1B) THE CSS

pie-chart.css

/* (A) PIE CHART */
.pie {
  /* (A1) CIRCLE */
  width: 300px; height: 300px;
  border-radius: 50%;
 
  /* (A2) SEGMENTS */
  background: conic-gradient(
    red 0deg 45deg,
    green 45deg 190deg,
    blue 190deg 360deg
  );
}
  • (A1) width: 300px height: 300px will create a square, and adding border-radius: 50% turns it into a circle.
  • (A2) Build the segments using conic-gradient, how it works is dreadfully simple. As you already know, a full circle is 360 degrees. So we just specify the segments using COLOR START-DEGREE END-DEGREE.

PART B) THE LEGEND

2A) THE HTML

pie-chart.html


First
Second
Third

Now, a few tutorials on the Internet do “brute force calculations” to put the labels/legend into the pie chart itself. I figured that is “not simple”, and the easiest way is to just create a separate legend.

2B) THE CSS

pie-chart.css

/* (B) LEGEND */
/* (B1) LEGEND CONTAINER */
.legend {
  display: grid;
  grid-template-columns: 50px auto;
  margin-top: 30px;
}
 
/* (B2) SEGMENTS */
.legend div { padding: 10px; }
.segment1 { background: red; }
.segment2 { background: green; }
.segment3 { background: blue; }

Not much of a mystery here either – The legend is just a “grid table”. The first cell is the “color block”, followed by the name of the segment itself.

IT WORKS, BUT NOT FOR COMPLEX PIES.

This simple pie chart works great if you only have a few segments, but it quickly grows painful when there are a lot of segments to deal with. Manually calculating the segments, assigning the colors, updating the legend is not fun to deal with… So a bit of Javascript to “automate” the calculation will be great – I shall leave that as a possible future update.

COMPATIBILITY CHECKS

  • CSS Grid – CanIUse
  • CSS Conic Gradient – CanIUse

CSS grid and conic gradient are well-supported on all modern “Grade A” browsers.

  • Gantt Chart – Code Boxx
  • Bar Chart – Code Boxx
  • Donut Chart – Code Boxx
  • Example on CodePen – Pure CSS Pie Chart

INFOGRAPHIC CHEAT SHEET

How do you make a pie chart using html and css?
Pure CSS Pie Chart (Click To Enlarge)

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

How do I create a pie chart in HTML and CSS?

You can create a Pie Chart in HTML using a simple CSS function called conic-gradient . First, we add a
element to our HTML page, which acts as a placeholder for our pie chart. And finally we are ready to populate the pie chart with our data.

How do I make a pie chart with just CSS?

The HTML Structure for the Pie Chart.
--p : This variable should contain the percentage value as a number (without the % sign). It be should the same as the content..
--b : This variable will define the thickness of the border..
--c : This variable will define the main color..

How do I make a simple pie chart in HTML?

Creating JavaScript Pie Chart.
Create an HTML page. The very first thing you need to do is to create a file in which you will put your chart later. ... .
Reference all necessary files. The second step is about adding links into the section. ... .
Put the data together. ... .
Write the chart code..

How do you create a diagram in HTML and CSS?

How to create the diagram.
Step 1: Sketch out the diagram. To create the diagram, start by sketching it out on a piece of paper or on a spreadsheet. ... .
Step 2: Create the HTML. ... .
Step 3: Create the CSS for the grid. ... .
Step 4: Style the blocks. ... .
Step 4: Position the blocks within the grid. ... .
Step 5: Position and Style the SVG connectors..