Anchored Popover
A popover with an arrow that flips above or below to fit and closes on Escape or outside click.
AI prompt
Build an anchored popover component. A trigger button "Details" opens a 220 pixel wide card with a small arrow pointing at the button. The card contains a title, two lines of text and a link button. It prefers to open below but flips above when there is not enough space; two small buttons let the user prefer Top or Bottom to test. Close on Escape, on outside click and when the trigger is pressed again; return focus to the trigger. aria-expanded and aria-controls on the trigger, role dialog without modal on the card. Subtle shadow, 12 pixel radius, 160 millisecond fade and slide with a prefers-reduced-motion fallback. Plain HTML, CSS and JavaScript.About this pattern
Definition
A popover is a small floating panel anchored to a trigger. Unlike a tooltip it can hold interactive content and opens on click; unlike a modal it does not block the page.
Purpose & UX principles
- Use it for secondary details, quick settings or short forms that relate to one control.
- Open on click or Enter, close on Escape, outside click or when focus leaves.
- Position it automatically to stay on screen, with an arrow that points to the trigger.
Interaction states
- Closed, open above, open below. Placement buttons let you prefer top or bottom; the popover flips if there is no space.
Motion specification
- Popover fades and slides 6px toward its final position over 160ms.
- Reduced motion: fade only.
Responsive behaviour
- Width up to 240px, limited to the viewport minus 16px margins; on very small screens consider a bottom sheet instead.
- Flip to the side with more room.
Code
<div class="bx"><div class="anch" id="anch"><button id="tg" type="button" aria-expanded="false" aria-controls="pop">Details</button><div class="pop" id="pop" role="dialog" aria-label="Details" hidden><i class="ar"></i><b>Billing period</b><p>Billed on the 1st.</p><a href="#" onclick="return false">Change plan</a></div></div>
<div class="pre" id="pre" role="group" aria-label="Preferred placement"><button data-p="top" aria-pressed="false">Prefer top</button><button data-p="bottom" aria-pressed="true">Prefer bottom</button></div></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.bx{position:relative;width:min(300px,100%);height:250px}
.anch{position:absolute;left:50%;top:148px;transform:translate(-50%,-50%)}
#tg{height:34px;padding:0 18px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:13px;cursor:pointer}
.pop{position:absolute;left:50%;width:220px;margin-left:-110px;padding:12px 14px;border-radius:12px;background:var(--surface);border:1px solid var(--line);box-shadow:0 10px 28px rgba(0,0,0,.12);font-size:12.5px;animation:pi .16s ease-out;z-index:2}
.pop[hidden]{display:none}
.pop.below{top:calc(100% + 10px)}.pop.above{bottom:calc(100% + 10px)}
.pop p{color:var(--muted);margin:3px 0 6px}.pop a{color:var(--text);font-weight:600;text-decoration:underline;text-underline-offset:2px}
.ar{position:absolute;left:50%;width:10px;height:10px;margin-left:-5px;background:var(--surface);border:1px solid var(--line);transform:rotate(45deg)}
.below .ar{top:-6px;border-right:0;border-bottom:0}.above .ar{bottom:-6px;border-left:0;border-top:0}
@keyframes pi{from{opacity:0;transform:translateY(var(--d,-6px))}}.above{--d:6px}
.pre{position:absolute;top:0;left:0;display:flex;gap:5px}.pre button{height:26px;padding:0 12px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
button:focus-visible,a:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.pop{animation:none}}const tg=document.getElementById('tg'),pop=document.getElementById('pop'),bs=[...document.querySelectorAll('#pre button')];let pref='bottom';
function place(){const r=tg.getBoundingClientRect(),need=pop.offsetHeight+16,below=innerHeight-r.bottom,above=r.top;let side=pref;if(side==='bottom'&&below<need&&above>below)side='top';else if(side==='top'&&above<need&&below>above)side='bottom';pop.className='pop '+(side==='top'?'above':'below');}
function open(o){pop.hidden=!o;tg.setAttribute('aria-expanded',String(o));if(o)place();}
tg.addEventListener('click',()=>open(pop.hidden));
document.addEventListener('pointerdown',e=>{if(!pop.hidden&&!pop.contains(e.target)&&e.target!==tg&&!e.target.closest('#pre'))open(false);});
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&!pop.hidden){open(false);tg.focus();}});
bs.forEach(b=>b.addEventListener('click',()=>{pref=b.dataset.p;bs.forEach(x=>x.setAttribute('aria-pressed',String(x===b)));if(!pop.hidden)place();}));One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Anchored Popover — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.bx{position:relative;width:min(300px,100%);height:250px}
.anch{position:absolute;left:50%;top:148px;transform:translate(-50%,-50%)}
#tg{height:34px;padding:0 18px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:13px;cursor:pointer}
.pop{position:absolute;left:50%;width:220px;margin-left:-110px;padding:12px 14px;border-radius:12px;background:var(--surface);border:1px solid var(--line);box-shadow:0 10px 28px rgba(0,0,0,.12);font-size:12.5px;animation:pi .16s ease-out;z-index:2}
.pop[hidden]{display:none}
.pop.below{top:calc(100% + 10px)}.pop.above{bottom:calc(100% + 10px)}
.pop p{color:var(--muted);margin:3px 0 6px}.pop a{color:var(--text);font-weight:600;text-decoration:underline;text-underline-offset:2px}
.ar{position:absolute;left:50%;width:10px;height:10px;margin-left:-5px;background:var(--surface);border:1px solid var(--line);transform:rotate(45deg)}
.below .ar{top:-6px;border-right:0;border-bottom:0}.above .ar{bottom:-6px;border-left:0;border-top:0}
@keyframes pi{from{opacity:0;transform:translateY(var(--d,-6px))}}.above{--d:6px}
.pre{position:absolute;top:0;left:0;display:flex;gap:5px}.pre button{height:26px;padding:0 12px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
button:focus-visible,a:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.pop{animation:none}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="anch" id="anch"><button id="tg" type="button" aria-expanded="false" aria-controls="pop">Details</button><div class="pop" id="pop" role="dialog" aria-label="Details" hidden><i class="ar"></i><b>Billing period</b><p>Billed on the 1st.</p><a href="#" onclick="return false">Change plan</a></div></div>
<div class="pre" id="pre" role="group" aria-label="Preferred placement"><button data-p="top" aria-pressed="false">Prefer top</button><button data-p="bottom" aria-pressed="true">Prefer bottom</button></div></div>
</div>
<script>
const tg=document.getElementById('tg'),pop=document.getElementById('pop'),bs=[...document.querySelectorAll('#pre button')];let pref='bottom';
function place(){const r=tg.getBoundingClientRect(),need=pop.offsetHeight+16,below=innerHeight-r.bottom,above=r.top;let side=pref;if(side==='bottom'&&below<need&&above>below)side='top';else if(side==='top'&&above<need&&below>above)side='bottom';pop.className='pop '+(side==='top'?'above':'below');}
function open(o){pop.hidden=!o;tg.setAttribute('aria-expanded',String(o));if(o)place();}
tg.addEventListener('click',()=>open(pop.hidden));
document.addEventListener('pointerdown',e=>{if(!pop.hidden&&!pop.contains(e.target)&&e.target!==tg&&!e.target.closest('#pre'))open(false);});
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&!pop.hidden){open(false);tg.focus();}});
bs.forEach(b=>b.addEventListener('click',()=>{pref=b.dataset.p;bs.forEach(x=>x.setAttribute('aria-pressed',String(x===b)));if(!pop.hidden)place();}));
</script>
</body></html>