Reputation: 8335
I'm trying to align a to the bottom-right corner of an element using the CSS Anchor Positioning
#sidebar {
anchor-name: --sidebar;
height: 100vh;
width: max-content;
position: fixed; /* Sidebar is fixed to the viewport */
}
button.sidebar-toggle-button {
position: absolute;
position-anchor: --sidebar;
left: anchor(right);
bottom: anchor(bottom);
z-index: 1;
}
<aside id="sidebar" class="scrollbar-custom">
... content ...
<button class="sidebar-toggle-button button-base">
<!-- ☰ Glossary -->
</button>
</aside>
fixed
or absolute
on #sidebar, Chrome's DevTools reports '--sidebar is not define` and the position-anchor stops working.Why it fail, I'm missing something ?
Are there a way to make it work ?
Upvotes: -1
Views: 58
Reputation: 8335
I found a related thread on GitHub: CSSWG Drafts Issue #10419, which discusses a similar issue.
The solution lies in the order of elements in the DOM tree. The position-anchor property depends on the anchor element being defined before the element that references it.
To fix my issue, I moved the tag after the in the DOM, and now it works as expected.
<button class="sidebar-toggle-button">Toggle Sidebar</button>
<aside id="sidebar">
<!-- Sidebar content -->
</aside>
<aside id="sidebar">
<!-- Sidebar content -->
</aside>
<button class="sidebar-toggle-button">Toggle Sidebar</button>
Upvotes: 1