Note: We strive to keep the polyfill up-to-date with ongoing changes to the spec, and we welcome code contributions and financial support to make that happen.
With polyfill applied: Target and Anchor’s right edges line up. Target’s top edge lines up with the bottom edge of the Anchor.
Note: this will not work across shadow root boundaries, and will not work with constructed stylesheets.
<anchor-web-component>
<template shadowrootmode="open">
<style>
#my-anchor-positioning {
anchor-name: --my-anchor-positioning;
}
#my-target-positioning {
position: absolute;
top: anchor(--my-anchor-positioning bottom);
right: anchor(--my-anchor-positioning right, 50px);
}
</style>
<div style="position: relative">
<div id="my-target-positioning">Target</div>
<div id="my-anchor-positioning">Anchor</div>
</div>
</template>
</anchor-web-component>
<script>
class AnchorDemo extends HTMLElement {
connectedCallback() {
window.ANCHOR_POSITIONING_POLYFILL({ roots: [this.shadowRoot] });
}
}
customElements.define("anchor-web-component", AnchorDemo);
</script>
At OddBird, we love contributing to the languages & tools developers rely on. We’re currently working on polyfills for new Popover & Anchor Positioning functionality, as well as CSS specifications for functions, mixins, and responsive typography. Help us keep this work sustainable and centered on your needs as a developer! We display sponsor logos and avatars on our website.
Sponsor OddBird’s OSS Work