🎯 top, right, bottom, left β€” Mastering CSS Offsets

🌐 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.

>>β€œOffset properties tell the browser *how far* an element should move from its reference edge.”

Note

⚠️ These properties ONLY work when position = relative, absolute, fixed, or sticky.
---

πŸ“Œ The Four Offsets

PropertyWhat It Means
topDistance from the top edge of the containing block
rightDistance from the right edge
bottomDistance from the bottom edge
leftDistance 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

βœ” Original space is preserved β€” layout does NOT shift.
---

πŸ”§ 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

⚠️ Without a positioned ancestor, it positions relative to viewport (html).
---

πŸ”§ 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

βœ” Sticky requires a scrollable parent with enough height.
---

πŸ“ How Offsets Behave Together

For absolute and fixed positioning:

Offsets SetBehavior
top + leftPositions from top-left corner
top + rightPositions from top-right corner
bottom + leftBottom-left corner
bottom + rightBottom-right corner
all fourSize 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

🧠 Always combine offsets with the correct positioning type.

πŸ”₯ 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.

>>β€œOffsets are simple β€” but they unlock some of the most advanced layout techniques in CSS.”

Learn more β†’MDN Docs πŸ“˜