A "New" badge (a small label) in the corner of a product card looks like a minor detail. But if one class is missing, the badge flies to the corner of the page, far from its card.
The CSS property position decides where the badge goes. Position sets whether an element follows the normal flow (elements placed one after another), or leaves the flow and is pinned to a certain spot. When two elements overlap, z-index decides which one shows in front. In Tailwind, each value is one class: relative, absolute, sticky, top-0, z-50. For example, the class relative generates position: relative;.
Think of a "New" sticker on a cake box. You say "measure from this box" (relative on the box), then "stick it in the top-right corner" (absolute top-0 right-0 on the sticker). If the box isn't marked, the sticker is measured from the table, and it sticks in the table's corner. In the code, the cake box is the card (the parent), the sticker is the badge (the child), and the table is the page.
A badge in the card's corner
This is the same card as in the Lab and the exercise:
<div id="parent" class="relative h-32 bg-gray-100">
Product card
<div id="badge" class="absolute top-0 right-0 bg-rose-500 text-white">New</div>
</div>
| Class | CSS it generates | Meaning |
|---|---|---|
relative |
position: relative; |
the card becomes the reference (the point things are measured from) for its children |
h-32 |
height: 8rem; |
the card is 128px tall |
absolute |
position: absolute; |
the badge leaves the flow and is pinned |
top-0 |
top: 0; |
0 from the reference's top edge |
right-0 |
right: 0; |
0 from the reference's right edge |
relativewith notoporleftdoesn't move the card at all. Its only job is to be the reference.absolutetakes the badge out of the flow, so the "Product card" text no longer makes room for the badge.top-0 right-0pins the badge to the top-right corner. The numbers follow Tailwind's scale:top-2= 8px from the top edge, and-top-2(with a minus) = 8px out past the top edge.
You get the "New" badge exactly in the card's top-right corner.
The reference: a relative parent
An absolute element is measured from its nearest parent whose position isn't static (the default). If #parent has no relative, no parent has a position other than static, so the badge is measured from the page. The badge flies to the page's top-right corner, not the card's.
The pattern always comes in pairs: relative on the parent, absolute on the child. An overlay (a layer that covers the whole card) uses the same pattern, with absolute inset-0 on the overlay (inset-0 = top, right, bottom and left all 0).
A navbar that sticks: sticky and fixed
This navbar (the menu bar at the top of a page) uses sticky:
<header class="sticky top-0 z-50 w-full bg-white">Navbar</header>
| Class | While the page scrolls |
|---|---|
static (default) |
follows the flow, scrolls up out of view |
sticky top-0 |
follows the flow until it touches the top of the screen, then sticks there |
fixed top-0 |
out of the flow from the start and pinned to the screen, it never scrolls out of view |
stickyneeds a limit, heretop-0: the navbar sticks when its distance to the top of the screen reaches 0. With no limit, astickynavbar never sticks.fixedleaves the flow from the start. So the content below it moves up into the navbar's place, and the first line of that content is covered by the navbar.stickykeeps its place in the flow.- So the difference:
fixedalways stays in place,stickyonly sticks after the page scrolls to its limit.
Who's in front: z-*
z-10 generates z-index: 10, and z-50 generates z-index: 50. The bigger number shows in front. The default scale is z-0, z-10, z-20, z-30, z-40, z-50, and z-auto. In Tailwind v4, other numbers work too, such as z-60.
In the Lab, the card is relative and written after the navbar. When the page scrolls, that card passes in front of the sticky navbar. The reason: neither has a z-index yet, so the element written later in the HTML shows in front. Add z-50 to the navbar, and the navbar shows in front of the card again.
⚠️ When things stick in the wrong place
- Forgetting
relativeon the parent. Theabsolutebadge flies to the page's corner. The exercise checks forposition: relativeon#parentandposition: absoluteon#badge. absolutewith no direction. Withouttop-0 right-0, anabsolutebadge stays where it was in the flow, on the card's left side, not in the top-right corner.stickywithouttop-0. The navbar scrolls out of view likestatic, because it has no limit to stick at.- A parent with
overflow-hiddenoroverflow-x-auto. People often add these classes to stop the page from scrolling sideways. As a result,stickystops working with no message: the navbar scrolls out of view. Useoverflow-x-clipinstead. It cuts off content that spills out sideways, so the page still doesn't scroll sideways, andstickykeeps working. z-50on astaticelement. z-index only works on elements whosepositionisn'tstatic, or on flex and grid items (direct children of a flex or grid element). For other elements, pairz-50withrelative,absolute,stickyorfixed.
TL;DR: In Tailwind, each position value is one class, for example relative generates position: relative;. relative on the parent + absolute top-0 right-0 on the child = the child sticks to its parent's top-right corner. The relative parent is the child's reference (the point it is measured from). Without relative, the reference is the page. sticky top-0 follows the page's flow, then sticks to the top of the screen as you scroll. fixed is pinned to the screen from the start and leaves the flow. z-50 = z-index: 50: the bigger number shows in front. z-index only works on elements with a position other than static (the default), or on flex and grid items (direct children of a flex or grid element).
⚡ A badge in the card's corner
Write:
<div id="parent" class="relative h-32 bg-gray-100">
Product card
<div id="badge" class="absolute top-0 right-0 bg-rose-500 text-white">New</div>
</div>
Guess first: absolute is measured from the nearest parent whose position isn't static. Remove relative from #parent, and where does the badge go?
The badge flies to the top-right corner of the page. Without a relative parent, the badge is measured from the page.
Meaning: The class relative (position: relative;) makes the parent #parent the reference, the point its children are measured from. Then absolute top-0 right-0 on the child #badge sticks the badge to that parent's top-right corner.
⚡ Sticking as you scroll
Write:
<header class="sticky top-0 z-50 w-full bg-white">Navbar</header>
Guess first: sticky sticks when its distance to the top of the screen reaches the limit top-0. Without top-0, does the navbar stick?
No. The navbar scrolls out of view like static, because it has no limit to stick at.
Meaning: sticky top-0 follows the page's flow first, then sticks once it touches the top of the screen. fixed is pinned to the screen from the start and leaves the flow, so the navbar can cover the content below it.
⚡ Who's in front
Meaning: z-50 = z-index: 50, and the bigger number shows in front. z-index only works on elements whose position isn't static, or on flex and grid items (direct children of a flex or grid element).