Sub topic 1
Before anchors: absolute positioning and containing blocks
CSS anchor positioning replaces a set of workarounds that front-end developers have used for years. To see what it changes, you need an exact picture of how absolute positioning works today. This chapter covers that picture: what an absolutely positioned element is measured against, how a tooltip is built without anchors, and where that approach breaks.
Prerequisites
This chapter assumes you already know the following:
- Basic CSS selectors and the cascade, including how one rule overrides another.
- The
positionproperty (static,relative,absolute,fixed) and the inset propertiestop,right,bottom, andleft. - The box model: content, padding, border, and margin.
- Basic HTML. You do not need to know the
popoverattribute yet. A later chapter introduces it.
You do not need any JavaScript for this chapter.
What absolute positioning does
An element with position: absolute is taken out of normal flow. Siblings behave as if it were not there, so it takes up no space. Its position comes from its inset properties, and those values are measured against its containing block.
The containing block is a rectangle. For an absolutely positioned element it is the padding box of the nearest ancestor that qualifies, and the browser finds it by walking up the tree. Percentages in top, left, right, and bottom resolve against that rectangle. The element's own size does not affect them.
An ancestor qualifies in two ways:
- Its
positionis anything other thanstatic. This meansrelative,absolute,fixed, orsticky. - It creates a containing block for other reasons. Common causes are a non-
nonetransform,translate,rotate, orscale, afilter, aperspective, acontainvalue oflayoutorpaint, orwill-changenaming one of these properties.
If nothing qualifies, the element uses the initial containing block. This rectangle is the size of the viewport and sits at the top of the page. It scrolls away with the page.
Inset properties against the containing block
Each inset property moves one edge of the element relative to the matching edge of the containing block.
| Declaration | Meaning |
|---|---|
| `top: 0` | The element's top edge touches the containing block's top edge |
| `top: 100%` | The element's top edge sits at the containing block's bottom edge, so the element hangs below it |
| `bottom: 100%` | The element's bottom edge sits at the containing block's top edge, so the element sits above it |
| `left: 50%` | The element's left edge is at the horizontal midpoint of the containing block |
| `right: 0` | The element's right edge touches the containing block's right edge |
The bottom: 100% case surprises many people. bottom measures up from the bottom edge of the containing block, so 100% pushes the element's bottom edge all the way to the top.
When all insets are auto
If every inset is auto, the element stays at its static position. This is the spot it would occupy if it were still in normal flow. The browser calculates it, but you cannot read it from CSS. This detail matters later, because the fallback behavior of anchor positioning builds on the idea that the element has a resting position.
The classic tooltip without anchors
Suppose you want a tooltip centered above a button. The tooltip needs the button's box as its containing block, so the traditional solution adds a wrapper.
<span class="tip-wrap">
<button class="tip-trigger" aria-describedby="save-tip">Save</button>
<span class="tip" id="save-tip" role="tooltip">Saves your draft</span>
</span>.tip-wrap {
position: relative;
display: inline-block;
}
.tip {
position: absolute;
bottom: 100%;
left: 50%;
translate: -50% 0;
margin-bottom: 8px;
width: max-content;
max-width: 16rem;
}Each line has a job:
.tip-wrapisposition: relative, so it becomes the containing block. Itsinline-blockdisplay makes its box fit the button instead of stretching across the line.bottom: 100%puts the tooltip's bottom edge at the wrapper's top edge.left: 50%puts the tooltip's left edge at the wrapper's center. The tooltip would then hang to the right of center, sotranslate: -50% 0shifts it left by half of its own width. Percentages intranslaterefer to the element's own size, which is the piece that inset percentages cannot supply.margin-bottom: 8pxleaves a gap between the button and the tooltip.width: max-contentprevents a common problem. An absolutely positioned element withleft: 50%has a shrink-to-fit width limited to the space between its left edge and the containing block's right edge. Withoutmax-content, a short button gives a very narrow tooltip whose text wraps on every word.
This works, and it is reliable CSS. The next section lists its costs.
Where this approach breaks down
The wrapper pattern ties the tooltip's position to the tree structure. Several problems follow from that.
The element must live inside the thing it points to
The tooltip has to be a descendant of the containing block, which means a descendant of the wrapper. You cannot place it elsewhere in the DOM, such as at the end of body, and still measure against the button. This conflicts with how menus, dialogs, and popovers are often structured.
Clipping by ancestors
An ancestor with overflow: hidden, auto, or scroll clips any descendant whose containing block is that ancestor or something inside it. A tooltip inside a scrolling table cell or a card with overflow: hidden gets cut off at the edge.
An absolutely positioned element is not clipped by an overflow ancestor that sits between it and its containing block. If the containing block is above the clipping ancestor, the element escapes the clip. That is rarely the case for the wrapper pattern, because the wrapper is usually inside the clipping container.
.card {
overflow: hidden; /* clips .tip when .tip-wrap is inside .card */
}Transforms silently change the containing block
If a component sets transform or filter on an ancestor, for example for an animation, that ancestor becomes the containing block for absolute and fixed descendants. A tooltip that used to be placed against the page may suddenly be placed against that ancestor. This bug is hard to spot, because nothing in the tooltip's own CSS changed.
.panel {
transform: translateZ(0); /* now .panel is the containing block, even for position: fixed children */
}Stacking and the top layer
A tooltip competes with other content through z-index and stacking contexts. A high z-index inside a low-priority stacking context still loses to later siblings of that context. Elements shown through the Popover API or <dialog>.showModal() go into the top layer. The top layer renders above everything else, regardless of z-index. A top-layer element is not positioned against its DOM parent in the usual way, so the wrapper pattern cannot work for popovers at all.
Placement depends on the viewport, and CSS cannot see it
The pattern above always places the tooltip above the button. If the button sits at the top of the viewport, the tooltip goes off screen. Plain CSS has no way to say "use this placement unless it overflows, then use another". The next section shows how developers have handled that.
The JavaScript workaround
Because CSS could not express these needs, most real projects measure positions in script. A typical version looks like this:
function placeTooltip(trigger, tooltip) {
const t = trigger.getBoundingClientRect();
const w = tooltip.offsetWidth;
const h = tooltip.offsetHeight;
let top = t.top - h - 8;
if (top < 0) {
top = t.bottom + 8; // not enough room above, flip below
}
const left = t.left + t.width / 2 - w / 2;
tooltip.style.position = "fixed";
tooltip.style.top = `${top}px`;
tooltip.style.left = `${left}px`;
}This handles the flip, but it brings new work:
- You must call it on show, on window resize, and on scroll of every scrollable ancestor.
- It reads layout (
getBoundingClientRect) and then writes styles, which can force extra layout calculations. - Between a scroll event and your handler running, the tooltip can lag a frame behind its anchor.
- It breaks when the page is rendered before JavaScript loads.
Libraries such as Floating UI and Popper package this logic and are widely used. They also have to handle every case above.
What anchor positioning changes
The rest of this tutorial covers the replacement. At a high level, it fixes the weaknesses above in these ways:
- The anchor can be any element on the page, regardless of where the positioned element lives in the DOM. The wrapper is no longer required.
- The positioned element describes its placement in CSS terms, using the anchor's edges and center instead of the wrapper's.
- The browser knows the anchor's position at layout time, so scrolling stays in sync without script.
- You can list fallback placements, and the browser picks one that fits the viewport.
Quick checklist before you move on
Before starting the next chapter, make sure you can answer these from memory:
- Which rectangle do the inset percentages of an absolutely positioned element resolve against?
- Which CSS properties, besides
position, can make an ancestor the containing block? - Why does
left: 50%need atranslateto center an element? - Why can an
overflow: hiddencard clip a tooltip, and when can it not?
Exercise
A .menu is position: absolute; top: 100%; left: 0; inside .wrap, which has no position set. .wrap sits inside .page, which has position: relative and is 600px tall. Where does the top edge of .menu land, and what changes if you add transform: scale(1) to .wrap?
Show answer
.wrap is static and has no transform, so it is skipped. .page is the nearest qualifying ancestor, so its padding box is the containing block. top: 100% puts the menu's top edge at the bottom of .page, 600px below .page's top edge, no matter how small .wrap is. Adding transform: scale(1) makes .wrap a containing block even though the transform changes nothing visually. The menu's top edge then sits at the bottom of .wrap instead.
Exercise
A tooltip uses bottom: 100%; left: 50%; translate: -50% 0; inside a relative wrapper, and its text wraps onto many short lines when the trigger is a small icon. Which declaration fixes this, and why?
Show answer
Add width: max-content (with a max-width as a cap). With left: 50%, the tooltip's shrink-to-fit width is limited to the space between its left edge and the right edge of the containing block. For a narrow trigger that space is tiny. max-content sizes the tooltip from its text instead.