Toast
Short, non-blocking messages that stack, auto-dismiss and pause on hover.
AI prompt
Build a toast system. A fixed container at bottom-right (bottom-centre under 480px) with aria-live="polite" holds toasts. A function toast(message, type) appends a card with an icon (check for success, ! for error, i for neutral), the message, and a close button with aria-label "Dismiss". Each toast animates in with translateY(16px) and fade over 220ms, shows a 2px progress line that shrinks over 4000ms, auto-dismisses at the end, and pauses (timer and progress) while hovered or focused. Closing animates opacity and collapses the height in 200ms before removing the node. Limit to three visible toasts; remove the oldest when exceeded. Provide three buttons in the demo to trigger each type. Use role="status" for neutral and success, role="alert" for errors. Under prefers-reduced-motion use opacity only.About this pattern
Definition
A small message that appears briefly at the edge of the screen to report the result of an action, then disappears by itself. Several toasts stack without covering each other.
Purpose & UX principles
- Confirms outcomes that do not need a decision ("Link copied").
- It must never be the only place an important error is shown, because it disappears. Use inline messages for problems that need fixing.
- Announce it to screen readers with a polite live region so it is heard without stealing focus.
Interaction states
- Entering, visible, paused (on hover or focus), leaving.
- Variants: success, error and neutral, each with an icon so colour is not the only cue.
- Close button always available; auto-dismiss after 4 seconds, with a progress line showing time left.
Motion specification
- Enter: translateY(16px) and opacity 0 to 1, 220ms ease-out.
- Exit: opacity to 0 and height collapse, 200ms.
- Progress line: linear over 4000ms; paused with the timer on hover.
- Reduced motion: opacity only.
Responsive behaviour
- Desktop: bottom-right stack, 320px wide.
- Phones: full-width minus 16px margins at the top or bottom, away from the on-screen keyboard.
Code
<div class="demo"><button class="b ok">Success</button><button class="b er">Error</button><button class="b nu">Info</button></div>
<div class="toasts" id="toasts" aria-live="polite"></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}
.demo{display:flex;gap:10px}
.b{height:42px;padding:0 18px;border-radius:12px;border:1px solid var(--line);background:var(--surface-2);color:var(--text);font:inherit;font-weight:600;cursor:pointer}
.b:focus-visible,.x:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.toasts{position:fixed;right:16px;bottom:16px;display:flex;flex-direction:column;gap:10px;width:min(320px,calc(100vw - 32px));z-index:9}
.t{position:relative;display:flex;gap:12px;align-items:center;padding:14px 14px 16px;border-radius:14px;background:var(--surface-2);border:1px solid var(--line);box-shadow:0 10px 26px rgba(0,0,0,.12);overflow:hidden;animation:in .22s ease-out;transition:opacity .2s,transform .2s}
.t.out{opacity:0;transform:translateX(12px)}
.ico{flex:none;width:24px;height:24px;border-radius:50%;display:grid;place-items:center;font-size:13px;font-weight:800;color:var(--accent-ink)}
.success .ico{background:var(--accent)}.error .ico{background:var(--danger)}.info .ico{background:#2563eb}
.t p{flex:1;font-size:14px}
.x{background:none;border:0;color:var(--muted);font-size:18px;cursor:pointer;border-radius:6px}
.bar{position:absolute;left:0;bottom:0;height:2px;background:var(--accent);width:100%;transform-origin:left;animation:bar 4s linear forwards}
.error .bar{background:var(--danger)}.info .bar{background:#2563eb}
.t.paused .bar{animation-play-state:paused}
@keyframes in{from{opacity:0;transform:translateY(16px)}}
@keyframes bar{to{transform:scaleX(0)}}
@media (max-width:480px){.toasts{right:50%;transform:translateX(50%)}}
@media (prefers-reduced-motion:reduce){.t{animation:opacity .01s}.t.out{transform:none}}const box=document.getElementById('toasts'),icons={success:'✓',error:'!',info:'i'};
function toast(msg,type){
while(box.children.length>=3)box.firstChild.remove();
const t=document.createElement('div');t.className='t '+type;t.setAttribute('role',type==='error'?'alert':'status');
t.innerHTML='<span class="ico" aria-hidden="true">'+icons[type]+'</span><p></p><button class="x" aria-label="Dismiss">×</button><i class="bar"></i>';
t.querySelector('p').textContent=msg;box.append(t);
let left=4000,start=Date.now(),timer;
const run=()=>{start=Date.now();timer=setTimeout(close,left);},close=()=>{clearTimeout(timer);t.classList.add('out');setTimeout(()=>t.remove(),200);};
const pause=()=>{clearTimeout(timer);left-=Date.now()-start;t.classList.add('paused');},resume=()=>{t.classList.remove('paused');run();};
t.addEventListener('mouseenter',pause);t.addEventListener('mouseleave',resume);t.addEventListener('focusin',pause);t.addEventListener('focusout',resume);
t.querySelector('.x').addEventListener('click',close);run();
}
document.querySelector('.ok').onclick=()=>toast('Link copied to clipboard','success');
document.querySelector('.er').onclick=()=>toast('Could not save. Check your connection.','error');
document.querySelector('.nu').onclick=()=>toast('3 new items are available','info');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>Toast — 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}}
.demo{display:flex;gap:10px}
.b{height:42px;padding:0 18px;border-radius:12px;border:1px solid var(--line);background:var(--surface-2);color:var(--text);font:inherit;font-weight:600;cursor:pointer}
.b:focus-visible,.x:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.toasts{position:fixed;right:16px;bottom:16px;display:flex;flex-direction:column;gap:10px;width:min(320px,calc(100vw - 32px));z-index:9}
.t{position:relative;display:flex;gap:12px;align-items:center;padding:14px 14px 16px;border-radius:14px;background:var(--surface-2);border:1px solid var(--line);box-shadow:0 10px 26px rgba(0,0,0,.12);overflow:hidden;animation:in .22s ease-out;transition:opacity .2s,transform .2s}
.t.out{opacity:0;transform:translateX(12px)}
.ico{flex:none;width:24px;height:24px;border-radius:50%;display:grid;place-items:center;font-size:13px;font-weight:800;color:var(--accent-ink)}
.success .ico{background:var(--accent)}.error .ico{background:var(--danger)}.info .ico{background:#2563eb}
.t p{flex:1;font-size:14px}
.x{background:none;border:0;color:var(--muted);font-size:18px;cursor:pointer;border-radius:6px}
.bar{position:absolute;left:0;bottom:0;height:2px;background:var(--accent);width:100%;transform-origin:left;animation:bar 4s linear forwards}
.error .bar{background:var(--danger)}.info .bar{background:#2563eb}
.t.paused .bar{animation-play-state:paused}
@keyframes in{from{opacity:0;transform:translateY(16px)}}
@keyframes bar{to{transform:scaleX(0)}}
@media (max-width:480px){.toasts{right:50%;transform:translateX(50%)}}
@media (prefers-reduced-motion:reduce){.t{animation:opacity .01s}.t.out{transform:none}}
</style></head>
<body><div class="stage">
<div class="demo"><button class="b ok">Success</button><button class="b er">Error</button><button class="b nu">Info</button></div>
<div class="toasts" id="toasts" aria-live="polite"></div>
</div>
<script>
const box=document.getElementById('toasts'),icons={success:'✓',error:'!',info:'i'};
function toast(msg,type){
while(box.children.length>=3)box.firstChild.remove();
const t=document.createElement('div');t.className='t '+type;t.setAttribute('role',type==='error'?'alert':'status');
t.innerHTML='<span class="ico" aria-hidden="true">'+icons[type]+'</span><p></p><button class="x" aria-label="Dismiss">×</button><i class="bar"></i>';
t.querySelector('p').textContent=msg;box.append(t);
let left=4000,start=Date.now(),timer;
const run=()=>{start=Date.now();timer=setTimeout(close,left);},close=()=>{clearTimeout(timer);t.classList.add('out');setTimeout(()=>t.remove(),200);};
const pause=()=>{clearTimeout(timer);left-=Date.now()-start;t.classList.add('paused');},resume=()=>{t.classList.remove('paused');run();};
t.addEventListener('mouseenter',pause);t.addEventListener('mouseleave',resume);t.addEventListener('focusin',pause);t.addEventListener('focusout',resume);
t.querySelector('.x').addEventListener('click',close);run();
}
document.querySelector('.ok').onclick=()=>toast('Link copied to clipboard','success');
document.querySelector('.er').onclick=()=>toast('Could not save. Check your connection.','error');
document.querySelector('.nu').onclick=()=>toast('3 new items are available','info');
</script>
</body></html>