[英]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.