目錄
2. Styling the Circle with CSS
3. Creating the Hollow Center (Ring Effect)
Option A: Using clip-path (Simpler)
Option B: Using a Pseudo-element (Recommended)
4. Adding a Label (Optional)
5. Full Working Example
Important Notes
首頁 web前端 css教學 如何使用CSS創建圓形進度欄?

如何使用CSS創建圓形進度欄?

Aug 05, 2025 pm 03:16 PM

使用HTML創建基礎結構,通過自定義屬性--progress設置進度值;2. 利用CSS的conic-gradient根據--progress生成彩色扇形背景;3. 通過偽元素::before配合inset和border-radius裁剪出中心區域,形成環形;4. 可選添加帶樣式的span標籤作為中心文字標籤;5. 整體通過flex佈局居中內容,實現一個純CSS的圓形進度條,最終效果為一個75%綠色填充、25%灰色剩餘的環形進度條,且可通過JavaScript動態更新進度值並實現動畫效果。

How to create a circular progress bar with CSS?

Creating a circular progress bar with CSS involves combining HTML structure and CSS styling, often using CSS conic-gradient , clip-path , or pseudo-elements with border-radius tricks. One of the most modern and flexible approaches uses conic-gradient and background properties. Here's how to do it step by step.

How to create a circular progress bar with CSS?

1. Basic Structure with HTML

Start with a simple container element that will represent the circle:

 <div class="circular-progress" style="--progress: 75;"></div>

Here, we're using a custom CSS property --progress to control the fill level (75% in this case).

How to create a circular progress bar with CSS?

2. Styling the Circle with CSS

Use CSS to create the circular shape and apply the gradient based on the progress value.

 .circular-progress {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background: 
    conic-gradient(
      #4caf50 var(--progress),
      #e0e0e0 var(--progress)
    );
  position: relative;
}

This creates a pie-chart-like effect where the green color ( #4caf50 ) fills up to the percentage defined by --progress , and the rest is gray ( #e0e0e0 ).

How to create a circular progress bar with CSS?

But this fills the entire circle. To make it look like a ring , we need to mask the center.


3. Creating the Hollow Center (Ring Effect)

Use a pseudo-element or a clip-path to cut out the center and form a ring.

Option A: Using clip-path (Simpler)

 .circular-progress {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background: 
    conic-gradient(
      #4caf50 calc(var(--progress) * 1%), 
      #e0e0e0 calc(var(--progress) * 1%)
    );
  clip-path: inset(15px);
}

clip-path: inset(15px) removes a margin from all sides, creating a ring. But this isn't perfectly centered or scalable.

 .circular-progress {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background: 
    conic-gradient(
      #4caf50 calc(var(--progress) * 1%), 
      #e0e0e0 calc(var(--progress) * 1%)
    );
  position: relative;
}

.circular-progress::before {
  content: &#39;&#39;;
  position: absolute;
  inset: 15px; /* Controls thickness of the ring */
  background: #fff; /* This masks the center */
  border-radius: 50%;
}

This method gives a clean ring. The inset: 15px creates a smaller circle inside, revealing only the outer ring.


4. Adding a Label (Optional)

You can add a text label in the center using another pseudo-element or a child element.

 <div class="circular-progress" style="--progress: 60">
  <span class="label">60%</span>
</div>
 .label {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-family: Arial, sans-serif;
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

Make sure the parent has position: relative .


5. Full Working Example

 <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Circular Progress Bar</title>
  <style>
    .circular-progress {
      width: 120px;
      height: 120px;
      border-radius: 50%;
      background: conic-gradient(
        #4caf50 calc(var(--progress) * 1%),
        #e0e0e0 calc(var(--progress) * 1%)
      );
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .circular-progress::before {
      content: &#39;&#39;;
      position: absolute;
      inset: 15px;
      background: #fff;
      border-radius: 50%;
    }

    .label {
      font-family: Arial, sans-serif;
      font-size: 18px;
      font-weight: bold;
      color: #333;
      z-index: 1;
    }
  </style>
</head>
<body>
  <div class="circular-progress" style="--progress: 75">
    <span class="label">75%</span>
  </div>
</body>
</html>

Important Notes

  • The conic-gradient method is not supported in older browsers like Internet Explorer.
  • For dynamic updates, you can change --progress via JavaScript:
     document.querySelector(&#39;.circular-progress&#39;).style.setProperty(&#39;--progress&#39;, 90);
  • To animate the progress, use CSS transitions on --progress — but note: CSS doesn't animate custom properties directly. You'll need a workaround using @property (experimental) or JavaScript animation.

  • Alternative: Stroke-based (SVG) Method

    While this question was about pure CSS, many developers use SVG for more control and animation support. But for a pure CSS solution , the conic-gradient pseudo-element method is clean and effective.


    That's it. You now have a responsive, customizable circular progress bar using only HTML and CSS. Basically just a clever use of gradients and masking.

    以上是如何使用CSS創建圓形進度欄?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Stock Market GPT

Stock Market GPT

人工智慧支援投資研究,做出更明智的決策

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

如何使用純CSS創建下拉菜單 如何使用純CSS創建下拉菜單 Sep 20, 2025 am 02:19 AM

使用HTML和CSS可創建無需JavaScript的下拉菜單。 2.通過:hover偽類觸發子菜單顯示。 3.利用嵌套列表構建結構,CSS設置隱藏與懸浮顯示效果。 4.可添加過渡動畫提升視覺體驗。

如何防止圖像拉伸或縮小CSS 如何防止圖像拉伸或縮小CSS Sep 21, 2025 am 12:04 AM

useobject-fitormax-widthwithheight:自動置換式; object-fitControlshowimagesfillcontainersfillcontainerswhilepreservingaspectratios,andmax-width:100%;高度;高度:autoEsoensuresResresresResresRessersRessiveScalingScalingWithOutStertracterging。

如何使用CSS中的指針事件屬性 如何使用CSS中的指針事件屬性 Sep 17, 2025 am 07:30 AM

Thepointer-eventspropertyinCSScontrolswhetheranelementcanbethetargetofpointerevents.1.Usepointer-events:nonetodisableinteractionslikeclicksorhoverswhilekeepingtheelementvisuallyvisible.2.Applyittooverlaystoallowclick-throughbehaviortounderlyingelemen

如何使用CSS添加盒子陰影效果 如何使用CSS添加盒子陰影效果 Sep 20, 2025 am 12:23 AM

USETHEBOX-SHADOWPROPERTYTOADDDROPSHADOWS.DEFINEHORIZONTALANDVERTICALESTESETSETSETSETSETSETSETSETSETSETSETSETSETSETSETSETSETSETESTESTESTESTESTESTEMENG:MMULTIPLESHADOWSARECOMMA-SEPARAWS.MEULTIPLESHADOWSARECOMMA-SEPARATED.EXAMPL

如何將過濾器應用於CSS的圖像 如何將過濾器應用於CSS的圖像 Sep 21, 2025 am 02:27 AM

thecssfilterpropertyallowseasyagestylinglingwisslikeblur,亮度和格雷斯卡爾(Grayscale.UseFilter):濾波器函數(值)onimagesorbackgroundImages.commonfunctionsIncludeBlurblur(px),亮度(brightness),亮度(%),偏見(%),損壞(%),sancale(%),飽和度(%)

如何在CSS中添加梯度背景 如何在CSS中添加梯度背景 Sep 16, 2025 am 05:30 AM

要添加CSS漸變背景,使用background或background-image屬性配合linear-gradient()、radial-gradient()等函數即可;首先選擇漸變類型,設置方向與顏色,並可通過顏色停靠點、形狀、大小等參數精細控制,例如linear-gradient(toright,#ff7e5f,#feb47b)創建從左到右的線性漸變,radial-gradient(circle,#ff9a9e,#fecfef)創建圓形徑向漸變,還可通過repeating-linear-gr

如何在CSS列表上創建交錯的動畫效果 如何在CSS列表上創建交錯的動畫效果 Sep 18, 2025 am 12:15 AM

使用CSS創建交錯動畫效果需為列表項設置相同動畫但錯開開始時間。首先構建無序列表HTML結構,接著定義如淡入上滑的@keyframes動畫,然後通過:nth-child選擇器或CSS自定義屬性為每個列表項設置遞增的animation-delay實現stagger效果,最後可選JavaScript控制進入視口時觸發。該方法通過協調元素時序實現自然流暢的級聯動畫。

如何在CSS網格佈局中創建空白? 如何在CSS網格佈局中創建空白? Sep 22, 2025 am 05:15 AM

使用gap、row-gap或column-gap屬性可在CSSGrid佈局中創建網格項之間的間距,gap是設置行列間距的簡寫屬性,可接受一個或兩個長度值,row-gap和column-gap則分別單獨控制行與列的間距,支持px、rem、%等單位。

See all articles