html - 在进度条背景上居中进度值

标签 html css alignment progress

我在我的页面上使用了 progress 元素,但是我的背景有问题,因为背景和填充部分没有居中对齐。

这是我的进度

<progress className={'progress'} max="100" value={value}>
        {value}%
      </progress>

通过CSS

.progress {
    height: 10px
    background: #BABABA center;
    border-color: transparent;
    border-radius: 6px;
  }

  progress::-webkit-progress-bar {
    height: 10px;
    background-color: #BABABA;
    border-radius: 6px;
  }
  progress::-webkit-progress-value {
    height: 16px;
    background-color: #128CA2;
    border-radius: 6px;
  }
  progress::-moz-progress-bar {
    background-color: #128CA2;
    border-radius: 6px;
  }

如何让背景和填充部分居中对齐?

Not center aligned

最佳答案

新答案

您可以在进度值上使用 position: absolute; 并使用 top: -3px; 使其居中。

查看片段中的评论:

.progress {
  height: 10px background: #BABABA center;
  border-color: transparent;
  border-radius: 6px;
}

progress::-webkit-progress-bar {
  position: relative; /* Added this */
  height: 10px;
  background-color: #BABABA;
  border-radius: 6px;
}

progress::-webkit-progress-value { /* Modified below */
  position: absolute;
  top: -3px;
  height: 16px;
  background-color: #128CA2;
  border-radius: 6px;
}

progress::-moz-progress-bar {
  background-color: #128CA2;
  border-radius: 6px;
}
<progress class="progress" max="100" value="10"></progress>


旧答案

您可以使用 linear-gradient 在视觉上降低条形图的彩色背景高度:

.progress { /* Modified this */
  height: 16px;
  background: linear-gradient(transparent 20%, #BABABA 20%, #BABABA 80%, transparent 80%) center;
  border-color: transparent;
  border-radius: 16px;
}

progress::-webkit-progress-bar {
  height: 10px;
  background: transparent; /* Modified here */
  border-radius: 6px;
}

progress::-webkit-progress-value {
  height: 16px;
  background-color: #128CA2;
  border-radius: 6px;
}

progress::-moz-progress-bar {
  background-color: #128CA2;
  border-radius: 6px;
}
<progress class="progress" max="100" value="10"></progress>

希望对您有所帮助。

关于html - 在进度条背景上居中进度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51494098/

相关文章:

html - 如何设置 html 元素,使其耗尽剩余的可视垂直空间

python - Python 的 Tabulate 逗号对齐错误

html - 如何水平居中元素?

javascript - 如何防止在 touchmove 和 mousemove 期间重定向 href?

html - 最大宽度不适用于移动设备

html - 如何在 Liquid 模板中声明换行符变量?

html - 如何在单击时消除按钮周围的焦点

javascript - 使内联 CSS 比外部 CSS 更强大!重要吗?

jquery - 单击按钮时获取同一行元素的值,每行一个按钮

javascript - 如何仅使用 JavaScript 从 <tbody> 中删除每第三行?