Css Sibling Absolute Positioning

后端 未结 5 420
温柔的废话
温柔的废话 2020-12-15 14:53

Is there any way to absolutely position a div relatively to its sibling? For example: Inside a div there are two other divs - div1 and div2. I want to absolutely position di

5条回答
  •  盖世英雄少女心
    2020-12-15 15:42

    The correct answer is: No, but at least its vertical position can be affected by siblings.

    As the other answers state, the position of an absolutely positioned div is relative to its ancestors. To be precice, its relative to the first ancestor that isn't statically positioned, when traversing up the DOM-tree.

    However: an absolutely positioned element will also be affected by its siblings (the ones that come before the element). If those preceding siblings are relatively positioned, and your absolutely positioned element has its top-property not set, then it's placed vertically below those relatively positioned siblings.

    relative 1
    relative 2
    absolute top 0
    absolute

    See this fiddle: https://jsfiddle.net/fgxeu54t/28/

提交回复
热议问题