Master the art of placing elements exactly where you want them on the page
CSS positioning allows you to control the layout of elements on your webpage. The position property specifies the type of positioning method used for an element.
Each positioning type behaves differently in relation to the document flow and other elements. Let's explore each one in detail.
This is the default position value for all elements. Elements render in order, as they appear in the document flow.
top, right, bottom, left properties.element {
position: static; /* Default value */
}
The element is positioned relative to its normal position in the document flow.
top, right, bottom, left.element {
position: relative;
top: 20px;
left: 30px;
}
The element is removed from the normal document flow and positioned relative to its nearest positioned ancestor.
.parent {
position: relative; /* Creates positioning context */
}
.child {
position: absolute;
top: 0;
right: 0;
}
The element is positioned relative to the viewport (browser window). It stays in the same place even when the page is scrolled.
.fixed-element {
position: fixed;
bottom: 20px;
right: 20px;
}
A hybrid of relative and fixed positioning. The element is treated as relative positioned until it crosses a specified threshold, then it becomes fixed.
.sticky-header {
position: sticky;
top: 0; /* Threshold where it becomes fixed */
}
Dropdowns typically use absolute positioning relative to their trigger button, with a higher z-index to appear above other content.
Modals use fixed positioning to cover the entire viewport, often with a semi-transparent overlay.