Loading...
Loading...
The position property in CSS determines how an element will be placed on the page and what it will be positioned relative to.
top, right, bottom, left, z-index properties.Static example:
.element {
position: static;
}
Element remains in the flow but can be shifted using top, right, bottom, left.
The space occupied by the element remains in place.
Relative example:
.element {
position: relative;
top: 10px; /* Shifts element down */
}
Element is removed from the flow and positioned relative to the nearest ancestor with position: relative, absolute, fixed, or sticky. If no such ancestor exists, it's positioned relative to body.
Can use top, right, bottom, left properties.
Absolute example:
.container {
position: relative;
}
.element {
position: absolute;
top: 0;
left: 0; /* Positioned relative to container */
}
Element is removed from the flow and fixed relative to the browser window. Does not move when the page is scrolled. Used to create fixed elements such as headers or popup menus.
Fixed example:
.element {
position: fixed;
top: 0;
left: 0;
}
Element behaves like relative while its parent block is in the visible area of the screen.
Positioned relative to the browser window when scrolling, if it crosses the specified top, right, bottom, left values.
.element {
position: sticky;
top: 10px; /* Fixed as soon as it reaches 10px from the top of the screen */
}
Attention:
When using sticky, make sure the parent container has height. Otherwise, positioning won't work.
| Property | Document Flow | Positioned Relative To |
|---|---|---|
static | In flow | Not positioned |
relative | In flow | Itself |
absolute | Out of flow | Nearest positioned ancestor or body |
fixed | Out of flow | Browser window |
sticky | In flow until fixed | Nearest scrollable container or viewport boundaries |