CSS Positioning Parameter Details
- 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 left:20px adds 20 pixels to the element's LEFT position
- 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 position: static within its parent until a given offset threshold is reached, then it acts as position: fixed.
- unset - Combination of initial and inherit. More info here.
Overlapping Elements with z-index
To change the default stack order positioned elements (position property set to relative, absolute or fixed), use the z-index property. 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.
left: 50px;top: 150px;
This creates the following effect:
z-index: [ number ] | auto;
- number An integer value. A higher number is higher on the z-index stack. 0 is the default value. Negative values are allowed.
- auto 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 property. 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, static.
Read about the z-index property and Stacking Contexts in the CSS Specification on layered presentation and at the Mozilla Developer Network.
When absolute positioning has been 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.
- 1. top
- 2. left
- 3. right
- 4. bottom
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 relative 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.
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.
ATutorialHub Related Guide
panduranga gupta2021-07-05 07:03:13
good website for learning and help me a lot
The awsome website i am looking like for a long time, good work atutorialhub team keep doing
Learning a lot from the courses present on atutorialhub. The courses are very well explained. Great experience
It is very helpful to students and easy to learn the concepts
Great job Tutorials are easy to understand Please make use of it
Zain Khan2021-09-18 15:07:23
Great content and customized courses.
Rudrakshi Bhatt2021-09-09 15:08:10
Well structured coursed and explained really well!
Pavana Somashekar2021-09-11 15:09:08
Good platform for beginners and learn a lot on this website