The basic HTML structure for a pie chart consists of a container element (usually a <div>
) that will hold the individual slices of the pie. Each slice can be represented by a <div>
as well. The container gives the overall shape and size of the pie chart, and the slices are positioned and sized to show the correct proportions.
border - radius
property. Setting border - radius: 50%
on an element makes it a circle if its width and height are equal.transform
property is used to rotate the slices to their correct positions. We can use transform: rotate(angle)
where angle
is specified in degrees.background - color
to distinguish them from one another.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale = 1.0">
<link rel="stylesheet" href="styles.css">
<title>Pie Chart</title>
</head>
<body>
<div class="pie - chart">
<div class="slice slice1"></div>
<div class="slice slice2"></div>
<div class="slice slice3"></div>
</div>
</body>
</html>
/* styles.css */
.pie - chart {
width: 200px;
height: 200px;
border - radius: 50%;
background: conic - gradient(
#ff5733 0deg 120deg,
#33ff57 120deg 240deg,
#5733ff 240deg 360deg
);
}
In the above CSS code, we first define the size and circular shape of the pie chart using width
, height
, and border - radius
. Then, we use the conic - gradient
function to create the different slices of the pie. Each color is specified along with the start and end angles in degrees.
When creating multiple slices, it’s a good practice to use classes for each slice. This makes the code more modular and easier to manage. For example:
.slice {
position: absolute;
width: 100%;
height: 100%;
border - radius: 50%;
}
.slice1 {
background - color: #ff5733;
clip - path: polygon(50% 50%, 100% 0, 100% 100%);
}
.slice2 {
background - color: #33ff57;
transform: rotate(120deg);
clip - path: polygon(50% 50%, 100% 0, 100% 100%);
}
.slice3 {
background - color: #5733ff;
transform: rotate(240deg);
clip - path: polygon(50% 50%, 100% 0, 100% 100%);
}
Here, we first define the common properties for all slices in the .slice
class. Then, we define the specific properties for each slice, such as background color and rotation.
Legends are used to explain what each slice of the pie chart represents. We can add a simple HTML list for the legend:
<ul class="legend">
<li><span class="legend - color slice1 - color"></span> Category 1</li>
<li><span class="legend - color slice2 - color"></span> Category 2</li>
<li><span class="legend - color slice3 - color"></span> Category 3</li>
</ul>
.legend - color {
display: inline - block;
width: 15px;
height: 15px;
margin - right: 5px;
}
.slice1 - color {
background - color: #ff5733;
}
.slice2 - color {
background - color: #33ff57;
}
.slice3 - color {
background - color: #5733ff;
}
To make the pie chart responsive, we can use relative units like percentages for width and height. For example:
.pie - chart {
width: 50%;
height: auto;
border - radius: 50%;
background: conic - gradient(
#ff5733 0deg 120deg,
#33ff57 120deg 240deg,
#5733ff 240deg 360deg
);
}
This way, the pie chart will resize according to the size of its parent container.
To make the pie chart accessible, we can add ARIA (Accessible Rich Internet Applications) attributes. For example, we can add role="img"
to the pie chart container and provide a aria - label
to describe what the pie chart represents:
<div class="pie - chart" role="img" aria - label="Pie chart showing the distribution of three categories">
<div class="slice slice1"></div>
<div class="slice slice2"></div>
<div class="slice slice3"></div>
</div>
Creating pie charts with HTML and CSS is a great way to add simple data visualizations to your web pages without relying on JavaScript libraries. By understanding the fundamental concepts of HTML structure and CSS styling, you can create visually appealing and accessible pie charts. Remember to follow common and best practices such as using classes, adding legends, making the chart responsive, and ensuring accessibility.