为什么在容器中绝对定位需要容器是相对的 [英] Why Does Absolute Positioning Within Container Require Container to be Relative

查看:186
本文介绍了为什么在容器中绝对定位需要容器是相对的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说没有意义。

如果我想要一个div离开:20px; top:20px从容器边缘,我应该能做一些类似于position:absolute-within-parent;定义子节点的位置不应该需要修改父节点。

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.

推荐答案

绝对定位是相对于称为包含块的东西。包含块是具有相对或绝对定位的最接近的父(其可以是主体元素,如果没有找到其他)。这允许您相对于其父项中的任何一个定位元素。 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.

这篇关于为什么在容器中绝对定位需要容器是相对的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆