CSS Pie Chart Code with Example

CSS Pie Chart Code with Example
Code Snippet:Pure CSS Pie Chart
Author: s
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 696
License: MIT
Edit Code online: View on CodePen
Read More

This CSS code example helps you to create a Pie Chart with colorful segments. It visually represents data percentages. The code utilizes CSS to generate pie slices for each data percentage. Each segment’s size and color correspond to its respective data value, offering a clear visual representation of proportions. This code helps display data distribution in a visually engaging way on web pages.

How to Create Pie Chart Using CSS

1. Start by setting up the HTML structure. Use a container <div> to hold the pie chart segments. Inside this container, create individual <div> elements for each segment, each with a nested <div> to display the percentage value using a <span> element. Here’s an example:

<div class="wrapper">
  <div class="d1"><div><span>60%</span></div></div>
  <div class="d2"><div><span>22%</span></div></div>
  <div class="d3"><div><span>8%</span></div></div>
  <div class="d4"><div><span>7%</span></div></div>
  <div class="d5"><div><span>3%</span></div></div>
</div>

2. Apply the necessary CSS styles to create the pie chart effect. The key is to use absolute positioning, rotation, and border properties to generate the segments. Each segment will have its own class with specific styling for size, color, and rotation. Here’s an example of the CSS:

.wrapper {
  width: 400px;
  height: 400px;
  background: #FC5B3F;
  margin: 100px auto;
  border-radius: 50%;
  overflow: hidden;
  position: relative;
  transform: rotate(108deg);
}
.wrapper .d1 {
  width: 800px;
  height: 800px;
  position: absolute;
  top: -200px;
  left: -200px;
  transform: rotate(0deg);
}
.wrapper .d1 div {
  width: 800px;
  height: 800px;
}
.wrapper .d1 div:after {
  content: '';
  width: 0;
  height: 0;
  display: block;
  border: solid transparent;
  border-width: 400px;
  border-top-color: #FC5B3F;
  position: relative;
  transform: scaleX(-3.07768);
}
.wrapper .d1 div span {
  display: block;
  width: 100%;
  position: absolute;
  left: 0;
  top: 34%;
  font-size: 12px;
  text-align: center;
  z-index: 100;
  color: #fff;
  transform: rotate(-108deg);
}
.wrapper .d2 {
  width: 800px;
  height: 800px;
  position: absolute;
  top: -200px;
  left: -200px;
  transform: rotate(147.6deg);
}
.wrapper .d2 div {
  width: 800px;
  height: 800px;
}
.wrapper .d2 div:after {
  content: '';
  width: 0;
  height: 0;
  display: block;
  border: solid transparent;
  border-width: 400px;
  border-top-color: #FCB03C;
  position: relative;
  transform: scaleX(0.82727);
}
.wrapper .d2 div span {
  display: block;
  width: 100%;
  position: absolute;
  left: 0;
  top: 34%;
  font-size: 12px;
  text-align: center;
  z-index: 100;
  color: #fff;
  transform: rotate(-255.6deg);
}
.wrapper .d3 {
  width: 800px;
  height: 800px;
  position: absolute;
  top: -200px;
  left: -200px;
  transform: rotate(201.6deg);
}
.wrapper .d3 div {
  width: 800px;
  height: 800px;
}
.wrapper .d3 div:after {
  content: '';
  width: 0;
  height: 0;
  display: block;
  border: solid transparent;
  border-width: 400px;
  border-top-color: #6FB07F;
  position: relative;
  transform: scaleX(0.25676);
}
.wrapper .d3 div span {
  display: block;
  width: 100%;
  position: absolute;
  left: 0;
  top: 34%;
  font-size: 12px;
  text-align: center;
  z-index: 100;
  color: #fff;
  transform: rotate(-309.6deg);
}
.wrapper .d4 {
  width: 800px;
  height: 800px;
  position: absolute;
  top: -200px;
  left: -200px;
  transform: rotate(228.6deg);
}
.wrapper .d4 div {
  width: 800px;
  height: 800px;
}
.wrapper .d4 div:after {
  content: '';
  width: 0;
  height: 0;
  display: block;
  border: solid transparent;
  border-width: 400px;
  border-top-color: #068587;
  position: relative;
  transform: scaleX(0.22353);
}
.wrapper .d4 div span {
  display: block;
  width: 100%;
  position: absolute;
  left: 0;
  top: 34%;
  font-size: 12px;
  text-align: center;
  z-index: 100;
  color: #fff;
  transform: rotate(-336.6deg);
}
.wrapper .d5 {
  width: 800px;
  height: 800px;
  position: absolute;
  top: -200px;
  left: -200px;
  transform: rotate(246.6deg);
}
.wrapper .d5 div {
  width: 800px;
  height: 800px;
}
.wrapper .d5 div:after {
  content: '';
  width: 0;
  height: 0;
  display: block;
  border: solid transparent;
  border-width: 400px;
  border-top-color: #1A4F63;
  position: relative;
  transform: scaleX(0.09453);
}
.wrapper .d5 div span {
  display: block;
  width: 100%;
  position: absolute;
  left: 0;
  top: 34%;
  font-size: 12px;
  text-align: center;
  z-index: 100;
  color: #fff;
  transform: rotate(-354.6deg);
}

body {
  background: #111;
}

To add more segments or customize existing ones, duplicate the segment <div> and adjust the styling in the CSS accordingly. You can change the percentages, colors, and positions to represent your data accurately.

That’s all! hopefully, you have successfully integrated this CSS pie chart code example into your project. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X