Say you have an elements with CSS tooltips and you’re going to position those tooltips such that it opens up next to the element on hover (or probably better: when clicked/tapped). Next to it where? Above it? What if the element is already really close to the top of the screen? In that case, it should probably open below it. Or vice versa — and the same goes for the left and right edges of the screen. You definitely want it to be visible rather than overflowing the viewport.
Sometimes when you open new UI elements, they need to be edge-aware to prevent the content inside from triggering weird scrollbars, or worse, cutting off content.
As ever, everything old is new again. Check out Floating UI, designed just for this problem.
Floating UI is a low-level toolkit to position floating elements while intelligently keeping them in view. Tooltips, popovers, dropdowns, menus, and more.
It looks super well done. I like the focus, the demos are super well done, and it’s a pretty tiny dependency.
But ya know what would be even cooler? If CSS could do this all by itself. That’s the vibe with CSS Anchored Positioning — for now just an “explainer” document:
When building interactive components or applications, authors frequently want to leverage UI elements that can render in a “top-layer”. Examples of such UI elements include content pickers, teaching UI, tooltips, and menus. “Enabling Popups” introduced a new
popupelement to make many of these top-layer elements easier to author.
Authors frequently wish to “pin” or “anchor” such top-layer UI to a point on another element, referred to here as an “anchor element”. How the top-layer UI is positioned with respect to its anchor element is further influenced or constrained by the edges of the layout viewport.
I love it. The web platform at its best. Seeing what authors are needing to do and reaching for libraries to do, and trying to step in and do it natively (and hopefully better).
Floating UI was formed from the people that brought us popperjs!
The proposal introduces a new HTML element with a unique behavior when styled as
position:fixed. I am wondering if that’s necessary. Wouldn’t it be better to just introduce a new value like
position:attached, which is taken out of the normal layering like
position:fixedwhile accounting for the parent-based position and the viewport bounds? I feel like it would open more possibilities than tooltips/popups.