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.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.