为什么容器内绝对定位需要容器相对
这对我来说没有意义。
如果我想要一个 div 距容器边缘 left:20px;top:20px,我应该能够执行诸如position:absolute-within-parent;之类的操作。定义子项的定位不应需要修改父项。
至少这样看起来更加模块化和解耦。作为一名程序员,我被教导要对此着迷。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绝对定位是相对于包含块的。包含块是具有相对或绝对定位的最近父级(如果找不到其他元素,则可能是
body
元素)。这允许您相对于其任何一个父元素定位一个元素。absolute-within-parent
会限制您只能相对于其直接父级绝对定位元素,这并不总是您所需要的。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.