Why Does Absolute Positioning Within Container Require Container to be Relative

柔情痞子 提交于 2019-12-10 03:34:35

问题


It doesn't make sense to me.

If I want a div to be left:20px;top:20px from the container edge, I should be able to do something like position:absolute-within-parent;. Defining the positioning of a child should not require modification of the parent.

At least it seems more modular and decoupling that way. And as a programmer I've been taught to obsess with that.


回答1:


Absolute positioning is relative to something called the containing block. The containing block is the closest parent which has relative or absolute positioning (which may be the body element if nothing else could be found). This allows you to position an element relative to any one of its parents. absolute-within-parent would restrict you to only being able to absolutely position an element relative to its immediate parent, which isn't always what you need.



来源:https://stackoverflow.com/questions/6725754/why-does-absolute-positioning-within-container-require-container-to-be-relative

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!