我记得很久以前在某个地方读过,绝对定位一个元素而不给它任何 top
、right
、bottom
或 left
属性将(与任何定位元素一样)将其从流中取出,但将其保留在同一个包含 block 中(静态定位时它应该在的那个包含 block )。
在下面的示例 ( jsFiddle ) 中,元素 #2 和 #3 是绝对定位的,但 #2 没有任何附加属性。
我一直在寻找解释该行为的文档,以便我可以继续使用它而不必担心可能的反斜杠,但在 Gooogle 或 SO 上并不幸运。有人能指出我正确的方向吗?
最佳答案
对于绝对定位的元素,偏移量(top
、left
等)的默认值为auto
。
在这种情况下,即使内容已从文档流中取出,该元素仍保留在它应有的位置 position: static
。
对“静态位置”概念的引用包括:
http://www.w3.org/TR/CSS2/visudet.html#abs-non-replaced-width
http://www.w3.org/TR/CSS2/visudet.html#abs-non-replaced-height
http://www.w3.org/TR/CSS2/visuren.html#position-props
引用:
10.3.7 Absolutely positioned, non-replaced elements
For the purposes of this section and the next, the term "static position" (of an element) refers, roughly, to the position an element would have had in the normal flow. More precisely:
The static-position containing block is the containing block of a hypothetical box that would have been the first box of the element if its specified 'position' value had been 'static' and its specified 'float' had been 'none'.
关于css - 包含没有 "top"、 "right"、 "bottom"或 "left"的绝对定位元素的 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19119748/