Overview
See this MDN article on position as well as Learn CSS Layout and The Lowdown on Absolute vs. Relative Positioning.
The position property is not inherited and can be one of the following values:
- static - The default value. Static elements are not positioned. They are part of the document flow and ignore the top, right, bottom and left edge-position properties.
- absolute - Positions an element to the specified edge-position, relative to its closest positioned ancestor or containing block. An absolute element may overlap other elements.
- relative - Same as absolute, but preserves the space in the flow it would have occupied had it not been moved (what good is that?). A relative element may overlap other elements.
- fixed - Positions an element to the specified edge-position, relative to the browser's window, and doesn't move when the window is scrolled. A fixed element may overlap other elements.
Shared CSS
This section defines styles shared by multiple experiments on this page. The styles are applied to three nested divs: outermost, which contains middleGrid, which in turn contains innermost.
Absolute To Relative
This section experiments with positioning an absolute block within a relative block.
Innermost with Margin
Innermost is positioned 40px from the top and 40px from the left of MiddleGrid and is given a 20px margin. Since it's an absolute-positioned div inside a relative one, the results are as expected: Innermost is positioned 60px from the left and 60px from the top, with no side-effects.
MiddleGrid was not given a margin, since it's a relative div nested inside a static div. To add a margin would trigger the Vertical Margin Transfer issue.
Two Innermost Boxes Overlap
The first innermost div is positioned 40px from the top and left of the middle div. The second innermost div is positioned 80px from the top and left of the middle div. Both were given given a 10px margin. Since they are absolute-positioned divs inside a relative div, the results are as expected: each innermost div is positioned as specified, with no side-effects.
MiddleGrid was not given a margin, since it's a relative div nested inside a static div. Adding a margin would have triggered the Vertical Margin Transfer issue.
Relative To Absolute
This section experiments with positioning a relative block within an absolute block.
Innermost and Middle with Margins
Innermost is positioned 40px from the top and 40px from the left of MiddleGrid and is given a 20px margin. Since it's an absolute-positioned div inside a relative one, the results are as expected: Innermost is positioned 60px from the left and 60px from the top, with no side-effects.
MiddleGrid was not given a margin, since it's a relative div nested inside a static div. To add a margin would trigger the Vertical Margin Transfer issue.
Two Innermost Boxes Overlap
Absolute To Absolute
This section experiments with positioning an absolute block within an absolute block.
Innermost and Middle with Margins
Innermost is positioned 40px from the top and 40px from the left of MiddleGrid and is given a 20px margin. Since it's an absolute-positioned div inside an absolute one, the results are as expected: Innermost is positioned 60px from the left and 60px from the top, with no side-effects.
MiddleGrid was also given a margin (10px). Since it's an absolute div nested inside an absolute one, its results are as expected: a 10px margin all around, with no side-effects..