简体   繁体   English

绝对定位的图像相对于绝对定位的父对象定位

[英]Absolute positioned image is positioning with respect to absolute positioned parent

I am unable to understand why the child image which is absolute positioned is getting its offset with respect to its parent which is also absolute. 我无法理解为什么绝对定位的子图像相对于其父代也是偏移的。 but, by definition it should position with respect to relative closest ancestor 但是,根据定义,它应该相对于相对最接近的祖先进行定位

 .supparent { width:100%; height:300px; position:relative; } .parent { position:absolute; top : 10%; left:10%; } .im { position:absolute; top:0%; left:0%; } 
 <div class="supparent"> <div class="parent"> <img src="https://tpc.googlesyndication.com/simgad/2621399702091823008" class="im"> </div> </div> 

And what is the hack to make it position with respect to supparent 相对于明显的地位,有什么技巧

By definition: from MDN 根据定义: 来自MDN

The absolutely positioned element is positioned relative to nearest positioned ancestor (non-static). 绝对定位的元素相对于最近定位的祖先(非静态)定位。 If a positioned ancestor doesn't exist, the initial container is used. 如果不存在已定位的祖先,则使用初始容器。

See this update with some bordered colors. 看到此更新带有一些带边框的颜色。 It does not matter if the parent is set position: relative; 父母是否设置好position: relative;无关紧要 or position: absolute; position: absolute;

 .supparent { width:100%; height:300px; position:relative; border: solid 5px orange; } .parent { position:absolute; top : 10%; left:10%; border: solid 5px red; } .im { position:absolute; top:0%; left:0%; border: solid 5px green; } 
 <div class="supparent"> <div class="parent"> <img src="https://tpc.googlesyndication.com/simgad/2621399702091823008" class="im"> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM