π What Are top, right, bottom, left?
The CSS properties top, right, bottom, andleftβoften called offset propertiesβare used to position elements when their position is set to anything other than static.
Note
π The Four Offsets
| Property | What It Means |
|---|---|
| top | Distance from the top edge of the containing block |
| right | Distance from the right edge |
| bottom | Distance from the bottom edge |
| left | Distance from the left edge |
Values can be px, %, rem, vh, etc..
---π§ 1οΈβ£ Offsets with position: relative
The element stays in the flow, but moves visually relative to its original position.
relative + offsets
.box {
position: relative;
top: 20px; /* moves down */
left: 30px; /* moves right */
}Note
π§ 2οΈβ£ Offsets with position: absolute
Element is removed from flow and positioned relative to thenearest ancestor with position β static.
absolute + offsets
.parent {
position: relative; /* positioning context */
}
.child {
position: absolute;
top: 10px;
right: 20px;
}The child sits 10px from the top and 20px from the right of its parent.
Note
π§ 3οΈβ£ Offsets with position: fixed
The element stays fixed to the viewport β even on scroll.
fixed + offsets
.button {
position: fixed;
bottom: 30px;
right: 30px;
}Perfect for floating action buttons, chat icons, sticky banners, etc.
---π§ 4οΈβ£ Offsets with position: sticky
Sticky elements move normally until a scroll threshold is reached. Then offsets define where they βstick.β
sticky + offsets
.header {
position: sticky;
top: 0;
}Note
π How Offsets Behave Together
For absolute and fixed positioning:
| Offsets Set | Behavior |
|---|---|
| top + left | Positions from top-left corner |
| top + right | Positions from top-right corner |
| bottom + left | Bottom-left corner |
| bottom + right | Bottom-right corner |
| all four | Size is constrained between those offsets |
π§± Example β Centering with Offsets
Absolute Centering Trick
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}A classic trick to place elements perfectly centered.
---π§ͺ Real-World Examples
1οΈβ£ Tooltip using absolute + offsets
Tooltip Example
.parent {
position: relative;
}
.tooltip {
position: absolute;
bottom: 100%;
left: 0;
padding: 6px;
background: black;
color: white;
}2οΈβ£ Sticky header
Sticky Header
header {
position: sticky;
top: 0;
background: white;
}3οΈβ£ Floating button
Floating Button
.fab {
position: fixed;
bottom: 20px;
right: 20px;
background: #ff4081;
padding: 15px;
border-radius: 50%;
}π§ Special Note on Percent Values
Percentages in offsets behave differently:
- relative: % is calculated from the element itself
- absolute / fixed: % is calculated from the containing block
β οΈ Common Mistakes
- Using offsets without setting position β NO EFFECT
- Forgetting to set position on parent for absolute children
- Expecting fixed to respect parent space (it never does)
- Using sticky without scrollable space
- Using % incorrectly for vertical offsets
Note
π₯ Summary
The top, right, bottom, and leftproperties give you precise control over element placement. Once you master how they interact with the different position values, you unlock powerful layout behaviors like floating buttons, sticky headers, tooltips, and complex UI overlays.
Learn more βMDN Docs π