HTML CSS 进度条

1 原生HTML标签

  • <meter>:显示已知范围的标量值或者分数值
  • <progress>:显示一项任务的完成进度,通常情况下,该元素都显示为一个进度条

1.1 <meter>

html 复制代码
<html>
    <head>
        <style>
            meter{
                width:200px;
            }
        </style>
    </head>
    <body>
        <div> 
            <span>进度:</span>
            <meter min="0" max="200" value="150"></meter>
        </div>
    </body>
</html>

minmaxvalue 分别表示最大值,最小值与当前值。

1.2 <progress>

html 复制代码
<html>
    <head>
        <style>
            progress{
                width:200px;
            }
        </style>
    </head>
    <body>
        <div> 
            <span>进度:</span>
            <progress max="200" value="150"></progress>
        </div>
    </body>
</html>

max 描述 progress 元素所表示的任务一共需要完成多少工作量,value 属性用来指定该进度条已完成的工作量。

1.3 区别

主要是语义上的差别。

  • <meter>:表示已知范围内的标量测量值或分数值
  • <progress>:表示任务的完成进度

比如,一个需求当前的完成度,应该使用 <progress>,而如果要展示汽车仪表盘当前的速度值,应该使用 meter

1.4 存在问题

  • 无法有效的修改 <meter><progress> 标签的样式,比如背景色,进度前景色等。并且,默认样式在不同浏览器之间不一致。
  • 无法添加动画效果、交互效果
html 复制代码
<html>
    <head>
        <style>
            meter{
                width:200px;
            }
        </style>
    </head>
    <body>
        <div> 
            <span>进度:</span>
            <meter min="0" max="200" value="150"></meter>
        </div>
    </body>
</html>

chrome:

html 复制代码
<html>
    <head>
        <style>
            progress{
                width:200px;
                color:red;
                background-color: blue;
            }
        </style>
    </head>
    <body>
        <div> 
            <span>进度:</span>
            <progress max="200" value="150"></progress>
        </div>
    </body>
</html>

chrome:

2 HTML标签+CSS

思路:使用背景色配合百分比

2.1 双标签

html 复制代码
<html>
<head>
    <style>
        .wrapper {
            width: 220px;
            height: 30px;
            border-radius: 30px;
            background: #8d8d8d;
        }

        .progress {
            width: 70%;
            height: inherit;
            border-radius: 30px 0 0 30px;
            background: #e1e43a;
            text-align: center;
            line-height: 30px;
        }
    </style>
</head>
<body>
    <div>
        <span>进度:</span>
        <div class="wrapper">
            <div class="progress">70%</div>
        </div>
    </div>
</body>
</html>

优点:

  • 进度具体数值可以直接传参给width属性
  • 可以自定义样式,比如前景色,背景色,渐变
  • 可以自定义动画和交互效果

2.2 单标签

使用渐变属性

html 复制代码
<html>
<head>
    <style>
        .progress {
            width: 200px;
            height: 30px;
            border-radius: 30px;
            background: linear-gradient(90deg, #e1e43a, #00ff00 70%, transparent 0);
            border: 1px solid #eee;
            text-align: center;
            line-height: 30px;
        }
    </style>
</head>

<body>
    <div>
        <span>进度:</span>
        <div class="progress">70%</div>
    </div>
</body>
</html>

如果不需要进度条渐变,只需要指定渐变颜色为同一颜色即可

html 复制代码
<html>
<head>
    <style>
        .progress {
            width: 200px;
            height: 30px;
            border-radius: 30px;
            background: linear-gradient(90deg, #e1e43a, #e1e43a 70%, transparent 0);
            border: 1px solid #eee;
            text-align: center;
            line-height: 30px;
        }
    </style>
</head>

<body>
    <div>
        <span>进度:</span>
        <div class="progress">70%</div>
    </div>
</body>
</html>

使用变量:

html 复制代码
<html>
<head>
    <style>
        .progress {
            width: 200px;
            height: 30px;
            border-radius: 30px;
            background: linear-gradient(90deg, #e1e43a, #e1e43a var(--progress), transparent 0);
            border: 1px solid #eee;
            text-align: center;
            line-height: 30px;
        }
    </style>
</head>

<body>
    <div>
        <span>进度:</span>
        <div class="progress" `style=``"--progress: 70%"`>70%</div>
    </div>
</body>
</html>

存在问题:CSS中,渐变(比如 linear-gradinetradial-gradientconic-gradient)不支持过渡变换的,因此.progress增加transition,不会有过渡动画效果。

解决方案:使用CSS@property

html 复制代码
<html>
<head>
    <style>
        @property --progress {
            syntax: '<percentage>';
            inherits: false;
            initial-value: 0%;
          }
        .progress {
            width: 200px;
            height: 30px;
            border-radius: 30px;
            background: linear-gradient(90deg, #e1e43a, #e1e43a var(--progress), transparent 0);
            border: 1px solid #eee;
            text-align: center;
            line-height: 30px;
            transition: 0.5s --progress;
        }
    </style>
</head>

<body>
    <div>
        <span>进度:</span>
        <div class="progress" `style=``"--progress: 70%"`>70%</div>
    </div>
</body>
</html>

优点:

  • 进度具体数值可以直接传参给--progress变量
  • 可以自定义样式,比如前景色,背景色,渐变
  • 可以自定义动画和交互效果

2.3 圆弧进度条

思路:圆锥渐变 conic-gradient()

html 复制代码
<html>
<head>
    <style>
        .progress {
            width: 200px;
            height: 200px;
            border-radius: 50%;
            background: conic-gradient(#e1e43a 0, #e1e43a 25%, #83b596 25%, #83b596);
            text-align: center;
            line-height: 200px;
        }
    </style>
</head>
<body>
    <div>
        <span>进度:</span>
        <div class="progress">70%</div>
    </div>
</body>
</html>

中间增加mask: 方法一(仅适用纯色背景):

html 复制代码
<html>
<head>
    <style>
        .progress {
            width: 200px;
            height: 200px;
            border-radius: 50%;
            background: conic-gradient(#e1e43a 0, #e1e43a 25%, #83b596 25%, #83b596);
            display:flex;
            justify-content: center;
            align-items: center;
        }
        .mask{
            width:170px;
            height: 170px;
            border-radius: 50%;
            background-color: #fff;
            display:flex;
            justify-content: center;
            align-items: center;
        }
    </style>
</head>
<body>
    <div>
        <span>进度:</span>
        <div class="progress"><div class="mask">70%</div></div>
    </div>
</body>
</html>
html 复制代码
<html>
<head>
    <style>
        .progress {
            width: 200px;
            height: 200px;
            border-radius: 50%;
            background: conic-gradient(#e1e43a 0, #e1e43a 25%, #83b596 25%, #83b596);
            position:relative;
        }
        .progress::after{
            content:'70%';
            width: 170px;
            height: 170px;
            border-radius: 50%;
            position:absolute;
            bottom:15px;
            left:15px;
            background-color: #fff;
            display:flex;
            justify-content: center;
            align-items: center;
        }
    </style>
</head>
<body>
    <div>
        <span>进度:</span>
        <div class="progress"></div>
    </div>
</body>
</html>

方法二(可适用渐变背景):

html 复制代码
<html>
<head>
    <style>
        .box{
            width:300px;
            height: 300px;
            display:flex;
            justify-content: center;
            align-items: center;
            background: linear-gradient(90deg, #fff, #0400ff 100%);
        }
        .progress {
            width: 200px;
            height: 200px;
            border-radius: 50%;
            background: conic-gradient(#e1e43a 0, #e1e43a 25%, #83b596 25%, #83b596);
            mask: radial-gradient(transparent, transparent 50%, #000 50%, #000 0);
            -webkit-mask: radial-gradient(transparent, transparent 50%, #000 50%, #000 0);
        }
       
    </style>
</head>
<body>
    <div class="box">
        <span>进度:</span>
        <div class="progress"></div>
    </div>
</body>
</html>

存在问题: 进度百分比不是类似于 0°、90°、180°、270°、360° 这类数字时,使用圆锥渐变时,不同颜色间的衔接处会有明显的锯齿。

解决办法: 在衔接处空余出一些距离让其应用渐变

html 复制代码
<html>
<head>
    <style>
        .box{
            width:300px;
            height: 300px;
            display:flex;
            justify-content: center;
            align-items: center;
            background: linear-gradient(90deg, #fff, #0400ff 100%);
        }
        .progress {
            width: 200px;
            height: 200px;
            border-radius: 50%;
            background: conic-gradient(#e1e43a 0, #e1e43a 25%, #83b596 25.3%, #83b596);
            mask: radial-gradient(transparent, transparent 50%, #000 50%, #000 0);
            -webkit-mask: radial-gradient(transparent, transparent 50%, #000 50%, #000 0);
        }
       
    </style>
</head>
<body>
    <div class="box">
        <span>进度:</span>
        <div class="progress"></div>
    </div>
</body>
</html>
相关推荐
ZC跨境爬虫5 小时前
跟着 MDN 学 HTML day_59:HTML表单与按钮——构建用户交互的基石
前端·javascript·ui·html·交互·媒体
ZC跨境爬虫5 小时前
模块化烹饪小程序开发日记 Day2:全局配置与 tabBar 实现
java·前端·javascript·微信小程序·html·notepad++
ZHOUPUYU6 小时前
PHP 开发实战:从零搭建一个高性能的 RESTful API 服务
运维·开发语言·后端·html·php
漂流瓶jz14 小时前
总结CSS组件化演进之路:命名规范/CSS Modules/CSS in JS/原子化CSS
前端·javascript·css
ZC跨境爬虫21 小时前
跟着 MDN 学 HTML day_56:(HTML 表格基础完全指南)
前端·javascript·ui·html·音视频
Dxy123931021621 小时前
CSS滤镜使用方法完全指南
前端·css
Dxy12393102161 天前
CSS中的filter属性详解
前端·css
ZC跨境爬虫1 天前
跟着 MDN 学 HTML day_55:HTML 音频与视频嵌入实战指南
前端·javascript·ui·html·音视频·媒体
Jack N1 天前
2026 浏览器原理 常见面试题(附答案)
前端·html·浏览器
何何____1 天前
CSS 易混淆易错知识点
前端·css