CKTIS / CSS
anchor-size() — velikost kotevního boxu
`anchor-size()` vypočte pro závislý absolutně pozicovaný prvek délku ze skutečného rozměru kotevního boxu.
Verze: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntaxe
anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )anchor-size() vypočte pro závislý absolutně pozicovaný prvek délku ze skutečného rozměru kotevního boxu.
Příklad nastaví panelu šířku anchor-size(--card width) a polovinu anchor-size(--card height); měření potvrdí panel 120 krát 20 pixelů zarovnaný pod kotvou 120 krát 40.
Gramatika: anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? ).
width a height čtou fyzické rozměry; block a inline mapují osy podle režimu psaní obsahujícího bloku, zatímco self-block a self-inline používají vlastní režim psaní dotazujícího se prvku. Vynechaný rozměr použije osu vlastnosti. Bez řešitelné kotvy se použije záložní délka; bez ní je deklarace při výpočtu neplatná.
Příklady
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);Výsledek
size=120x20; aligned=true; computed=120px,20px
`anchor-size()` vypočte pro závislý absolutně pozicovaný prvek délku ze skutečného rozměru kotevního boxu.