<div>
<p>這是文字<p>
</div>
div{positon:relative;width:200px;height:200px}想讓p距離div底部20px;p使用了position:absolute;bottom:20px;定位后p不能水平居中,好像p必須設(shè)置width才能行,p是文字無法知道寬度,該怎么居中
7 回答
已采納

qq_青棗工作室_0
TA貢獻446條經(jīng)驗 獲得超754個贊
不用 position:absolute;
直接在div上 padding-bottom: 20px; 或者,在p:last-child 上 margin-bottom: 20px;

肥柴
TA貢獻7條經(jīng)驗 獲得超10個贊
*{border:1px?solid?red}/*框只是為了看起來直觀*/ div{position:relative;width:200px;height:200px;text-align:center;} p{position:absolute;bottom:20px;width:100%;}
另外:不是非常必要話,個人很不推薦絕對定位

不再熱情似火
TA貢獻15條經(jīng)驗 獲得超6個贊
用了position:absolute 之后,即使你設(shè)置了寬度,margin: 0 auto 也不會居中的,可以用text-align:center,來實現(xiàn)居中,(少用position屬性)

ThinkAllNight
TA貢獻35條經(jīng)驗 獲得超31個贊
p是塊狀元素,寬度自適應(yīng)父元素的寬度,即100% , 此時若要居中,可給p元素添加屬性
p{text-align:center;}
添加回答
舉報
0/150
提交
取消