"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > ## Can You Create Pie Chart Segments in CSS Without JavaScript?

## Can You Create Pie Chart Segments in CSS Without JavaScript?

Published on 2024-11-06
Browse:104

## Can You Create Pie Chart Segments in CSS Without JavaScript?

Segments in a Circle Using CSS

Creating circles in CSS using border-radius is a common practice. However, can we achieve a similar effect with segments, akin to a pie chart? This article delves into ways of achieving this through HTML and CSS alone, excluding the use of JavaScript.

Generating Equal-Sized Segments

One approach for equal-sized segments involves generating a stop list for a conic-gradient() using SCSS. We can create a custom SCSS function stops($c) to generate these stops based on the provided colors ($c), ensuring equal spacing. This function takes into account the number of slices, calculates the slice angle as a percentage, and generates a list of stops with appropriate percentages.

For instance, with a palette of colors from Coolors, we can use our stops() function within a conic-gradient() declaration:

.pie {
  width: 20em; /* Desired pie diameter */
  aspect-ratio: 1; /* Square element */
  border-radius: 50%; /* Turn square into disc */
  background: conic-gradient(stops($c));
}

This approach allows for customizable slice angles by specifying a different from angle in the conic-gradient().

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3