Create a CSS-only tooltip system using the CSS Anchor Positioning API. The tooltip should automatically flip to the top if there isn't enough space on the bottom. Provide a JavaScript fallback using floating-ui for browsers that don't support anchor positioning yet.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Providing CSS Anchor API tooltip with Floating UI fallback
The plan is a minimal CSS-only tooltip using the experimental CSS Anchor Positioning API with defined tries for auto-flip, wrapped in an @supports block to gate styling, plus a JavaScript fallback using Floating UI that applies when anchor positioning isn't supported. The tooltip anchors to the trigger with shared custom properties, showing on hover/focus with consistent positioning logic.
Below is a minimal, self‑contained example that:
Uses CSS Anchor Positioning to place a tooltip under a trigger and will automatically “flip” to the top if there isn’t enough space.
Falls back to floating‑ui for browsers that don’t support anchor positioning yet.
⚠️ Anchor positioning is still experimental; you’ll need a recent Chrome/Edge with the feature flag enabled to see the pure‑CSS part work. The JS fallback will work everywhere.
html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Anchor-positioned tooltip with fallback</title>
<style>
:root {
color-scheme: light dark;
font-family: system-ui, sans-serif;
}
body {
min-height: 100vh;
display: grid;
place-items: center;
padding: 3rem;
gap: 2rem;
}
.tooltip-pair {
position: relative; /* helps JS fallback when using absolute positioning */
}
.tooltip-trigger {
--anchor-name: --tooltip-1; /* per-instance anchor name */
padding: 0.6rem 1rem;
border-radius: 0.5rem;
border: 1px solid color-mix(in srgb, currentColor 20%, transparent);
background: color-mix(in srgb, currentColor 8%, transparent);
cursor: pointer;
}
/* Base tooltip styling */
.tooltip {
position: fixed; /* works for both anchor CSS and JS fallback */
max-inline-size: min(24ch, 80vw);
padding: 0.5rem 0.75rem;
border-radius: 0.5rem;
background: #1f1f1f;
color: white;
font-size: 0.9rem;
line-height: 1.3;
z-index: 1000;
opacity: 0;
pointer-events: none;
transition: opacity 120ms ease;
}
/* Simple show/hide on hover/focus for the demo */
.tooltip-trigger:focus + .tooltip,
.tooltip-trigger:hover + .tooltip {
opacity: 1;
}
/* Anchor Positioning implementation */
@supports (position-anchor: --x) {
.tooltip-trigger {
anchor-name: var(--anchor-name);
}
.tooltip {
position-anchor: var(--anchor-name);
position-try: --try-bottom, --try-top;
}
/* Try placing under the trigger */
@position-try --try-bottom {
inset-block-start: anchor(bottom);
inset-inline-start: anchor(center);
translate: -50% 8px;
}
/* Fallback: place above if bottom doesn’t fit */
@position-try --try-top {
inset-block-end: anchor(top);
inset-inline-start: anchor(center);
translate: -50% -8px;
}
}
/* When JS fallback runs, we mark the tooltip as "floating-active"
to skip anchor properties if you wish; not strictly required here. */
.floating-active {
opacity: 0; /* JS sets opacity on show */
}
</style>
</head>
<body>
<div class="tooltip-pair">
<button class="tooltip-trigger" aria-describedby="tip-1">Hover me</button>
<div class="tooltip" id="tip-1" role="tooltip">I’ll flip to the top if there isn’t room below.</div>
</div>
<script type="module">
// Detect anchor positioning support
const anchorSupported = CSS.supports?.('position-anchor: --x');
if (!anchorSupported) {
// Import floating-ui on the fly
import('https://unpkg.com/@floating-ui/dom@1.6.5/dist/floating-ui.dom.esm.js').then(
({ computePosition, offset, flip, shift }) => {
const pair = document.querySelector('.tooltip-pair');
const referenceEl = pair.querySelector('.tooltip-trigger');
const tooltipEl = pair.querySelector('.tooltip');
tooltipEl.classList.add('floating-active');
let cleanup = () => {};
const show = () => {
tooltipEl.style.opacity = '1';
tooltipEl.style.position = 'fixed'; // viewport coords
update();
window.addEventListener('scroll', update, true);
window.addEventListener('resize', update, true);
cleanup = () => {
window.removeEventListener('scroll', update, true);
window.removeEventListener('resize', update, true);
};
};
const hide = () => {
tooltipEl.style.opacity = '0';
cleanup();
};
const update = () => {
computePosition(referenceEl, tooltipEl, {
placement: 'bottom',
middleware: [
offset(8), // space between trigger and tooltip
flip(), // flip to top/other sides if needed
shift({ padding: 8 }) // keep in viewport
],
}).then(({ x, y }) => {
tooltipEl.style.left = `${x}px`;
tooltipEl.style.top = `${y}px`;
});
};
referenceEl.addEventListener('mouseenter', show);
referenceEl.addEventListener('focus', show);
referenceEl.addEventListener('mouseleave', hide);
referenceEl.addEventListener('blur', hide);
}
);
}
</script>
</body>
</html>
How it works:
CSS (anchor positioning): The trigger sets an anchor-name. The tooltip uses position-anchor with position-try listing two attempts: --try-bottom first, and --try-top if the first doesn’t fit. The UA picks the first that fits; effectively “flip” is built in.
JS fallback (floating‑ui): If position-anchor isn’t supported, we load @floating-ui/dom and call computePosition with offset + flip + shift middleware to mimic the same behavior (bottom by default, flip to top if needed, keep it in the viewport). The tooltip is shown/hidden on hover/focus for this demo.