- position: static|absolute|fixed|relative|sticky|initial|inherit|unset;
- z-index: auto|number|initial|inherit;
|static||Default value. Elements render in order, as they appear in the document flow. The top, right, bottom, left and z-index properties do not apply.|
|relative||The element is positioned relative to its normal position, so |
|fixed||The element is positioned relative to the browser window|
|absolute||The element is positioned relative to its first positioned (not static) ancestor element|
|initial||Sets this property to its default value.|
|inherit||Inherits this property from its parent element.|
|sticky||Experimental feature. It behaves like |
|unset||Combination of initial and inherit. More info here.|
Normal Flow is the flow of elements if the position of element is static.
- defining width is beneficial because in some cases it prevents overlapping of element's content.
When absolute positioning is used the box of the desired element is taken out of the Normal Flow and it no longer affects the position of the other elements on the page. Offset properties:
specify the element should appear in relation to its next non-static containing element.
This code will move the box containing element with attribute
class="abspos" down 0px and right 500px relative to its containing element.
Defining position as fixed we can remove an element from the document flow and set its position relatively to the browser window. One obvious use is when we want something to be visible when we scroll to the bottom of a long page.
Overlapping Elements with z-index
To change the default stack order positioned elements (
position property set to
fixed), use the
The higher the z-index, the higher up in the stacking context (on the z-axis) it is placed.
In the example below, a z-index value of 3 puts green on top, a z-index of 2 puts red just under it, and a z-index of 1 puts blue under that.
This creates the following effect:
See a working example at JSFiddle.
|An integer value. A higher number is higher on the |
|Gives the element the same stacking context as its parent. (Default)|
All elements are laid out in a 3D axis in CSS, including a depth axis, measured by the
z-index only works on positioned elements: (see: Why does z-index need a defined position to work?). The only value where it is ignored is the default value,
Relative positioning moves the element in relation to where it would have been in normal flow .Offset properties:
are used to indicate how far to move the element from where it would have been in normal flow.
This code will move the box containing element with attribute class="relpos" 20px down and 30px to the right from where it would have been in normal flow.
The default position of an element is
static. To quote MDN:
This keyword lets the element use the normal behavior, that is it is laid out in its current position in the flow. The top, right, bottom, left and z-index properties do not apply.