div中的中心文本,里面有div

Ron*_*ero 0 html css

我有这个代码:

<div style="border:1px solid #afafaf;background-color:#efefef;width:100px;height:14px;">
    <div style="text-align:center;width:50px;height:14px;background-color:green;">
        50%
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

如果第一个DIV,第二个div可能有0到100像素的宽度(进度条),我如何将50%放在中间

thi*_*dot 5

现场演示

无论width您的进度条是什么,它都不会影响文本div.

HTML:

<div style="border:1px solid #afafaf;background-color:#efefef;width:100px;height:14px;position:relative">
    <div style="text-align:center;width:50px;height:14px;background-color:green;"></div>
    <div id="text">50%</div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

#text {
    position: absolute;
    width: 100%;
    top: 0;
    left: 0;
    text-align: center
}
Run Code Online (Sandbox Code Playgroud)