Sub topic 4
Placing an element with position-area
This chapter assumes you know position: absolute, inset properties such as top and left, and how an element's containing block is chosen. You should also know how to declare an anchor with anchor-name and link an element to it with position-anchor.
The idea: a 3x3 grid around the anchor
The anchor() function places each edge with a separate inset value. position-area is shorter. The browser draws a grid of nine cells around the anchor, and you name the cells where the element may sit.
The anchor is the center cell. The grid's outer edges are the edges of the anchored element's containing block. The other eight cells fill the space around the anchor.
| Row | left column | center column | right column |
|---|---|---|---|
| **top row** | `top left` | `top` | `top right` |
| **middle row** | `left` | `center` | `right` |
| **bottom row** | `bottom left` | `bottom` | `bottom right` |
The chosen cell or cells become the element's containing block. The element is then laid out inside that block, so you don't write any top, left, or translate values.
A tooltip with one declaration
Compare this with the tooltip from the anchor() chapter. Three properties replace the inset and translate values:
.trigger {
anchor-name: --tip;
}
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
}top selects the top-center cell. The element sits directly above the anchor and is centered horizontally. The margin-bottom adds a gap, and it still works because margins apply inside the selected cells.
Choosing more than one cell
You can combine keywords to pick a corner or span several cells.
- Two physical keywords select a cell.
top leftis the top-left corner.bottom rightis the bottom-right corner. - A
span-keyword extends the area.bottom span-rightcovers the bottom-center and bottom-right cells. The element starts at the anchor's left edge and can grow to the right. span-allcovers a whole row or column.top span-allcovers all three cells of the top row.
A dropdown that opens below its button and aligns to the button's left edge looks like this:
.dropdown {
position: absolute;
position-anchor: --menu-btn;
position-area: bottom span-right;
min-width: anchor-size(width);
}The min-width line uses anchor-size(), which a later chapter covers. It keeps the menu at least as wide as the button.
Logical keywords such as block-start and inline-end also work. They follow the writing mode, so the same rule is correct in right-to-left layouts.
How the element is aligned inside the area
The element's size is up to you. Its position inside the area comes from default alignment rules:
- With a single-cell keyword such as
top, the element is centered along the axis where the cell is flush with the anchor. - With a
span-keyword, the element is aligned to the anchor's edge. Forbottom span-right, that is the left edge.
You can override this with justify-self and align-self when the default isn't what you want.
Common errors
- Nothing moves. The element needs
position: absoluteorposition: fixed, and it needsposition-anchorunless it has an implicit anchor, such as a popover invoked withpopovertargetwhen the browser supports that. Check both first. - The element lands in the wrong place when mixing methods.
position-areasets the containing block, so explicit insets such astop: 20pxare then measured against the selected cells and not the page. Use one method per element unless you have a specific reason. - A popover keeps its default placement. The browser gives popovers
insetandmargindefaults. Reset them withinset: autoandmargin: 0before relying onposition-area. - The gap is missing. Anchor positioning doesn't add space between the anchor and the element. Add a margin on the side that faces the anchor.
Exercise
A menu should open directly below its button, aligned to the button's left edge, and be allowed to extend to the right. Which position-area value does this, and which cells does it select?
Show answer
position-area: bottom span-right;. It selects the bottom-center cell, which is directly below the anchor, and the bottom-right cell. The element starts at the anchor's left edge and can extend to the right.