CSS provides different positioning methods to control how elements are placed on a webpage.
5 Main CSS Positioning Properties
Static (default) – Normal document flow
Relative – Moves element relative to its normal position
Absolute – Positions element relative to its nearest positioned ancestor
Fixed – Stays at a fixed position, even when scrolling
Sticky – Toggles between relative and fixed based on scroll position
1️⃣ position: static; (Default)
👉 Normal flow (default behavior of all elements)
📌 The element appears as it normally would in the document.
.box {
position: static;
}
✅ Use case: Default position (rarely modified).
2️⃣ position: relative;
👉 Moves the element relative to where it would be normally.
📌 Uses top, right, bottom, left to adjust.
.box {
position: relative;
top: 20px; /* Moves 20px DOWN */
left: 30px; /* Moves 30px RIGHT */
}
✅ Use case: Offset an element without affecting nearby elements.
3️⃣ position: absolute;
👉 Moves element relative to its nearest positioned ancestor.
📌 If no parent has position: relative;, it positions relative to <html> (viewport).
.parent {
position: relative; /* Needed to make child position correctly */
width: 300px;
height: 200px;
background: lightgray;
}
.child {
position: absolute;
top: 10px;
right: 10px;
background: red;
}
✅ Use case: Floating elements like modals, tooltips, dropdowns.
4️⃣ position: fixed;
👉 Stays fixed in the same place, even when scrolling.
📌 Always relative to the viewport.
.fixed-box {
position: fixed;
top: 0;
right: 0;
background: blue;
color: white;
padding: 10px;
}
✅ Use case: Sticky navbars, floating chat buttons, popups.
5️⃣ position: sticky;
👉 Acts like relative until the user scrolls past a certain point, then it becomes fixed.
📌 Requires top, left, right, or bottom to work.
.sticky-box {
position: sticky;
top: 20px; /* Sticks after scrolling past 20px */
background: yellow;
padding: 10px;
}
✅ Use case: Sticky headers, table columns, floating elements.
🔹 Comparison Table
| Position | Moves from Normal Flow? | Affects Nearby Elements? | Scroll Behavior |
|---|---|---|---|
static | ❌ No | ✅ Yes | Normal |
relative | ✅ Yes (relative to itself) | ✅ Yes | Normal |
absolute | ✅ Yes (relative to nearest positioned ancestor) | ❌ No | Moves with parent |
fixed | ✅ Yes (relative to viewport) | ❌ No | Stays fixed |
sticky | ✅ Yes (relative, then fixed) | ✅ Yes | Sticks on scroll |
🔹 Summary
✅ Static → Default, follows document flow
✅ Relative → Moves relative to itself, but keeps space
✅ Absolute → Moves relative to parent (or <html> if none)
✅ Fixed → Doesn’t move when scrolling (relative to viewport)
✅ Sticky → Sticks at a point while scrolling
