Sub topic 1
Before anchors: absolute positioning and containing blocks
Prerequisites
This chapter assumes you know basic CSS selectors and the cascade, the position property values (static, relative, absolute, fixed), and the inset properties top, right, bottom, and left. You should also know the box model. Knowing HTML is enough. You don't need the popover attribute yet.
Why start here
Anchor positioning builds on absolute positioning. Anchored elements are still absolutely positioned (or fixed). The only change is the reference box. Once you can predict what an absolutely positioned element is measured against, anchors will feel like a small step.
The containing block
An absolutely positioned element is taken out of normal flow. Its top, right, bottom, and left values are measured against its containing block. This is a rectangle defined by another element.
For position: absolute, the browser finds the containing block by walking up the ancestors. It picks the nearest ancestor that is a positioned element, meaning its position is anything other than static. Some other properties also create a containing block, such as transform. If no ancestor qualifies, the containing block is the initial containing block, which is roughly the viewport-sized area at the top of the page.
Here is a tooltip placed with the traditional method.
<div class="card">
<button class="trigger">Hover me</button>
<div class="tooltip">Saved!</div>
</div>.card {
position: relative;
width: 240px;
padding: 24px;
}
.tooltip {
position: absolute;
top: 100%;
left: 0;
margin-top: 8px;
}The .card is position: relative, so it becomes the containing block. top: 100% means 100% of the card's height, which places the tooltip's top edge at the card's bottom edge. If you remove position: relative from .card, the tooltip jumps to a position measured against the page, and that is a common source of confusion.
Where this approach breaks down
The containing block is always an ancestor. That creates real limits:
- The tooltip must be a descendant of the element it is measured against. Often that forces you to nest it inside a wrapper.
- Offsets are relative to the ancestor, not to the trigger. If the trigger sits in the middle of the card,
top: 100%lands below the card, not below the trigger. - To center a tooltip over a trigger, you need the trigger's size. Width and height are unknown in CSS, so people measure them with JavaScript.
- Ancestors with
overflow: hiddenclip the tooltip. Atransformon an ancestor changes the containing block and shifts everything. - When the tooltip would run off the screen, CSS has no way to detect that and move it.
The usual workaround is JavaScript. You call getBoundingClientRect() on the trigger, compute coordinates, write them as inline styles, and repeat on scroll and resize. Libraries such as Floating UI exist to handle these cases.
const rect = trigger.getBoundingClientRect();
tooltip.style.top = `${rect.bottom + window.scrollY + 8}px`;
tooltip.style.left = `${rect.left + window.scrollX}px`;This code is a snapshot. It goes stale when the page scrolls inside a container, when fonts load, or when the layout reflows.
What comes next
The next chapters let you declare any element as an anchor and tie an element to it with anchor-name and position-anchor. The browser then does the measuring, updates it on scroll and resize, and can even pick a different placement when space runs out.
Exercise
A .tooltip has position: absolute; top: 100%;. Its parent .wrapper has no position set, and its grandparent .card has position: relative. Against which element is top: 100% measured, and what happens if you add position: relative to .wrapper?
Show answer
It is measured against .card, because that is the nearest ancestor with a position other than static. If you add position: relative to .wrapper, then .wrapper becomes the containing block, and 100% now refers to the height of .wrapper.
Exercise
Give two reasons why placing a tooltip directly under a trigger with only top and left offsets from the containing block is fragile.
Show answer
Any two of: the offsets are measured from an ancestor, not from the trigger, so they break when the trigger moves; centering needs the trigger's size, which CSS cannot read; the tooltip must be nested inside the ancestor it is measured against; and nothing moves the tooltip when it would overflow the viewport.