[英]Why an absolutely positioned element inside centered relatively positioned div is not centered?
我不明白為什么下面的代碼不能使圖像居中。 你能幫助我嗎?
<div align="center" style="position:relative;">
<div style="position: absolute">
<img src="a.png"/>
</div>
</div>
使用絕對值時必須設置top和left屬性
要使圖像水平和垂直居中,請添加left:0; right:0; top:0; bottom:0; margin: auto;
left:0; right:0; top:0; bottom:0; margin: auto;
<div style="position:relative; height: 100%; background: #eee;">
<div >
<img style="position: absolute; left:0; right:0; top:0; bottom:0; margin: auto;" src="http://placehold.it/350x150"/>
</div>
</div>
如果要水平居中,則必須定義left
和right
屬性。
看看演示 。
<div style="position:relative; border-top:1px solid red;">
<div style="position: absolute; left:50%; right:50%;">
<img src="http://placehold.it/100x100" />
</div>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.