There are a lot of questions on S.O. that cover the answer to how to fix this (add top: 0
), but none of them attempt to actually explain the reasoning behind the header movement. I'm more curious as to why this is the case.
<header>Project Header</header>
<main class="container" id="layout-mainContent">
<div class="row" id="first-row">somecontent</div>
</main>
header {
position: fixed;
}
#layout-maincontent {
margin-top: 90px; //moves header down.
}
List of like-questions but with no reasoning:
- Topmost 'fixed' position div moving with non position div
- margin affects other fixed elements position
- margin-top div causes fixed header div to move down
It seems reasonable to think that the fixed header sticks to the top of the browser window and should not move because of another non-positioned, non-child, non-parent div (aka sibling). Esp. because the fixed header is outside of normal document flow. MDN on Fixed Positioning
Hypothesis: The confusion stems from the idea that fixed elements are relative to the browser window. This is true, but is calculated using the viewport. The viewport is calculated using elements that are within the regular document flow. Because the first div that is within document flow is the non-header div, the viewport starts after the margin-top is applied. This is just speculation and I would love to see someone confirm or correct me.
position: fixed
suddenly shifted my header downwards, thanks a lot for the tip on usingtop: 0
! – Horwitz