Tooltip
A short hint that appears on hover or focus, flips to stay inside the screen.
AI prompt
Build a tooltip component for buttons placed at different positions of a 380x240 panel (left, centre, right and near the top edge). Tooltips are elements with role="tooltip" and unique ids referenced by aria-describedby on their triggers. Show on hover after a 400ms delay and immediately on keyboard focus; hide on mouseleave, blur or Escape, with a 100ms grace so the pointer can travel onto the tooltip without closing it. Position above the trigger by default with an 8px gap and a small arrow; if there is not enough space above, flip below; clamp horizontally so the tooltip never leaves the panel. Animate opacity and a 4px translate over 140ms ease-out. Implement positioning with getBoundingClientRect, no libraries. Reduced motion: opacity only.About this pattern
Definition
A small floating label that explains an element. It shows when the pointer rests on the element or when the element receives keyboard focus, and hides on leave, blur or Escape.
Purpose & UX principles
- Names icon-only controls and gives brief extra context.
- A tooltip is supplementary: never put essential information or interactive content in it, and do not use it on touch-only flows.
- Hovering the tooltip itself should keep it open so people with tremors or magnifiers can move onto it.
Interaction states
- Hidden, shown after a 400ms delay on hover (immediately on focus), hiding on mouseleave, blur or Escape.
- The trigger references the tooltip with
aria-describedby, and the tooltip hasrole="tooltip".
Motion specification
- Fade and 4px slide towards the trigger, 140ms ease-out.
- Opening delay 400ms for hover only; closing is instant after a 100ms grace period.
- Reduced motion: opacity only.
Responsive behaviour
- If there is no room above, the tooltip flips below. Near the left or right edge it shifts to stay inside the viewport.
- No hover on touch: use long-press or an explicit help button instead.
Code
<div class="panel" id="panel">
<button class="t tl" data-tip="Left edge — clamped inside">Left</button>
<button class="t tc" data-tip="Centred above the button">Centre</button>
<button class="t tr" data-tip="Right edge — clamped inside">Right</button>
<button class="t tt" data-tip="No room above, so I flip below">Top</button>
</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}
.panel{position:relative;width:min(380px,100%);height:240px;border:1px solid var(--line);border-radius:18px;background:var(--surface)}
.t{position:absolute;height:38px;padding:0 16px;border-radius:10px;border:1px solid var(--line);background:var(--surface-2);color:var(--text);font:inherit;font-size:14px;cursor:pointer}
.t:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.tl{left:8px;bottom:50px}.tc{left:50%;margin-left:-35px;bottom:50px}.tr{right:8px;bottom:50px}.tt{left:50%;margin-left:-28px;top:6px}
.tip{position:absolute;z-index:5;max-width:200px;padding:6px 10px;border-radius:8px;background:var(--text);color:var(--bg);font-size:12.5px;font-weight:600;line-height:1.3;opacity:0;pointer-events:auto;transition:opacity .14s ease-out,transform .14s ease-out;transform:translateY(4px)}
.tip.below{transform:translateY(-4px)}
.tip.show{opacity:1;transform:none}
.tip::after{content:"";position:absolute;left:var(--ax,50%);bottom:-4px;width:8px;height:8px;background:inherit;transform:translateX(-50%) rotate(45deg)}
.tip.below::after{bottom:auto;top:-4px}
@media (prefers-reduced-motion:reduce){.tip,.tip.below{transform:none;transition:opacity .14s}}const panel=document.getElementById('panel');let tip,timer,hideTimer,cur;
tip=document.createElement('div');tip.className='tip';tip.role='tooltip';tip.id='tip';panel.append(tip);
tip.addEventListener('mouseenter',()=>clearTimeout(hideTimer));tip.addEventListener('mouseleave',hide);
function place(el){
tip.textContent=el.dataset.tip;tip.classList.remove('below');
const p=panel.getBoundingClientRect(),r=el.getBoundingClientRect(),w=tip.offsetWidth,h=tip.offsetHeight;
let left=r.left-p.left+r.width/2-w/2,top=r.top-p.top-h-8;
if(top<4){top=r.bottom-p.top+8;tip.classList.add('below');}
const cl=Math.max(4,Math.min(p.width-w-4,left));tip.style.left=cl+'px';tip.style.top=top+'px';
tip.style.setProperty('--ax',(r.left-p.left+r.width/2-cl)+'px');
}
function show(el,delay){clearTimeout(hideTimer);clearTimeout(timer);timer=setTimeout(()=>{cur=el;el.setAttribute('aria-describedby','tip');place(el);tip.classList.add('show');},delay);}
function hide(){clearTimeout(timer);hideTimer=setTimeout(()=>{tip.classList.remove('show');if(cur)cur.removeAttribute('aria-describedby');},100);}
panel.querySelectorAll('.t').forEach(b=>{
b.addEventListener('mouseenter',()=>show(b,400));b.addEventListener('mouseleave',hide);
b.addEventListener('focus',()=>show(b,0));b.addEventListener('blur',hide);
});
document.addEventListener('keydown',e=>{if(e.key==='Escape'){tip.classList.remove('show');}});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>Tooltip — 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}}
.panel{position:relative;width:min(380px,100%);height:240px;border:1px solid var(--line);border-radius:18px;background:var(--surface)}
.t{position:absolute;height:38px;padding:0 16px;border-radius:10px;border:1px solid var(--line);background:var(--surface-2);color:var(--text);font:inherit;font-size:14px;cursor:pointer}
.t:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.tl{left:8px;bottom:50px}.tc{left:50%;margin-left:-35px;bottom:50px}.tr{right:8px;bottom:50px}.tt{left:50%;margin-left:-28px;top:6px}
.tip{position:absolute;z-index:5;max-width:200px;padding:6px 10px;border-radius:8px;background:var(--text);color:var(--bg);font-size:12.5px;font-weight:600;line-height:1.3;opacity:0;pointer-events:auto;transition:opacity .14s ease-out,transform .14s ease-out;transform:translateY(4px)}
.tip.below{transform:translateY(-4px)}
.tip.show{opacity:1;transform:none}
.tip::after{content:"";position:absolute;left:var(--ax,50%);bottom:-4px;width:8px;height:8px;background:inherit;transform:translateX(-50%) rotate(45deg)}
.tip.below::after{bottom:auto;top:-4px}
@media (prefers-reduced-motion:reduce){.tip,.tip.below{transform:none;transition:opacity .14s}}
</style></head>
<body><div class="stage">
<div class="panel" id="panel">
<button class="t tl" data-tip="Left edge — clamped inside">Left</button>
<button class="t tc" data-tip="Centred above the button">Centre</button>
<button class="t tr" data-tip="Right edge — clamped inside">Right</button>
<button class="t tt" data-tip="No room above, so I flip below">Top</button>
</div>
</div>
<script>
const panel=document.getElementById('panel');let tip,timer,hideTimer,cur;
tip=document.createElement('div');tip.className='tip';tip.role='tooltip';tip.id='tip';panel.append(tip);
tip.addEventListener('mouseenter',()=>clearTimeout(hideTimer));tip.addEventListener('mouseleave',hide);
function place(el){
tip.textContent=el.dataset.tip;tip.classList.remove('below');
const p=panel.getBoundingClientRect(),r=el.getBoundingClientRect(),w=tip.offsetWidth,h=tip.offsetHeight;
let left=r.left-p.left+r.width/2-w/2,top=r.top-p.top-h-8;
if(top<4){top=r.bottom-p.top+8;tip.classList.add('below');}
const cl=Math.max(4,Math.min(p.width-w-4,left));tip.style.left=cl+'px';tip.style.top=top+'px';
tip.style.setProperty('--ax',(r.left-p.left+r.width/2-cl)+'px');
}
function show(el,delay){clearTimeout(hideTimer);clearTimeout(timer);timer=setTimeout(()=>{cur=el;el.setAttribute('aria-describedby','tip');place(el);tip.classList.add('show');},delay);}
function hide(){clearTimeout(timer);hideTimer=setTimeout(()=>{tip.classList.remove('show');if(cur)cur.removeAttribute('aria-describedby');},100);}
panel.querySelectorAll('.t').forEach(b=>{
b.addEventListener('mouseenter',()=>show(b,400));b.addEventListener('mouseleave',hide);
b.addEventListener('focus',()=>show(b,0));b.addEventListener('blur',hide);
});
document.addEventListener('keydown',e=>{if(e.key==='Escape'){tip.classList.remove('show');}});
</script>
</body></html>