Sub topic 5
Sizing from the anchor with anchor-size()
This chapter assumes you know the CSS position property, inset properties such as top and left, and how an anchor is declared with anchor-name and linked with position-anchor. If those are new, read the earlier chapters on declaring anchors first.
Why size from the anchor
Placement is only half of the job. A dropdown that opens under a button usually looks wrong if it is narrower than the button. A tooltip may need to match the width of its trigger. Without anchor positioning, you would measure the button in JavaScript and copy its width into the menu.
The anchor-size() function removes that step. It returns a length taken from the anchor's current box, so the anchored element tracks the anchor's size whenever it changes.
The syntax
anchor-size() takes a size keyword and, optionally, an anchor name and a fallback value.
.dropdown {
position: absolute;
position-anchor: --menu-btn;
position-area: bottom span-right;
min-width: anchor-size(width);
}Here the dropdown opens below the anchor and extends to the right from the anchor's left edge. anchor-size(width) resolves to the anchor's width, so the dropdown is never narrower than the button.
The keywords you can pass are:
| Keyword | Meaning |
|---|---|
| `width` | The anchor's width |
| `height` | The anchor's height |
| `block` | The anchor's size in the block direction |
| `inline` | The anchor's size in the inline direction |
| `self-block` | The anchor's block size, based on the anchored element's writing mode |
| `self-inline` | The anchor's inline size, based on the anchored element's writing mode |
Use width and height for physical sizes. Use the logical keywords when your layout must work in vertical writing modes.
Where it can be used
anchor-size() is valid in the sizing properties: width, height, min-width, min-height, max-width, max-height. It also works in inset properties and margins. The element using it must be absolutely or fixed positioned, and it must have a valid anchor.
The choice of property matters:
width: anchor-size(width)forces an exact match.min-width: anchor-size(width)lets the content grow wider than the anchor but never narrower.max-width: anchor-size(width)caps the element at the anchor's width.
For menus, min-width is usually the best choice, because long menu items should not be cut off.
Naming a specific anchor
By default, the function uses the anchor given by position-anchor. You can name another anchor by placing its dashed name first:
.panel {
position: absolute;
position-anchor: --menu-btn;
position-area: bottom;
width: anchor-size(--sidebar width);
}This panel is placed against --menu-btn but takes its width from --sidebar. That is useful when one element must line up with one anchor and match the size of another.
Providing a fallback value
If the anchor does not exist, the function would be invalid. You can add a fallback length as the last argument:
.dropdown {
min-width: anchor-size(width, 10rem);
}When no anchor is found, min-width becomes 10rem. Without that argument, the declaration is invalid at computed-value time, and the property behaves as if it were unset.
Combining with calc()
Because anchor-size() returns a length, you can do math with it:
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
max-width: calc(anchor-size(width) * 2);
}This tooltip can be up to twice as wide as its trigger. A common use is adding padding: min-width: calc(anchor-size(width) + 1rem).
Common errors
- The size has no effect. Check that the element is
position: absoluteorfixedand that it is linked to an anchor throughposition-anchoror an explicit name. - The element is the wrong width in a vertical writing mode. Switch from
widthtoinline, or useself-inline. - Content is clipped. You used
widthormax-widthwheremin-widthwas needed. - Nothing happens in some browsers. Anchor positioning is not available everywhere. Later chapters cover feature detection.
Exercise
A dropdown is placed below a button with position-area: bottom span-right. You want it to be at least as wide as the button, but wider when menu items are long. Which declaration do you use, and what would you add so it still works if the anchor is missing?
Show answer
Use min-width: anchor-size(width, 10rem);. min-width lets long items widen the dropdown. The second argument, 10rem, is the fallback length used when no anchor is found. Any sensible length works.
Exercise
Write a rule that limits a tooltip to the width of its anchor named --tip, using the logical keyword that suits a vertical writing mode.
Show answer
.tooltip { max-inline-size: anchor-size(--tip inline); }. max-inline-size is the logical counterpart of max-width, and the inline keyword gives the anchor's size along the inline axis.