Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

anchor-size() — anchor-box size

`anchor-size()` calculates a length from a real anchor box dimension for a dependent absolutely positioned element.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )

anchor-size() calculates a length from a real anchor box dimension for a dependent absolutely positioned element.

The example gives a panel the width anchor-size(--card width) and half of anchor-size(--card height); measurement confirms a 120 by 20 pixel panel aligned beneath its 120 by 40 pixel anchor.

Grammar: anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? ).

width and height read physical dimensions; block and inline map axes using the containing block’s writing mode, whereas self-block and self-inline use the queried element’s own writing mode. An omitted dimension defaults to the property’s axis. A missing resolvable anchor invokes the length fallback, and no fallback makes the declaration invalid at computed-value time.

Examples

const stage=document.createElement('div'),card=document.createElement('div'),panel=document.createElement('div');document.body.append(stage);stage.append(card,panel);stage.style.cssText='position:relative;width:320px;height:180px';card.style.cssText='position:absolute;left:30px;top:20px;width:120px;height:40px;anchor-name:--card';panel.style.cssText='position:absolute;position-anchor:--card;left:anchor(--card left);top:anchor(--card bottom);width:anchor-size(--card width);height:calc(anchor-size(--card height)/2)';const a=card.getBoundingClientRect(),b=panel.getBoundingClientRect(),s=getComputedStyle(panel);const aligned=Math.round(b.left-a.left)===0&&Math.round(b.top-a.bottom)===0;if(!aligned||Math.round(b.width)!==120||Math.round(b.height)!==20)throw new Error('anchor-size relationship did not resolve');console.log('size='+Math.round(b.width)+'x'+Math.round(b.height)+'; aligned='+aligned+'; computed='+s.width+','+s.height);

Result

size=120x20; aligned=true; computed=120px,20px

`anchor-size()` calculates a length from a real anchor box dimension for a dependent absolutely positioned element.

Sources