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 the box model. It also assumes you can declare an anchor with anchor-name and link an element to it with position-anchor, as covered earlier in this tutorial.
Why size from the anchor
Placing a dropdown under its button is only half of the job. The dropdown usually needs a size that depends on the button. A select-like menu should be at least as wide as the control that opened it. A tooltip arrow might need to match the trigger's height. Before anchor positioning, you measured the button in JavaScript with getBoundingClientRect() and wrote the result into an inline style. That code had to run again on resize, on font load, and on layout changes.
The anchor-size() function removes that code. It returns the width or height of the anchor, and the browser keeps the value current as the anchor changes size.
The syntax
anchor-size() takes an optional anchor name and a size keyword. It can also take a fallback value.
anchor-size(<anchor-name>? <anchor-size>, <fallback>?)The common size keywords are:
| Keyword | Meaning |
|---|---|
| `width` | The anchor's physical width |
| `height` | The anchor's physical height |
| `block` | The anchor's size in the block axis |
| `inline` | The anchor's size in the inline axis |
| `self-block` | The anchor's size in the axis that is the block axis of the anchored element |
| `self-inline` | The anchor's size in the axis that is the inline axis of the anchored element |
Use width and height when you think in physical terms. Use block and inline when your layout must follow writing modes, for example in a vertical-text interface.
The anchor name is optional. If you leave it out, the function uses the anchor named by the element's position-anchor property. You only need the name when an element must read the size of a different anchor than its default one.
.dropdown {
position: absolute;
position-anchor: --menu-btn;
min-width: anchor-size(width);
}This is the same line used in the dropdown example from the topic's code samples. The menu is never narrower than the button, and it can grow wider when its content needs more space.
Where the function can be used
anchor-size() is valid in the properties that size and position an anchored element. These are:
width,height,min-width,min-height,max-width,max-height- The inset properties:
top,right,bottom,left, and their logical forms such asinset-inline-start - Margin properties
It works inside calc(), min(), max(), and clamp(). That lets you build sizes such as "the anchor's width plus 16px", or "no wider than twice the anchor".
The element that uses the function must be absolutely or fixed positioned. It also needs a usable anchor. Anchors must come from a valid anchor-name and be found by the element, either through position-anchor or through an explicit name in the function.
A worked example: a select-style menu
The next example builds a menu that matches its button's width, grows if its content is wider, and never grows past a limit tied to the anchor.
<button id="size-btn" popovertarget="size-menu">Choose a plan</button>
<div id="size-menu" popover>
<a href="/free">Free</a>
<a href="/team">Team (up to 25 people)</a>
<a href="/enterprise">Enterprise with custom contract</a>
</div>#size-btn {
anchor-name: --size-btn;
}
#size-menu {
position-anchor: --size-btn;
position-area: bottom span-right;
inset: auto;
margin: 0;
min-width: anchor-size(width);
max-width: calc(anchor-size(width) * 2);
}
#size-menu a {
display: block;
padding: 0.5rem 0.75rem;
}Here is what each part does:
position-area: bottom span-rightplaces the menu below the button, starting at the button's left edge and extending to the right.inset: autoandmargin: 0remove the defaults that the browser gives to popovers, soposition-areacontrols the placement. This is the same reset used in the popover example earlier in the topic.min-width: anchor-size(width)guarantees the menu is at least as wide as the button.max-width: calc(anchor-size(width) * 2)stops long labels from producing an enormous menu. Text inside wraps once the limit is reached.
If the button changes width, for example because a translated label is longer, the menu follows without any script.
Using anchor-size() with the width property
You can use width instead of min-width when you want an exact match:
.menu {
position: absolute;
position-anchor: --size-btn;
position-area: bottom;
width: anchor-size(width);
}This makes a menu exactly as wide as its button. Long content then wraps or overflows. For most menus, min-width is the safer choice, because it never truncates content. Choose width for cases where alignment matters more than fitting content, such as a visual "attached panel" look.
Sizing with a different anchor
An element can read the size of any anchor it can see. Pass the name as the first argument:
.summary {
position: absolute;
position-anchor: --header;
position-area: bottom;
width: anchor-size(--sidebar width);
}The element is placed against --header, but its width equals the width of --sidebar. Note that the name and the keyword are separated by a space, not a comma. The comma is reserved for the fallback value.
Sizing through inset properties
Because anchor-size() is allowed in inset properties, you can use it to offset an element by an amount based on the anchor. A common use is a badge that sits half its anchor's height above the anchor's top edge:
.badge {
position: absolute;
position-anchor: --avatar;
left: anchor(right);
bottom: calc(anchor(top) - anchor-size(height) / 4);
translate: -50% 0;
}This mixes anchor(), which reads a position, with anchor-size(), which reads a length. Both can appear in the same calc() expression, because each resolves to a length.
Fallback values
If the function cannot resolve, the declaration would normally become invalid at computed-value time. This happens when the element has no matching anchor. You can provide a second argument after a comma, which the browser uses instead:
.menu {
min-width: anchor-size(width, 12rem);
}With a valid anchor, the menu uses the anchor's width. Without one, for example when the anchor element is not rendered, the menu uses 12rem. This is a good habit for components that might render before their anchor exists.
How it interacts with position-area
position-area places an element in a cell of a 3x3 grid built around the anchor. The element is sized to fit that cell by default. This has two effects on anchor-size():
- A
min-widthorwidththat usesanchor-size()applies inside the cell. If the cell is narrower than the requested size, the element overflows the cell unless you reduce the value. - Spanning keywords such as
span-rightgive the element a wider cell, which leaves more room for ananchor-size()based minimum.
For example, position-area: bottom uses the column that matches the anchor's width, so an element with min-width: anchor-size(width) fits it exactly. With bottom span-right, the cell extends to the right of the anchor, so content wider than the anchor can grow without leaving the cell.
Common errors
- No effect at all. The element is not absolutely or fixed positioned, or the anchor name does not match. Check that
position-anchorandanchor-nameuse the same dashed identifier, such as--size-btn. - Menu much wider than expected.
anchor-size(width)reads the anchor's border box. If the anchor is a wide block element, such as adivwith no width set, the anchor fills its container and so does your element. Give the anchor an inline or fit-content width, such asdisplay: inline-block. - Comma where a space belongs. Write
anchor-size(--sidebar width), notanchor-size(--sidebar, width). With a comma, the browser readswidthas a fallback value, and the declaration does not do what you intended. - Using a size where a position is needed.
anchor-size()returns a length, not an edge position. To align an edge with the anchor, useanchor()orposition-area. - Unexpected width after a content change. If you use
widthinstead ofmin-width, the element stays at the anchor's width and wraps content. Switch tomin-widthif you want it to grow. - Anchor hidden by
display: none. An anchor that is not rendered has no box, soanchor-size()falls back or the declaration is invalid. Provide a fallback length when this can happen.
Browser support
Anchor positioning is supported in Chromium-based browsers, and support in other engines is arriving more slowly. Guard the declarations with @supports and keep a sensible layout outside the block, as shown in the last chapter of this tutorial. A plain min-width such as 10rem placed before the guarded block gives other browsers a workable menu:
#size-menu {
min-width: 10rem;
}
@supports (anchor-name: --a) {
#size-menu {
position-anchor: --size-btn;
position-area: bottom span-right;
inset: auto;
margin: 0;
min-width: anchor-size(width);
}
}Summary of the key points
anchor-size()returns the anchor's width, height, or logical size as a length.- It works in sizing, inset, and margin properties, and inside
calc(),min(),max(), andclamp(). - Omit the anchor name to use the element's
position-anchor. Add it, separated by a space, to read another anchor. - A comma introduces the fallback value, not the keyword.
- Prefer
min-widthoverwidthwhen content must not be cut off.
Exercise
A dropdown is placed under its button with position-area: bottom span-right. You want it to be at least as wide as the button, but no wider than three times the button's width. Which two declarations do this, and why would width: anchor-size(width) be a poor choice?
Show answer
Use min-width: anchor-size(width); and max-width: calc(anchor-size(width) * 3);. The width declaration would force the dropdown to exactly the button's width, so longer menu items would wrap or overflow instead of letting the menu grow to fit its content.
Exercise
What is wrong with the declaration width: anchor-size(--sidebar, width);, and how do you fix it?
Show answer
The comma separates the anchor-size() arguments from the fallback value, so width is read as a fallback, not as the size keyword. Replace the comma with a space: width: anchor-size(--sidebar width);.
Exercise
A component sometimes renders before its anchor element exists. How can you keep its minimum width sensible in that case?
Show answer
Add a fallback length as the second argument after a comma, for example min-width: anchor-size(width, 12rem);. The browser uses 12rem when no anchor can be found.