Very Simple Pie Chart With Pure HTML 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 CSS Pie Chart Useful Bits & Links
The End

 

DOWNLOAD & DEMO

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

First
Second
Third

 

 

PURE CSS PIE CHART

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
<!-- (A) PIE CHART -->
<div class="pie"></div>

Yes, a <div class="pie"> 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
<!-- (B) LEGEND -->
<div class="legend">
  <div class="segment1"></div> <div>First</div>
  <div class="segment2"></div> <div>Second</div>
  <div class="segment3"></div> <div>Third</div>
</div>

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.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

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 and conic gradient are well-supported on all modern “Grade A” browsers.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

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!

Leave a Comment

Your email address will not be published.