HTML做多颜色进度条

文章展示了如何使用HTML和CSS创建具有不同颜色层次的进度条,从基础的单层进度条开始,逐步改进添加了底层背景颜色,然后增加了标尺线,最后通过提取CSS样式进行优化,使其更易于维护。

先上效果图
在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
  <title>多颜色进度条</title>
</head>
<body>
  <div style="width: 300px; height: 20px; background-color: #eee; border-radius: 10px; overflow: hidden;">
    <div style="height: 100%;">
      <div style="width: 30%; background-color: #00ffce; height: 100%; float: left;"></div>
      <div style="width: 20%; background-color: #ffc02f; height: 100%; float: left;"></div>
      <div style="width: 40%; background-color: #fff2cf; height: 100%; float: left;"></div>
      <div style="width: 10%; background-color: #fde0e2; height: 100%; float: left;"></div>
    </div>
  </div>
</body>
</html>

首次改进后
底层背景颜色在这里插入图片描述

用于表层进度的颜色在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
  <title>多层多颜色进度条</title>
</head>
<body>
  <div style="width: 300px; height: 20px; background-color: #eee; border-radius: 10px; overflow: hidden;">
    <div style="height: 100%; position: relative;">
      <!-- 第一层进度条 -->
      <div style="width: 20%; background-color: #d4ffec; height: 100%; position: absolute;"></div>
      <div style="width: 60%; background-color: #fff2cf; height: 100%; position: absolute; left: 20%;"></div>
      <div style="width: 20%; background-color: #fde0e2; height: 100%; position: absolute; left: 80%;"></div>
      
      <!-- 第二层进度条 -->
      <div style="width: 20%; background-color: #00ffce; height: 100%; position: absolute;"></div>
      <div style="width: 60%; background-color: #ffc02f; height: 100%; position: absolute; left: 20%;"></div>
      <div style="width: 20%; background-color: #ff5733; height: 100%; position: absolute; left: 80%;"></div>
    </div>
  </div>
</body>
</html>

二次改进
增加标尺线在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
  <title>多层多颜色进度条</title>
</head>
<body>
  <div style="width: 300px; height: 25px; background-color: #eee; border-radius: 10px; overflow: hidden;">
	<div style="height: 100%; position: relative;">
      <!-- 第一层进度条 -->
      <div style="width: 20%; background-color: #d4ffec; height: 100%; position: absolute;"></div>
      <div style="width: 60%; background-color: #fff2cf; height: 100%; position: absolute; left: 20%;"></div>
      <div style="width: 20%; background-color: #fde0e2; height: 100%; position: absolute; left: 80%;"></div>
      
      <!-- 第二层进度条 -->
      <div style="width: 20%; background-color: #00ffce; height: 100%; position: absolute;"></div>
      <div style="width: 60%; background-color: #ffc02f; height: 100%; position: absolute; left: 20%;"></div>
      <div style="width: 20%; background-color: #ff5733; height: 100%; position: absolute; left: 80%;"></div>
    </div>
    <div style="width: 2px; height: 34px; background-color: transparent; position: absolute; left: 7%; top: 0; margin-top: 4px; border-left: 2px dashed red; z-index: 1;"></div>
    
  </div>
</body>
</html>    

提取css样式后

<!DOCTYPE html>
<html>
<head>
  <title>多层多颜色进度条</title>
  <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
  <div class="progress-container">
    <div class="progress-bar">
      <!-- 第一层进度条 -->
      <div class="progress-layer" style="width: 20%; background-color: #d4ffec;"></div>
      <div class="progress-layer" style="width: 60%; background-color: #fff2cf; left: 20%;"></div>
      <div class="progress-layer" style="width: 20%; background-color: #fde0e2; left: 80%;"></div>
      
      <!-- 第二层进度条 -->
      <div class="progress-layer" style="width: 20%; background-color: #00ffce;"></div>
      <div class="progress-layer" style="width: 60%; background-color: #ffc02f; left: 20%;"></div>
      <div class="progress-layer" style="width: 20%; background-color: #ff5733; left: 80%;"></div>
    </div>
    <div class="dashed-line"></div>
  </div>
</body>
</html>
/* styles.css */
.progress-container {
  width: 300px;
  height: 25px;
  background-color: #eee;
  border-radius: 10px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  position: relative;
}

.progress-layer {
  height: 100%;
  position: absolute;
}

/* 垂直虚线样式 */
.dashed-line {
  width: 2px;
  height: 34px;
  background-color: transparent;
  position: absolute;
  left: 7%;
  top: 0;
  margin-top: 4px;
  border-left: 2px dashed red;
  z-index: 1;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gristle_egg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值