search
HomeWeb Front-endCSS TutorialIn ten minutes, you will learn how to implement a pie chart using only one div and css.

This article brings you some related questions about how to use a div with css to implement a pathological diagram. I hope it will be helpful to you.

In ten minutes, you will learn how to implement a pie chart using only one div and css.

Please scroll to the end of the article for the complete code.

We only use one div and only css to implement the pie chart.

HTMl structure

<div class="pie" style="--p:60;--b:10px;--c:purple;">60%</div>

We added several css variables:

  • --p: the percentage of the progress bar (pure number, without %), the pie chart value is consistent with the div content (with %).

  • --b: The value of the border thickness

  • --c: The main color of the border

This article uses abbreviated variables. In a production environment, in order to achieve readability, we should use --p -> --percentage, --b -> --border-thickness, --c - > --main-color to represent.

Basic settings of Pie

We set the basic style for the pie chart.

.pie {
  --w: 150px; // --w -> --width
  width: var(--w);
  aspect-ratio: 1; // 纵横比,1 说明是正方形
  display: inline-grid;
  place-content: center;
  margin: 5px;
  font-size: 25px;
  font-weight: bold;
  font-family: sans-serif;
}

Above we used aspect-ratio: 1; to ensure that the div is square. Of course, you can also use height: var(--w) to achieve the effect.

Next, we use pseudo elements to implement a simple pie chart:

.pie:before {
  content: "",
  position: absoute;
  border-radius: 50%;
  inset: 0; // 知识点 1
  background: conic-gradient(var(--c) calc(var(--p)*1%),#0000 0); // 知识点 2
}

Knowledge point 1: inset: 0; is equivalent to top: 0; right: 0; bottom: 0; top : 0;

Knowledge point 2: conic-gradient cone gradient, css method, more content, #0000 here is the hexadecimal value of transparent.

#0000 Hex Color · Red (0%) · Green (0%) · Blue (0%).

After conic-gradient application:

In ten minutes, you will learn how to implement a pie chart using only one div and css.

In order to make only the border area visible, we use the mask attribute to hide the middle circle part. We will use the radial-gradient() method:

radial-gradient(farthest-side,red calc(99% - var(--b)),blue calc(100% - var(--b)))

After applying the above code, the effect diagram can be obtained as follows:

In ten minutes, you will learn how to implement a pie chart using only one div and css.

Our goal is as follows:

In ten minutes, you will learn how to implement a pie chart using only one div and css.

We can achieve this by changing the code:

<div class="pie" style="--p:60;--b:10px;--c:purple;">60%</div>
.pie {
  --w:150px;
  
  width: var(--w);
  aspect-ratio: 1;
  position: relative;
  display: inline-grid;
  place-content: center;
  margin: 5px;
  font-size: 25px;
  font-weight: bold;
  font-family: sans-serif;
}
.pie:before {
  content: "";
  position: absolute;
  border-radius: 50%;
  inset: 0;
  background: conic-gradient(var(--c) calc(var(--p)*1%),#0000 0);
  -webkit-mask:radial-gradient(farthest-side,#0000 calc(99% - var(--b)),#000 calc(100% - var(--b)));
          mask:radial-gradient(farthest-side,#0000 calc(99% - var(--b)),#000 calc(100% - var(--b)));
}

Add a circular edge

How to add a circular edge, look at the illustration below , you will understand this little trick.

In ten minutes, you will learn how to implement a pie chart using only one div and css.

For the effect (1) in the picture, place the circle at the starting edge.

.pie:before {
  background: 
    radial-gradient(farthest-side, var(--c) 98%, #0000) top/var(--b) var(--b) no-repeat,
    conic-gradient(var(--c) calc(var(--p)*1%), #0000 0);
}

For the effect (2) in the picture, place the circle on the edge of the end.

.pipe: after {
  content: "";
  position: absolute;
  border-radius: 50%;
  inset: calc(50% - var(--b)/2); // 知识点1
  background: var(--c);
  transform: rotate(calc(var(--p)*3.6deg)) translateY(calc(50% - var(--w)/2)); // 知识点2
}

Knowledge point 1: The inset: 0; we also mentioned above - it is the abbreviation of left: 0; right: 0; bottom: 0; top: 0;.

Here we have:

left = right = 50% - b/2

Here we have moved the element to the left and right by 50% - b/2, which is equivalent to the element width being b, and centered left and right. Same thing for height.

Knowledge point 2: Calculation of rotation degrees --

angle = percentage * 360deg / 100

First rotate the element by the corresponding degree, and then move its position. This involves centering the Y axis. The text may be a little difficult to understand, so let’s understand it with the following illustrations:

In ten minutes, you will learn how to implement a pie chart using only one div and css.

Add animation

So far, what we have achieved is a static pie shape picture. We'll add animation to it next.

First register the variable:

@property --p {
  syntax: &#39;<number>&#39;;
  inherits: true;
  initial-value: 0;
}

Then, we create the key frame:

@keyframes p {
  from {
    --p: 0
  }
}

Note: Here we only need to set the --p value of from. The browser will automatically match the value in our preset to (div class="pie" style="--p:60;">60%)

Finally, we call the animation .

animation: p 1s .5s both;

Hey~ Copy the code below to experience it. Of course, we also provide pictures.

Code and renderings

<div class="pie" style="--p:20"> 20%</div>
<div class="pie" style="--p:40;--c:darkblue;--b:10px"> 40%</div>
<div class="pie no-round" style="--p:60;--c:purple;--b:15px"> 60%</div>
<div class="pie animate no-round" style="--p:80;--c:orange;"> 80%</div>
<div class="pie animate" style="--p:90;--c:lightgreen"> 90%</div>
@property --p{
  syntax: &#39;<number>&#39;;
  inherits: true;
  initial-value: 1;
}
.pie {
  --p:20;
  --b:22px;
  --c:darkred;
  --w:150px;
  width: var(--w);
  aspect-ratio: 1;
  position: relative;
  display: inline-grid;
  margin: 5px;
  place-content: center;
  font-size: 25px;
  font-weight: bold;
  font-family: sans-serif;
}
.pie:before,
.pie:after {
  content: "";
  position: absolute;
  border-radius: 50%;
}
.pie:before {
  inset: 0;
  background:
    radial-gradient(farthest-side,var(--c) 98%,#0000) top/var(--b) var(--b) no-repeat,
    conic-gradient(var(--c) calc(var(--p)*1%),#0000 0);
  -webkit-mask: radial-gradient(farthest-side,#0000 calc(99% - var(--b)),#000 calc(100% - var(--b)));
          mask: radial-gradient(farthest-side,#0000 calc(99% - var(--b)),#000 calc(100% - var(--b)));
}
.pie:after {
  inset: calc(50% - var(--b)/2);
  background: var(--c);
  transform: rotate(calc(var(--p)*3.6deg)) translateY(calc(50% - var(--w)/2));
}
.animate {
  animation: p 1s .5s both;
}
.no-round:before {
  background-size: 0 0, auto;
}
.no-round:after {
  content: none;
}
@keyframes p{
  from{--p:0}
}

Renderings:

In ten minutes, you will learn how to implement a pie chart using only one div and css.

## (Learning video sharing:

css video tutorial)

The above is the detailed content of In ten minutes, you will learn how to implement a pie chart using only one div and css.. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金. If there is any infringement, please contact admin@php.cn delete
Show Search Button when Search Field is Non-EmptyShow Search Button when Search Field is Non-EmptyApr 15, 2025 am 10:00 AM

I think the :placeholder-shown selector is tremendously cool. It allows you to select the placeholder of an input () when that placeholder is present.

The Trick to Animating the Dot on the Letter 'i'The Trick to Animating the Dot on the Letter 'i'Apr 15, 2025 am 09:55 AM

Here’s the trick: by combining the Turkish letter "ı" and the period "." we can create something that looks like the letter "i," but is made from two separate

Weekly Platform News: WebAPK Limited to Chrome, Discernible Focus Rectangles, Modal Window APIWeekly Platform News: WebAPK Limited to Chrome, Discernible Focus Rectangles, Modal Window APIApr 15, 2025 am 09:53 AM

In this week's roundup: "Add to home screen" has different meanings in Android, Chrome and Edge add some pop to focus rectangles on form inputs, and how

Making a Chart? Try Using Mobx State Tree to Power the DataMaking a Chart? Try Using Mobx State Tree to Power the DataApr 15, 2025 am 09:49 AM

Who loves charts? Everyone, right? There are lots of ways to create them, including a number of libraries. There’s D3.js, Chart.js, amCharts, Highcharts, and

Blocking Third-Party Hands from the Cookie JarBlocking Third-Party Hands from the Cookie JarApr 15, 2025 am 09:48 AM

Third-party cookies are set on your computer from domains other than the one that you're actually on right now. For example, if I log into css-tricks.com,

Ten-Ton WidgetsTen-Ton WidgetsApr 15, 2025 am 09:43 AM

At a recent conference talk (sorry, I forget which one), there was a quick example of poor web performance in the form of a third-party widget. The example

Recipes for Performance Testing Single Page Applications in WebPageTestRecipes for Performance Testing Single Page Applications in WebPageTestApr 15, 2025 am 09:42 AM

WebPageTest is an online tool and an Open Source project to help developers audit the performance of their websites. As a Web Performance Evangelist at

Stop Animations During Window ResizingStop Animations During Window ResizingApr 15, 2025 am 09:40 AM

Say you have page that has a bunch of transitions and animations on all sorts of elements. Some of them get triggered when the window is resized because they

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools