CSS works with boundaries called normal flow . Elements stack on top of each other moving from top down, in the order in which elements appears in your HTML.
Types of Positioning
CSS offers 5 position
properties:
static
relative
absolute
fixed
inherit
They can be split into two categories:
- Static-type positioning -
static
- Relative-type positioning -
relative
,absolute
,fixed
Each property is used for specific purposes.
TL;DR
Position | Normal flow | Offset properties | Relative to |
Static | ✔️ | ❌ | ❌ |
Relative | ✔️ | ✔️ | Itself |
Absolute | ❌ | ✔️ | Nearest parent |
Fixed | ❌ | ✔️ | Viewport |
Static-type Positioning
position: static
- Default
position
value of an element - Single-column layout, stack on top of each other like a tower
- Cons: Cannot use offset properties:
top
,left
,right
,bottom
Relative-type Positioning
position: relative
- Behave exactly like
static
elements - Elements are still in the flow of document (element still takes up space)
- Pros:
Able to adjust elements using offset properties:
top
,left
,right
,bottom
Create reference point for offset properties (coordinate system)
position: absolute
- Elements are removed from normal flow (elements do not take up space)
- Placed anywhere without affecting other elements in the flow
- Pros:
Respond to offset properties for positioning
Create new coordinate system for child elements Offset coordinates are in respect to each parent element
position: fixed
- Similar to
absolute
element, but browser window (viewport) positionsfixed
elements - Does not scroll with document => element stays fixed in place
position: inherit
- Element inherits value from parent element
Illustration by Murat Kalkavan from Icons8
See Also
- ishadeed.com/article/learn-css-positioning (Really cute explanations using cat illustrations!)
- developer.mozilla.org/en-US/docs/Learn/CSS/..
- alistapart.com/article/css-positioning-101
- medium.com/@jacobgreenaway12/taming-the-css..
- internetingishard.com/html-and-css/advanced..