Sub topic 4
Placing an element with position-area
This chapter assumes you can write CSS selectors, use position: absolute with inset properties, and know what a containing block is. You should also know how to declare an anchor with anchor-name and link an element to it with position-anchor. This chapter covers the second way to place an anchored element, the position-area property. You will learn how its grid works, how to read its keywords, and where it differs from anchor().
The idea: a 3x3 grid around the anchor
The anchor() function from the previous chapter gives you exact control. You pick an edge of the anchor and write it into top, left, bottom, or right. That is flexible but verbose. A tooltip centered above its anchor needed an anchor() call on two edges, a translate, and a margin.
position-area replaces that with a coarser model. The browser draws a grid of nine cells and you choose which cells your element occupies. The grid is built from two things:
- The anchor's box is the center cell.
- The edges of the anchor extend as lines across the element's containing block. These lines split it into three columns and three rows.
The outer cells stretch from the anchor's edges to the edges of the containing block. The element is placed inside the cells you select, so it stays against the anchor without any offset arithmetic.
The nine cells
You name a cell with one keyword for the row and one for the column. For example, top left is the top row and the left column. Physical names are the easiest to start with.
| Row \ Column | left | center | right |
|---|---|---|---|
| top | `top left` | `top center` | `top right` |
| center | `center left` | `center center` | `center right` |
| bottom | `bottom left` | `bottom center` | `bottom right` |
With physical keywords the order does not matter, so left top and top left are the same cell. You can also give a single keyword. Where the missing axis is not obvious, the browser fills it in so the element is centered on the anchor along that axis. That is why position-area: top puts a tooltip above the anchor and centered on it.
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
}This does the same job as the longer version with bottom: anchor(top), left: anchor(center), and translate: -50% 0. The element is placed in the top row, and its default alignment centers it on the anchor. The margin then adds a gap, because margins apply inside the selected area.
A complete page you can run:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>position-area demo</title>
<style>
body { min-height: 100vh; margin: 0; display: grid; place-items: center; }
.trigger { anchor-name: --tip; padding: 0.5rem 1rem; }
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
padding: 0.25rem 0.5rem;
background: #222;
color: #fff;
}
</style>
</head>
<body>
<button class="trigger">Hover target</button>
<div class="tooltip" role="tooltip">Saved</div>
</body>
</html>The body is not positioned, so the tooltip's containing block is the initial containing block, which is the viewport-sized area. The top row therefore runs from the top of the viewport down to the button's top edge.
Logical keywords
CSS also supports keywords that follow writing direction, which matter for right-to-left and vertical text. Use them when the layout should mirror with the language.
| Physical | Logical, using the element's containing block | Logical, relative to the element itself |
|---|---|---|
| `top` | `block-start` | `self-block-start` |
| `bottom` | `block-end` | `self-block-end` |
| `left` | `inline-start` | `self-inline-start` |
| `right` | `inline-end` | `self-inline-end` |
The generic keywords start and end also exist. They refer to the start and end of whichever axis the position in that slot belongs to. You cannot mix physical and logical keywords in one declaration. top inline-end is invalid, but block-start inline-end is fine.
Spanning several cells
One cell is often not enough. A dropdown should usually be wider than the anchor's own column, and a cell that matches the anchor's width may be too narrow for the content. The span keywords select two adjacent cells, or all three, along one axis.
| Keyword | Cells selected | Where the element's edges land |
|---|---|---|
| `span-left` | center and left columns | right edge at the anchor's right edge, grows leftward |
| `span-right` | center and right columns | left edge at the anchor's left edge, grows rightward |
| `span-top` | center and top rows | bottom edge at the anchor's bottom edge, grows upward |
| `span-bottom` | center and bottom rows | top edge at the anchor's top edge, grows downward |
| `span-all` | all three columns or rows | full extent of the containing block, centered on the anchor |
Logical forms such as span-inline-end and span-block-start exist too. You pair a span keyword with a row or column keyword on the other axis.
.dropdown {
position: absolute;
position-anchor: --menu-btn;
position-area: bottom span-right;
min-width: anchor-size(width);
}Read this as "the bottom row, and the center and right columns". The dropdown opens below the button with its left edge aligned to the button's left edge. It is free to extend to the right. A span-left version would align the right edges, which suits a button near the right edge of the page. The min-width: anchor-size(width) line, which gets its own chapter, keeps the dropdown at least as wide as the button.
What the area does to sizing and alignment
Three behaviors cause most of the surprises.
- The area is the available space. An element with
width: autoshrinks to fit the selected area. A tooltip in the narrow top-left corner cell of a small viewport can wrap into a very thin column. When text wraps unexpectedly, select a wider area with a span keyword, or setwidth: max-content. - Alignment defaults toward the anchor. Inside the area, the default alignment pushes the element against the anchor for outer cells, and centers it when the area is the center or spans all columns or rows. You rarely need
justify-selforalign-self, but you can set them when you want a different edge, for examplejustify-self: start. - Insets and margins are measured against the area.
position-areachanges the containing block used for the element to the selected area. Any inset value you set resolves relative to that area, not the original block. Leave insets atauto, which is the initial value, unless you want to nudge the element inside the area.
position-area or anchor()?
Both properties work on the same anchor, and you can use them together. Choose by the shape of the placement.
| Situation | Better choice |
|---|---|
| Tooltip or menu on one side of the anchor | `position-area` |
| Same placement must flip to another side when it overflows | `position-area`, because the fallbacks chapter works well with named areas |
| Element must line up two arbitrary edges, such as a selection handle | `anchor()` |
| Arrow, caret, or offset from a specific percentage of the anchor | `anchor()` with a percentage |
| Element must be stretched between anchor edges and the viewport | either, but `position-area` is shorter |
Because position-area hands the browser a whole region instead of coordinates, it is also the easier property to flip on overflow. Fallback rules can swap one area keyword for another without recomputing offsets.
Common errors
- Forgetting
position-anchor. Declaringanchor-nameon the trigger is not enough. The positioned element must name the anchor too, orposition-areadoes nothing. - Leftover insets. An old
top: 0; left: 0on the element is resolved against the area and drags the box to its corner. Remove insets when switching toposition-area. - Mixing physical and logical keywords. The declaration is invalid and the browser drops it. Check the Styles panel for a struck-through value.
- Expecting a gap from the grid. The grid cells touch the anchor. Add spacing with a margin on the side facing the anchor, such as
margin-bottomfor an element above it. - Popover defaults. A popover element has built-in
inset: 0andmargin: auto, which fightposition-area. Reset them withinset: autoandmargin: 0. The popover chapter covers this in detail.
Exercise
A "Settings" button sits near the right edge of the page. You want its menu to open below the button, with the menu's right edge lined up with the button's right edge, and the menu free to grow leftward. Which position-area value do you use, and what insets should the menu have?
Show answer
Use position-area: bottom span-left;. This selects the bottom row and the center and left columns, so the menu's right edge sits at the anchor's right edge and it grows leftward. Leave top, right, bottom, and left at auto (or remove them), because insets are resolved against the selected area and would shift the menu away from the anchor. Add margin-top if you want a gap below the button.
Exercise
A tooltip uses position-area: top left. On a narrow screen its text wraps into a column one word wide. Give two ways to fix this without changing which side of the anchor it appears on.
Show answer
First, select a wider area with a span keyword, such as top span-left, so the area includes the center column and is wider. Second, stop the text from shrinking with width: max-content, or with a min-width, so the element is not limited to the narrow cell width.