进度条显示

188 阅读2分钟

进度条的背景色设置为绿色的渐变效果,从左到右由浓变淡,可以使用 CSS 的线性渐变功能,并设置适当的颜色。以下是示例代码:

html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
    .progress-container {
        width: 100%; /* 设置容器宽度 */
        background-color: #e0e0e0; /* 设置容器背景色 */
        border-radius: 5px; /* 设置圆角 */
        overflow: hidden; /* 隐藏超出的内容 */
        position: relative; /* 使子元素绝对定位相对于这个容器 */
    }

    .progress-bar {
        height: 20px; /* 设置进度条高度 */
        background: linear-gradient(to right, #006400, #76c7c0); /* 设置渐变背景色 */
        text-align: center; /* 使文字居中 */
        line-height: 20px; /* 设置行高与进度条高度相同,实现垂直居中 */
        color: white; /* 设置文字颜色 */
        border-radius: 5px; /* 设置圆角 */
        position: relative; /* 使子元素绝对定位相对于这个容器 */
    }

    .progress-text {
        position: absolute; /* 使文字绝对定位 */
        width: 100%; /* 使文字宽度占满整个容器 */
        top: 0; /* 与顶部对齐 */
        left: 0; /* 与左边对齐 */
        text-align: center; /* 使文字居中 */
        line-height: 20px; /* 设置行高与进度条高度相同,实现垂直居中 */
        color: black; /* 设置文字颜色 */
    }
</style>
</head>
<body>

<table>
    <tr>
        <td>
            <div class="progress-container">
                <div class="progress-bar" style="width: 75%;">
                    <div class="progress-text">75%</div>
                </div>
            </div>
        </td>
        <td>
            <div class="progress-container">
                <div class="progress-bar" style="width: 50%;">
                    <div class="progress-text">50%</div>
                </div>
            </div>
        </td>
    </tr>
    <!-- 添加更多行和数据 -->
</table>

</body>
</html>

解释说明:

  • background: linear-gradient(to right, #006400, #76c7c0); : 这行代码设置了进度条的背景色为从左到右的线性渐变,颜色从浓绿色 #006400 渐变到淡绿色 #76c7c0
  • .progress-text: 将进度条的文本作为 progress-bar 的子元素进行定位,这样确保文本始终在进度条的中间。

通过这种方式,进度条的背景色将显示为绿色从左到右由浓变淡的渐变效果。你可以根据需要调整渐变颜色的起点和终点颜色。