Sub topic 11
Building badges, sticky headers, and modal overlays
This chapter combines the pieces from earlier chapters into three common patterns. It assumes you know the box model, CSS selectors, and the basic position values. You should also know what a containing block is and how z-index orders overlapping boxes.
The pieces you need
Each pattern relies on one positioning value and one idea about where the element is anchored.
| Pattern | Position value | Anchored to |
|---|---|---|
| Corner badge | `absolute` | Nearest positioned ancestor |
| Sticky header | `sticky` | Its scroll container and parent |
| Modal and backdrop | `fixed` | The viewport |
Corner badge
A badge sits in the corner of a card. Give the card position: relative so it becomes the containing block. Then give the badge position: absolute and 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>The card has no offsets, so it stays where it was in the flow. Its only job is to anchor the badge. Without it, the badge would use the next positioned ancestor, or the page itself if none exists, and would appear in the corner of the viewport area instead.
Sticky header
A sticky header scrolls with the page until it reaches the offset you set. Then it stays there.
.site-header {
position: sticky;
top: 0;
background: white;
}The top: 0 value is required. Without an offset, sticky has nothing to stick to, and the header behaves like a normal element. The background matters too. Without it, the page content shows through the header as it scrolls underneath.
Modal overlay
A modal has two parts. The backdrop dims the page, and the modal sits above it.
.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;
}The inset: 0 shorthand sets top, right, bottom, and left to zero, so the backdrop covers the whole viewport. For the modal, top: 50% and left: 50% move its top-left corner to the center. The translate(-50%, -50%) then shifts it back by half its own width and height, which centers the box itself.
The modal has a higher z-index than the backdrop, so it is painted on top.
Controlling overlap with z-index
Positioned elements can overlap, and z-index decides which one is painted on top.
<div class="a">A</div>
<div class="b">B</div>
<style>
.a, .b {
position: absolute;
width: 100px;
height: 100px;
}
.a { top: 0; left: 0; background: tomato; z-index: 2; }
.b { top: 50px; left: 50px; background: steelblue; z-index: 1; }
</style>The two boxes overlap, and box A is drawn over box B because its z-index is higher. Swap the values and the order reverses.
Common mistakes
- Badge in the wrong place. The parent is missing
position: relative. Add it to the intended anchor. - Sticky header does not stick. The
topoffset is missing, or an ancestor hasoverflow: hidden,auto, orscroll. That ancestor becomes the scroll container, and the header sticks to it instead of the page. - Modal appears under other content. An ancestor with its own stacking context may be capping the modal's
z-index. Move the modal higher in the HTML, or place it directly inbody. z-indexhas no effect. The element isposition: static. Give it a position value.
Exercise
A sticky header with position: sticky; top: 0; scrolls away with the page. Its parent is a div with overflow: hidden. What is the likely cause, and what is one fix?
Show answer
The overflow: hidden parent becomes the scroll container, and since it does not scroll itself, the header has nothing to stick against. Remove the overflow from that ancestor, or move the header outside it.
Exercise
You set position: absolute; top: 0; right: 0; on a badge, but it appears at the top-right of the page instead of the card. What one change fixes it?
Show answer
Add position: relative to the card, so the card becomes the badge's containing block.