База знанияИзпитен център
BGБългарски
Избери език13

CKTIS / CSS

anchor-size() — размер на кутия-котва

`anchor-size()` изчислява дължина от реален размер на кутията-котва за зависим абсолютно позициониран елемент.

Версия: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Синтаксис

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

anchor-size() изчислява дължина от реален размер на кутията-котва за зависим абсолютно позициониран елемент.

Примерът задава на панела ширина anchor-size(--card width) и половината от anchor-size(--card height); измерването потвърждава панел 120 на 20 пиксела, подравнен под котва 120 на 40.

Граматика: anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? ).

width и height четат физически размери; block и inline съпоставят осите според режима на писане на съдържащия блок, докато self-block и self-inline използват собствения режим на писане на заявяващия елемент. При пропуснат размер се използва оста на свойството. Без разрешима котва се прилага резервната дължина; без нея декларацията е невалидна при изчисляване.

Примери

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);

Резултат

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

`anchor-size()` изчислява дължина от реален размер на кутията-котва за зависим абсолютно позициониран елемент.

Източници