CSS absolute positioned elements and margins

后端 未结 2 1808
你的背包
你的背包 2021-01-27 04:12

Am a right to conclude that a CSS margin (e.g. margin-left) influences the final position of an absolute postioned element? It seems a negative margin-left pulls it to the left

相关标签:
2条回答
  • 2021-01-27 04:25

    Correct. Margins influence where the edges of an absolutely positioned element begin.

    0 讨论(0)
  • 2021-01-27 04:33

    Lets understand it this way:

    When you have an statically-positioned element, the element is part of the normal flow of the document. Hence, any margins applied on it are considered 'with respect to its surrounding elements'.

    When you have an relatively-positioned element, the element is still part of the normal flow of the document. Hence, any margins applied on it are still considered 'with respect to its surrounding elements'.

    BUT,

    When you have an absolutely-positioned element, the element is taken out of the normal flow of the document. This element's positioning is now dictated by the first parent container that is not statically positioned (or the top level body element as a fallback). Hence, when you apply margin, the parent container is taken as the 'surrounding element' and the margin in calculated with 'respect to it'.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题