In this example, you can move multiple positioned elements around, associating them with different anchors. This example demonstrates how an anchor can be associated with multiple positioned elements, but an anchor-positioned element can only be associated with a single anchor at a time, the anchor defined by the anchor-position
property.
HTML
We have four anchors and two positioned elements, distinguished with different id
values. The positioned elements contain <select>
boxes that allow you to choose which anchor you want to associate them with.
<div id="anchor-container">
<div class="anchor" id="anchor1">⚓︎</div>
<div class="anchor" id="anchor2">⚓︎</div>
<div class="anchor" id="anchor3">⚓︎</div>
<div class="anchor" id="anchor4">⚓︎</div>
</div>
<div class="infobox" id="infobox1">
<form>
<label for="anchor1-anchor-select">Place infobox on:</label>
<select id="anchor1-anchor-select">
<option value="1">Anchor 1</option>
<option value="2">Anchor 2</option>
<option value="3">Anchor 3</option>
<option value="4">Anchor 4</option>
</select>
</form>
</div>
<div class="infobox" id="infobox2">
<form>
<label for="anchor2-anchor-select">Place infobox on:</label>
<select id="anchor2-anchor-select">
<option value="1">Anchor 1</option>
<option value="2">Anchor 2</option>
<option value="3">Anchor 3</option>
<option value="4">Anchor 4</option>
</select>
</form>
</div>
CSS
We declare the first anchor
<div>
as an anchor using the anchor-name
property, which is given two comma-separated anchor names, one for each positioned element. This is the initial state of the demo — both positioned elements will be tethered to the first anchor.
#anchor1 {
anchor-name: --myAnchor1, --myAnchor2;
}
Each of the positioned elements is given a position-anchor
property with a value matching one of the two anchor names. The positioned elements are then given anchor-relative positioning information using a combination of inset, alignment, and margin properties.
#infobox1 {
position-anchor: --myAnchor1;
position: fixed;
left: anchor(right);
align-self: anchor-center;
margin-left: 10px;
}
#infobox2 {
position-anchor: --myAnchor2;
position: fixed;
bottom: anchor(top);
justify-self: anchor-center;
margin-bottom: 15px;
}
JavaScript
We dynamically change which anchor elements the anchor-name
values are set on in response to different anchors being selected in the positioned elements' <select>
menus. The key functionality here is the change
event handler, updateAnchorNames()
. It sets both anchor names on one anchor, if the anchors chosen in the two <select>
menus are the same. Otherwise, it sets a single anchor name on two separate anchors as appropriate.
// Get references to the two select menus
const select1 = document.querySelector("#anchor1-anchor-select");
const select2 = document.querySelector("#anchor2-anchor-select");
// Store references to all the anchors in a NodeList (array-like)
const anchors = document.querySelectorAll("#anchor-container > div");
// Set the same change event handler on both select menus
select1.addEventListener("change", updateAnchorNames);
select2.addEventListener("change", updateAnchorNames);
function updateAnchorNames() {
// Remove all anchor names from all anchors
for (const anchor of anchors) {
anchor.style.anchorName = "none";
}
// convert the select menu values to numbers, and remove one to
// make them match the selected anchors' index positions in the NodeList
const value1 = Number(select1.value) - 1;
const value2 = Number(select2.value) - 1;
if (value1 === value2) {
// If the chosen anchors are both the same, set both anchor
// names on the same anchor
anchors[value1].style.anchorName = "--myAnchor1, --myAnchor2";
} else {
// If they are not the same, set the anchor names separately
// on each selected anchor
anchors[value1].style.anchorName = "--myAnchor1";
anchors[value2].style.anchorName = "--myAnchor2";
}
}
Result
Select different values from the drop-down menus to change the anchors that the elements are positioned relative to.