CKTIS / CSS
anchor() — poloha strany kotvy
`anchor()` vypočíta dĺžku k vybranej strane skutočného kotviaceho boxu a použije ju na umiestnenie absolútne pozicovaného boxu.
Verzia: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
anchor( <anchor-name>? && <anchor-side> , <length-percentage>? )anchor() vypočíta dĺžku k vybranej strane skutočného kotviaceho boxu a použije ju na umiestnenie absolútne pozicovaného boxu.
Príklad prepojí pomenované prvky: anchor(--pin right) zarovná ľavý okraj pomôcky s pravým okrajom kotvy a anchor(--pin bottom) jej horný okraj so spodným; obe namerané medzery sú nulové.
Gramatika: anchor( <anchor-name>? && <anchor-side> , <length-percentage>? ).
Funkcia je povolená iba vo vlastnostiach inset boxu s position: absolute. anchor-name: --pin vytvorí kotvu a position-anchor: --pin ju vyberie ako predvolenú; fyzická strana musí zodpovedať osi inset. Neriešiteľný odkaz použije zadanú záložnú hodnotu, alebo bez nej zneplatní deklaráciu pri výpočte.
Príklady
const stage=document.createElement('div'),pin=document.createElement('div'),tip=document.createElement('div');document.body.append(stage);stage.append(pin,tip);stage.style.cssText='position:relative;width:300px;height:180px';pin.style.cssText='position:absolute;left:70px;top:40px;width:80px;height:30px;anchor-name:--pin';tip.style.cssText='position:absolute;position-anchor:--pin;left:anchor(--pin right);top:anchor(--pin bottom);width:20px;height:10px';const a=pin.getBoundingClientRect(),b=tip.getBoundingClientRect(),s=getComputedStyle(tip);const x=Math.round(b.left-a.right),y=Math.round(b.top-a.bottom);if(x!==0||y!==0)throw new Error('anchor relationship did not resolve: '+x+','+y);console.log('edge-gap='+x+','+y+'; computed='+s.left+','+s.top);Výsledok
edge-gap=0,0; computed=150px,70px
`anchor()` vypočíta dĺžku k vybranej strane skutočného kotviaceho boxu a použije ju na umiestnenie absolútne pozicovaného boxu.