我有一个样式的HTML5进度条,我想在进度条跨浏览器兼容的情况下显示数据标签.目前它显示在进度条上方.
HTML:
<progress max="100" value="50" data-label="50% Complete"></progress>
Run Code Online (Sandbox Code Playgroud)
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 {
background-color: #7cc4ff;
}
progress::-moz-progress-bar
{
background-color: #7cc4ff;
}
Run Code Online (Sandbox Code Playgroud)
我得到以下结果(见图),但我想在进度条中移动数据标签:
非常感谢您的建议.
Jac*_*ray 20
<progress>.正如其他答案所述,以及我在评论中所说的,HTML/CSS进度条比使用<progress>元素更好.
基于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;
}Run Code Online (Sandbox Code Playgroud)
<progress max="100" value="50" data-label="50% Complete"></progress>Run Code Online (Sandbox Code Playgroud)
请注意,这并不能有很好的浏览器支持(事实上,这是相当可怕),因为<progress>是替换元素,像<input>.
如果被替换的元素可以具有伪元素,则CSS规范并不完全清楚,因此不同的浏览器具有不同的渲染.基于Webkit的浏览器(例如Chrome)有时会显示它们.基于Gecko的,如Firefox,不会.
在类似的问题上看到这个答案.
因此,如果这是针对网站而非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%;
}Run Code Online (Sandbox Code Playgroud)
<div class="progress" data-label="50% Complete">
<span class="value" style="width:50%;"></span>
</div>Run Code Online (Sandbox Code Playgroud)
注意:即使这是针对具有基于webkit的浏览器的应用程序,您仍然不应该使用psuedo元素<progress>.正如我上面所说,这个功能的规格还不清楚,将来可能会改变,打破你的进步元素.Webkit也可以放弃对此的支持.
我建议只使用HTML/CSS进度条,以后将为您节省很多麻烦.
| 归档时间: |
|
| 查看次数: |
14816 次 |
| 最近记录: |