如何使用以下结构将 div 中的文本居中对齐:
.centerText {
position: absolute;
left: 100px;
top: 20px;
text-align: center;
font-size: 25px;
}
<div class="centerText">Hello World</div>
这样做仍然会将文本与左侧对齐;距左侧 100 像素。
要在仍在使用时将文本居中,left: 100px
您需要添加right: 100px
以自动设置 div 的宽度,并在两侧具有相同的间距,因此也将 div 居中。
.centerText {
position: absolute;
left: 100px;
right: 100px;
top: 20px;
text-align: center;
font-size: 25px;
}
<div class="centerText">Hello World</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句