【问题标题】:How to display data label inside HTML5 progress bar? (cross-browser)如何在 HTML5 进度条内显示数据标签? (跨浏览器)
【发布时间】:2017-05-16 17:48:48
【问题描述】:

我有一个样式化的 HTML5 进度条,我想在进度条内显示跨浏览器兼容的数据标签。目前显示在进度条上方。

HTML:

<progress max="100" value="50" data-label="50% Complete"></progress>

CSS:

progress {
  text-align:center;
  height: 1.5em;
  width: 100%;
  -webkit-appearance: none;
  border: none;
}
progress:before {
  content: attr(data-label);
  font-size: 0.8em;
  vertical-align: 0
}
progress::-webkit-progress-bar {
  background-color: #c9c9c9;
}
progress::-webkit-progress-value,
progress::-moz-progress-bar {
  background-color: #7cc4ff;
}

我得到了下面的结果,但我想将数据标签移动到进度条内部:

【问题讨论】:

  • 顺便说一句,我使用的是 Firefox 49.0.2,进度条标签没有显示。如果你想让它跨浏览器,我建议你制作经典的 HTML 进度条,或者至少打印进度。

标签: html css cross-browser position progress-bar


【解决方案1】:

TL;DR:不要在 &lt;progress&gt; 元素上使用伪元素。

正如其他答案和me in the comments 所说,HTML/CSS 进度条将是比使用&lt;progress&gt; 元素更好的解决方案。

基于 Gecko 的浏览器,如 firefox,根本不会显示伪元素。

但是,要回答您的问题,只需将文本放在进度条上即可:

progress {
  text-align: center;
  height: 1.5em;
  width: 100%;
  -webkit-appearance: none;
  border: none;
  
  /* Set the progressbar to relative */
  position:relative;
}
progress:before {
  content: attr(data-label);
  font-size: 0.8em;
  vertical-align: 0;
  
  /*Position text over the progress bar */
  position:absolute;
  left:0;
  right:0;
}
progress::-webkit-progress-bar {
  background-color: #c9c9c9;
}
progress::-webkit-progress-value {
  background-color: #7cc4ff;
}
progress::-moz-progress-bar {
  background-color: #7cc4ff;
}
&lt;progress max="100" value="50" data-label="50% Complete"&gt;&lt;/progress&gt;

注意,这不有很好的浏览器支持(事实上,这很可怕),因为&lt;progress&gt; 是replaced element,就像&lt;input&gt;。

CSS 规范并不完全清楚被替换的元素是否可以具有伪元素,因此不同的浏览器有不同的渲染。基于 Webkit 的浏览器,例如 Chrome,有时会显示它们。基于 Gecko,例如 Firefox,不会。

请参阅this answer,了解一些类似的问题。

所以,如果这是针对网站,而不是 electron 应用或类似应用,我强烈建议使用 HTML/CSS 进度条:

.progress {
  height: 1.5em;
  width: 100%;
  background-color: #c9c9c9;
  position: relative;
}
.progress:before {
  content: attr(data-label);
  font-size: 0.8em;
  position: absolute;
  text-align: center;
  top: 5px;
  left: 0;
  right: 0;
}
.progress .value {
  background-color: #7cc4ff;
  display: inline-block;
  height: 100%;
}
<div class="progress" data-label="50% Complete">
  <span class="value" style="width:50%;"></span>
</div>

注意:即使这是针对带有基于 webkit 浏览器的应用程序,您仍然不应该在 &lt;progress&gt; 上使用伪元素。正如我上面所说,此功能的规格尚不清楚,将来可能会发生变化,从而破坏您的进度元素。 Webkit 也可以放弃对此的支持。

我建议只使用 HTML/CSS 进度条,以后会省去很多麻烦。

【讨论】:

  • 哦,我明白了,那我现在不明白为什么人们会使用它,至少我会等到与大多数浏览器版本兼容。
  • @Troyer 它可能永远不会与大多数浏览器兼容,因为它不是规范的一部分。 Webkit 只是简单地展示了它们,但考虑到 textarea 等不支持它,我发现 webkit 对此的支持可能会在未来某个时候被放弃。
【解决方案2】:

您需要做的就是将状态文本设置为在状态文本元素上设置 z-index 或 1(-1 无效,0 是默认值),然后设置负数 margin-top: -32px(对于height: 32px) 并且您可以保留 progress 元素而无需添加任何多余内容:

<div style="z-index: 2;"><span>Staging item 144 of 271...</span></div>
<div style="margin-top: -32px;"><progress max="100" value="44">44%</progress></div>

【讨论】:

    【解决方案3】:

    显然我需要用居中的文本制作 CSS HTML 进度条并找到这个答案,但这里的答案没有居中的文本。这是带有居中文本的 HTML、CSS 进度条。

    #progress_container {
      width: 100px;
      background-color: rgb(161, 161, 161);;
      height: 30px;
      position: relative;
    }
    
    #progress_container_text {
      position: absolute;
      left: 0;
      right: 0;
      height: 100%;
    }
    
    #progress_container_text_align_center {
      display: flex;
      height: 100%;
      justify-content: center;
      align-items: center;
    }
    
    #loading_bar {
      width: 30%;
      height: 100%;
      background-color: rgb(58, 166, 255);
    }
    <div id="progress_container">
      <div id="progress_container_text">
        <div id="progress_container_text_align_center">30%</div>
      </div>
      <div id="loading_bar"></div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 2016-12-20
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多