Lesson 30 / الدرس 30

position & z-index / خاصيتا position و z-index

static, relative, absolute, fixed and sticky — what each one positions an element against, and how z-index decides which one sits on top when they overlap.

static و relative و absolute و fixed و sticky — بالنسبة لماذا يتموضع كل عنصر، وكيف تقرر z-index أيها يجلس فوق الآخر عند التداخل.

By default every element sits exactly where the normal flow puts it, the same flow you have used since the display lesson. The position property lets you pull an element out of that flow and place it deliberately, using four offset properties — top, right, bottom and left — that only start working once you give position a value other than the default. There are five values to know: static, relative, absolute, fixed and sticky.

static and relative: the default, and a small nudge

static is the default value every element already has — top, right, bottom and left are simply ignored on it. relative is the smallest step away from that: the element still takes its normal spot in the flow, reserving exactly the same space it always would, and the offsets then nudge it visually from that spot — without ever affecting where its neighbors sit.

.box2 {
    position: relative;
    top: 20px;
    left: 20px;
}
Three boxes in a row. The middle one is nudged down and to the right with position: relative, but its original spot stays reserved — exactly like visibility: hidden from the last lesson, nothing else moves to fill the gap.

absolute: positioned against an ancestor

position: absolute is a bigger step: the element leaves the normal flow completely, the same way display: none does — its neighbors close the gap it would have left, as if it was never there. It is then positioned using top, right, bottom and left against its nearest ancestor that has a position other than static — usually one you deliberately set to relative for exactly this reason. If no ancestor qualifies, it positions against the page itself.

.card {
    position: relative;
}

.badge {
    position: absolute;
    top: 8px;
    right: 8px;
}
A classic pattern: a "New" badge pinned to the corner of a card. The card gets position: relative so it becomes the reference point, and the badge gets position: absolute with top and right to sit exactly in its corner. Be deliberate about which corner: left and right are physical offsets and do not follow the page direction the way text-align: start does. This badge stays in the top-right corner on a dir="rtl" page too — which is the far corner from where an Arabic reader starts reading, and probably not what you wanted.

fixed and sticky: pinned to the viewport, or almost

.header {
    position: sticky;
    top: 0;
}
position: fixed pins an element to the browser's viewport, ignoring scrolling entirely — a back-to-top button or a fixed header. position: sticky is the hybrid: it behaves like relative until the page scrolls to the point given by top, then it locks in place like fixed, but only inside its own parent. Scroll inside the box below and watch the heading stick to the top the moment it reaches it.

z-index: deciding who sits on top

.front {
    position: absolute;
    z-index: 2;
}
When positioned boxes overlap, z-index decides which one draws on top — a higher number wins. It only has an effect on an element whose position is something other than static. Swap the two numbers below and watch the stacking order flip.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. An element has position: relative but no top, right, bottom or left set. Where does it appear?

2. What does position: absolute position an element against?

3. Two positioned boxes overlap. One has z-index: 1 and the other has z-index: 5. Which one is drawn on top?

Your task / مهمتك

Build a small profile card: give the card position: relative, then pin a status badge to its top-right corner with position: absolute, top and right.

ابنِ بطاقة تعريف صغيرة: أعطِ البطاقة position: relative، ثم ثبّت شارة حالة في زاويتها العلوية اليمنى باستخدام position: absolute و top و right.

  • The card has position: relative البطاقة تملك position: relative
  • The badge has position: absolute with top and right set الشارة تملك position: absolute مع تحديد top و right
  • The badge sits inside the card's corner, not the far corner of the page الشارة تجلس داخل زاوية البطاقة، لا في زاوية الصفحة البعيدة
How do you want to submit? / كيف تريد التسليم؟