我一直在使用Twitter的Bootstrap,希望进度条上的文本无论值为多少都能居中显示。
以下链接是我现在拥有的。我希望所有文本都与最底部的进度条对齐。
截图如下:
我已经尝试过使用纯CSS来解决问题,并且正在尽可能避免使用JS,但如果这是最清晰的解决方法,我也愿意接受它。
<div class="progress">
<div class="bar" style="width: 86%">517/600</div>
</div>
我一直在使用Twitter的Bootstrap,希望进度条上的文本无论值为多少都能居中显示。
以下链接是我现在拥有的。我希望所有文本都与最底部的进度条对齐。
截图如下:
我已经尝试过使用纯CSS来解决问题,并且正在尽可能避免使用JS,但如果这是最清晰的解决方法,我也愿意接受它。
<div class="progress">
<div class="bar" style="width: 86%">517/600</div>
</div>
Bootstrap 5:(与 v4x 相同)
<div class="progress position-relative">
<div class="progress-bar" role="progressbar" style="width: 60%" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100"></div>
<small class="justify-content-center d-flex position-absolute w-100">60% complete</small>
</div>
使用实用类的Bootstrap 4:(感谢MaPePeR的贡献)<div class="progress position-relative">
<div class="progress-bar" role="progressbar" style="width: 60%" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100"></div>
<small class="justify-content-center d-flex position-absolute w-100">60% complete</small>
</div>
Bootstrap 3:
Bootstrap现在支持进度条中span元素内的文本。HTML与Bootstrap示例相同。(请注意,类sr-only
已被删除)
HTML:
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%;">
<span>60% Complete</span>
</div>
</div>
...然而,它只会将文字居中于进度条本身,因此我们需要一些自定义CSS。
将以下内容粘贴到另一个样式表/加载Bootstrap CSS的位置下面:
CSS:
/**
* Progress bars with centered text
*/
.progress {
position: relative;
}
.progress span {
position: absolute;
display: block;
width: 100%;
color: black;
}
JsBin文件:http://jsbin.com/IBOwEPog/1/edit
Bootstrap 2:
将以下内容粘贴到另一个样式表中,或者在加载Bootstrap的CSS之下:
/**
* Progress bars with centered text
*/
.progress {
position: relative;
}
.progress .bar {
z-index: 1;
position: absolute;
}
.progress span {
position: absolute;
top: 0;
z-index: 2;
color: black; /* Change according to needs */
text-align: center;
width: 100%;
}
然后通过在.bar
外添加一个span
元素来向进度条添加文本:
<div class="progress">
<div class="bar" style="width: 50%"></div>
<span>Add your text here</span>
</div>
以下是Bootstrap 3的答案,如Bootstrap示例所示
<div class="progress text-center">
<div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%;">
<span>60% Complete</span>
</div>
<span>40% Left</span>
</div>