gpt4 book ai didi

css - 绝对定位遵循什么层次

转载 作者:行者123 更新时间:2023-11-28 06:48:03 25 4
gpt4 key购买 nike

当父级既不是绝对也不是相对时,我无法理解绝对定位。我知道绝对定位会根据相对于它的父级的偏移量来放置自己,它可以是相对的也可以是绝对的。
如果父级不是绝对的还是相对的?它会将自己定位到最近的绝对或相对元素。或者它会相对于 body 定位自己吗?

当 parent 是相对的或绝对的时,我没有问题。如果 parent 是静态的,我很难理解会发生什么。绝对元素将如何放置自己

最佳答案

这是在这里定义的:http://www.w3.org/TR/css3-positioning/#def-cb

该引用资料中的相关内容:

For fixed, absolute, center and page, it is defined as follows:

If the element has 'position: absolute', the containing block is established by the nearest ancestor with a position other than static, in the following way: 1. In the case that the ancestor is block-level, the containing block is formed by the padding edge of the ancestor. 2. In the case that the ancestor is inline-level, the containing block depends on the direction property of the ancestor ...

If there is no such ancestor, the containing block is the initial containing block

此处有更多详细信息:http://www.w3.org/TR/CSS2/visudet.html#containing-block-details

所以,直接回答你的问题——包含 block 是由最近的祖先建立的,其位置不是静态的。如果没有这样的祖先,则包含 block 是初始包含 block 。这恰好是 html。这就是为什么许多开发人员选择为 body 提供 position: relative 以避免与视口(viewport)混淆的原因。

关于css - 绝对定位遵循什么层次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33934689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com