Sub topic 11
Building badges, sticky headers, and modal overlays
This chapter combines the positioning rules from earlier chapters into three patterns you will build often: a corner badge, a sticky header, and a modal overlay. Each pattern uses a different position value, and each fails in a predictable way.
It assumes you know the following:
- Basic HTML structure and CSS selectors.
- The box model.
- The units
px,%, andrem. - The earlier chapters on containing blocks,
absolute,fixed,sticky, andz-index.
Choosing the value for each pattern
Start from what the element should do, then pick the value.
| Pattern | Position value | What anchors it | Leaves the flow? |
|---|---|---|---|
| Corner badge | `absolute` | Nearest positioned ancestor | Yes |
| Sticky header | `sticky` | Its scroll container and its parent | No |
| Modal and backdrop | `fixed` | The viewport | Yes |
Pattern 1: the corner badge
A badge is a small label pinned to a corner of a card. It needs two rules. The card gets position: relative, so it becomes the containing block. The badge gets position: absolute with offsets.
<div class="card">
<span class="badge">New</span>
<p>Card content</p>
</div>
<style>
.card {
position: relative;
padding: 1rem;
border: 1px solid #ccc;
}
.badge {
position: absolute;
top: 0;
right: 0;
}
</style>position: relative on the card with no offsets moves nothing. Its only job here is to be the nearest positioned ancestor. Without it, the badge would look further up the tree. It would end up in the corner of the page, or of whatever positioned ancestor it found first.
Because the badge leaves the flow, the card reserves no space for it. Text can run underneath it, so give the card enough padding on that side.
Making the badge overhang the corner
Many designs place the badge's center on the card's corner. Offsets alone cannot do this, because they move the badge's own edge. Shift the badge back by half its own size with transform:
.badge {
position: absolute;
top: 0;
right: 0;
transform: translate(50%, -50%);
}Percentages in translate() refer to the element's own box. This works for any badge size.
Badge edge cases
- Clipping: An overhanging badge is cut off if the card or any ancestor up to its containing block has
overflow: hidden. Move the badge inside, or move the overflow rule to an inner wrapper. - Right-to-left text:
right: 0stays on the physical right. Useinset-inline-end: 0if the badge should follow the writing direction. - Inline parents: A
<span>or<a>card works as a containing block, but its box can wrap across lines. Give itdisplay: inline-blockordisplay: blockto get one predictable box. - Clicks: A badge sits above sibling content that comes earlier in the source. If it blocks a link underneath, add
pointer-events: noneto the badge when it is purely decorative.
Pattern 2: the sticky header
A sticky element behaves as relative until its scroll position reaches a threshold. Then it behaves like fixed until the end of its parent. It stays in the flow the whole time, so the page does not jump when it activates.
.site-header {
position: sticky;
top: 0;
background: white;
}Three details matter in real pages.
- The threshold is required.
position: stickywith notop,bottom,left, orrightnever sticks. The offset says how far from the scroll container's edge the element should stop. - A sticky element cannot leave its parent. It sticks only while its parent box is in view. A header placed inside a short wrapper disappears when the wrapper scrolls away. For a site-wide header, make it a direct child of
<body>, or of a wrapper that spans the whole page. - The scroll container is the nearest ancestor that scrolls. If any ancestor has
overflow: hidden,auto, orscroll, that ancestor becomes the scroll container, even if it does not actually scroll. The header then sticks to that ancestor, not the viewport. This is the most common reason sticky "does nothing".
Keeping the header on top
A sticky header stays in the flow, but later content can still paint over it. Positioned elements further down the page, such as badges, paint after non-positioned content and can slide over the header. Give the header a z-index:
.site-header {
position: sticky;
top: 0;
z-index: 10;
background: white;
}The background matters too. Without it, the header is transparent and content shows through as it scrolls underneath.
Anchor links under a sticky header
When a user follows a link such as #pricing, the browser scrolls the target to the very top, which puts it behind the header. Reserve space with scroll-margin-top on the targets:
section[id] {
scroll-margin-top: 4rem; /* roughly the header height */
}You can also set scroll-padding-top once on the scrolling element, usually html.
Pattern 3: the modal overlay
A modal has two layers. The backdrop covers the screen and dims the page. The dialog sits above the backdrop. Both use position: fixed, so they ignore scrolling.
.backdrop {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 100;
}
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 101;
}inset: 0 is shorthand for top, right, bottom, and left all set to 0. A fixed box with all four edges pinned stretches to fill its containing block.
The modal centers in two steps. top: 50%; left: 50% puts its top-left corner at the middle of the viewport. translate(-50%, -50%) then pulls it back by half its own width and height, so its center lands on the viewport center.
Making the modal safe for long content
The centering above breaks when the modal is taller than the viewport. Part of it ends up off-screen, and the user cannot scroll to reach it. Cap the size and let the modal scroll internally:
.modal {
max-width: min(32rem, 90vw);
max-height: 90vh;
overflow: auto;
}Modal edge cases
- A transformed ancestor changes what fixed means. If any ancestor has
transform,filter, orperspectiveset, afixeddescendant is positioned against that ancestor, not the viewport. The overlay then covers only part of the page. Put the modal markup as a direct child of<body>, near the end. - Stacking contexts cap z-index. A
z-index: 101inside a parent that forms its own stacking context cannot rise above siblings of that parent. Placing the modal at the root avoids this. - The page behind can still scroll. A backdrop does not block scrolling. A common fix is to set
overflow: hiddenonbodywhile the modal is open. - Positioning does not make an accessible dialog. CSS paints the modal, but it does not move keyboard focus, trap Tab, or close on Escape. The native
<dialog>element opened withshowModal()handles these, and it renders in the browser's top layer. In that case you do not needz-indexat all. Use the fixed technique when you need to understand or maintain existing code, or when you control all the behavior yourself.
Putting the three layers in order
The three patterns share a page, so their z-index values must agree. Use a small, documented scale instead of arbitrary numbers:
| Layer | `z-index` |
|---|---|
| Page content and badges | `auto` or `1` |
| Sticky header | `10` |
| Backdrop | `100` |
| Modal | `101` |
Gaps between values leave room to add layers later. Custom properties such as --z-header: 10 keep the scale in one place.
A complete page
This page runs as written. It shows a sticky header, a card with a corner badge, and a modal shown permanently so you can inspect it. Scroll to see the header stick.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Positioning patterns</title>
<style>
body { margin: 0; font-family: sans-serif; }
.site-header {
position: sticky;
top: 0;
z-index: 10;
padding: 1rem;
background: white;
border-bottom: 1px solid #ccc;
}
.card {
position: relative;
margin: 2rem auto;
padding: 1.5rem;
max-width: 24rem;
border: 1px solid #ccc;
}
.badge {
position: absolute;
top: 0;
right: 0;
transform: translate(50%, -50%);
padding: 0.25rem 0.5rem;
background: tomato;
color: white;
}
.backdrop {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 100;
}
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 101;
max-width: min(32rem, 90vw);
max-height: 90vh;
overflow: auto;
padding: 1.5rem;
background: white;
}
</style>
</head>
<body>
<header class="site-header">My site</header>
<div class="card"><span class="badge">New</span><p>Card one</p></div>
<div class="card"><span class="badge">Sale</span><p>Card two</p></div>
<div class="card"><p>Card three</p></div>
<div class="backdrop"></div>
<div class="modal" role="dialog" aria-modal="true">
<p>Modal content, centered over a dimmed page.</p>
</div>
</body>
</html>To see the sticky header and badges clearly, delete the backdrop and modal elements and add enough cards to make the page scroll.
Debugging checklist
- Badge in the wrong place: check that its parent has a non-
staticposition. - Badge cut off: look for
overflow: hiddenbetween the badge and its containing block. - Sticky does nothing: check for a missing offset, an
overflowancestor, or a parent that is only as tall as the header. - Modal covers only part of the page: look for
transformorfilteron an ancestor. z-indexseems ignored: check that the element is positioned, and look for a stacking context on a parent.
Exercise
A sticky header with position: sticky; top: 0 works on one page but does nothing on another. The second page wraps all content in <main style="overflow-x: hidden">, and the header is inside <main>. What is the likely cause, and how would you fix it?
Show answer
The overflow-x: hidden on <main> makes <main> the header's scroll container. The header sticks relative to <main>, which does not scroll vertically, so it never appears to stick. Fix it by removing the overflow rule, or by moving the header outside <main> so that its nearest scrolling ancestor is the viewport. overflow-x: clip is another option, because clip does not create a scroll container.
Exercise
You want a "Sale" badge whose center sits exactly on the top-right corner of a card. Which properties go on the card, and which on the badge?
Show answer
The card needs position: relative so it becomes the containing block. The badge needs position: absolute; top: 0; right: 0; transform: translate(50%, -50%);. The translate percentages refer to the badge's own size, so the badge's center lands on the corner for any badge size.