文字对齐和绝对定位

时间:2018-12-12 13:32:08

标签: html css position absolute text-align

我想将第二个div中的文本对齐到第一个div的最右侧。通常用text-align来完成:但是,似乎绝对定位(使用该定位是为了使文本不会在页面上移动其他元素)绝对不允许这样做。这两个div元素只是在页面的左侧相互转换。

<div style=" color:#ffffff; position: absolute;">
Im on the left.
</div>
<div style=" color:#ffffff; position: absolute; text-align: right;">
I need to be on the right.
</div>

1 个答案:

答案 0 :(得分:1)

<div style=" color:#ffffff; position: absolute;top:0;left:20px;">
Im on the left.
</div>
<div style=" color:#ffffff; position: absolute; top:0;right: 20px;">
I need to be on the right.
</div>

尝试一下,希望它能正常工作,如果父元素不是position: relative,请确保父元素具有body

相关问题