目录
1. Use a CSS-only approach with pseudo-elements and border rotation
2. Create a ring-style progress bar using border and clip-path
3. Add animation and smooth transitions
4. Customize appearance
首页 web前端 css教程 如何使用CSS创建圆形进度栏

如何使用CSS创建圆形进度栏

Aug 23, 2025 am 07:38 AM

使用CSS创建环形进度条可通过conic-gradient或clip-path与旋转结合实现,前者适用于静态或JavaScript更新的进度,后者更适合平滑动画;通过CSS变量和伪元素构建圆形外观,用transform: rotate()控制进度填充,并添加transition实现动画效果,支持自定义颜色、厚度和中心标签,根据浏览器兼容性和动画需求选择合适方法。

How to create a circular progress bar with CSS

Creating a circular progress bar with CSS is a clean way to visually represent progress or completion percentages on a webpage. While pure CSS has limitations for dynamic updates, you can build a functional and animated circular progress bar using a combination of CSS and minimal HTML. Here's how.

1. Use a CSS-only approach with pseudo-elements and border rotation

A common method uses a container with a pseudo-element that rotates to simulate the progress fill. This works well for static or semi-dynamic bars (updated via JavaScript classes).

HTML structure:

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

CSS:

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

.circular-progress::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 80%;
  height: 80%;
  background: white;
  border-radius: 50%;
}

This creates a circle filled up to the percentage defined by the --progress CSS variable (e.g., 75%). The conic-gradient draws the colored arc, and the ::before pseudo-element masks the center to make it look like a ring.

Note: conic-gradient is well-supported in modern browsers but may need fallbacks for older ones.

2. Create a ring-style progress bar using border and clip-path

If you want a thin circular stroke (like a loading spinner), use a bordered element inside a clipped container.

HTML:

<div class="progress-ring">
  <div class="progress-fill"></div>
</div>

CSS:

.progress-ring {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: #e0e0e0;
  position: relative;
}

.progress-fill {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  clip: rect(0px, 100px, 100px, 50px);
  background: #4caf50;
  transform: rotate(135deg); /* Start from top */
}

.progress-fill::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  clip: rect(0px, 50px, 100px, 0px);
  background: #4caf50;
  transform: rotate(90deg); /* Adjust based on progress */
  transform-origin: 50% 50%;
}

To animate this with a specific percentage (e.g., 60%), you’d calculate the rotation angle: 360 * (progress / 100). You can update this via JavaScript by modifying the transform: rotate() value on .progress-fill::before.

3. Add animation and smooth transitions

To make the bar animate smoothly when the progress changes, use CSS transitions. However, conic-gradient and clip aren't directly animatable, so you can animate the transform: rotate() on the fill element.

Example with animation:

.progress-fill::before {
  transition: transform 0.5s ease;
}

Then update the rotation via JavaScript:

const fill = document.querySelector('.progress-fill::before');
// Not directly possible — instead, use a wrapper or class toggle

A better approach: wrap the fill in a container and rotate it:

<div class="progress-ring">
  <div class="progress-mask">
    <div class="progress-fill"></div>
  </div>
</div>
.progress-mask {
  position: absolute;
  width: 100%;
  height: 100%;
  clip: rect(0, 100px, 100px, 50px);
  transform: rotate(0deg); /* Controlled by JS */
}

.progress-fill {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: #4caf50;
}

Now use JavaScript to set rotation:

function setProgress(percent) {
  const rotateDeg = percent * 3.6; // 360 / 100
  document.querySelector('.progress-mask').style.transform = 
    `rotate(${rotateDeg}deg)`;
}

4. Customize appearance

  • Change the ring thickness by adjusting the inner cutout or using a border instead.
  • Add a label in the center using a child element.
  • Support theming with CSS variables:
    .circular-progress {
      --progress: 50%;
      --fg-color: #4caf50;
      --bg-color: #e0e0e0;
      background: conic-gradient(
        var(--fg-color) var(--progress),
        var(--bg-color) var(--progress)
      );
    }

    Basically, it's a mix of clever gradients, clipping, and rotation. The conic-gradient method is simplest for static or JS-updated values. For smoother animations, the clip-path rotate method gives more control. Pick based on your browser support and animation needs.

    以上是如何使用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