Notification Bell with Badge
A bell icon that rings and pops a count badge when something new arrives.
AI prompt
Create a bell icon button 44px wide with a 20px red circular badge on its top right showing an unread count, plus a "Simulate notification" text button. When a notification arrives, increment the count (cap the display at "9+"), play a bell swing animation by rotating the bell icon around its top centre through -14, 12, -8, 5 and 0 degrees over 700ms ease-in-out, and pop the badge with a scale 0 to 1.2 to 1 over 260ms. Update the button's aria-label to "Notifications, N unread" and announce "New notification" in a polite live region. Clicking the bell clears the count, removes the badge and sets the label to "Notifications". Retrigger the animation reliably by removing and re-adding the class. Show a 2px focus-visible ring. Under prefers-reduced-motion remove the swing and use a 150ms badge fade.About this pattern
Definition
An icon button with a small number badge. When a new notification arrives the bell swings briefly and the badge pops to the new count; opening it clears the badge.
Purpose & UX principles
- Draws attention to new activity once, without looping animations.
- The badge number is part of the button's accessible name ("Notifications, 3 unread").
- The ring is short and happens only on arrival, so it does not become noise.
Interaction states
- No unread (no badge), unread (badge), ringing, open (badge cleared), focus-visible.
Motion specification
- Bell swing: rotate -14, 12, -8, 5, 0 degrees over 700ms ease-in-out, pivoting at the top centre.
- Badge pop: scale 0 to 1.2 to 1 in 260ms.
- Reduced motion: no swing; the badge appears with a 150ms fade.
Responsive behaviour
- 44px button; the badge scales with the icon and caps at "9+".
Code
<div class="wrap">
<button class="bell" id="bell" aria-label="Notifications"><svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true"><path d="M6 17V11a6 6 0 0 1 12 0v6l1.500 2h-15zM10 21h4"/></svg><b class="bd" id="bd" hidden>0</b></button>
<button class="sim" id="sim" type="button">Simulate notification</button>
<p class="live" id="live" aria-live="polite"></p>
</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}
.wrap{display:grid;justify-items:center;gap:12px}
.bell{position:relative;width:48px;height:48px;border-radius:14px;border:1px solid var(--line);background:var(--surface);cursor:pointer;display:grid;place-items:center;transition:background .15s}
.bell:hover{background:var(--bg)}
.bell:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
.bell svg{fill:none;stroke:var(--text);stroke-width:1.8;stroke-linecap:round;stroke-linejoin:round;transform-origin:50% 8%}
.bell.ring svg{animation:ring .7s ease-in-out}
@keyframes ring{0%{transform:rotate(0)}20%{transform:rotate(-14deg)}40%{transform:rotate(12deg)}60%{transform:rotate(-8deg)}80%{transform:rotate(5deg)}100%{transform:rotate(0)}}
.bd{position:absolute;right:-6px;top:-6px;min-width:20px;height:20px;padding:0 5px;border-radius:10px;background:#e11d48;color:#fff;font-size:11px;font-weight:700;display:grid;place-items:center}
.bd.pop{animation:bp .26s ease-out}
.bd[hidden]{display:none}
@keyframes bp{0%{transform:scale(0)}70%{transform:scale(1.2)}100%{transform:scale(1)}}
.sim{height:32px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12.5px;cursor:pointer}
.sim:hover{border-color:var(--accent)}
.sim:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.live{font-size:12px;color:var(--muted);min-height:16px}
@media (prefers-reduced-motion:reduce){.bell.ring svg{animation:none}.bd.pop{animation:fd .15s}@keyframes fd{from{opacity:0}}}const bell=document.getElementById('bell'),bd=document.getElementById('bd'),live=document.getElementById('live');let n=0;
function show(){bd.hidden=n===0;bd.textContent=n>9?'9+':n;bell.setAttribute('aria-label',n?'Notifications, '+n+' unread':'Notifications');}
document.getElementById('sim').addEventListener('click',()=>{n++;show();live.textContent='New notification';
[[bell,'ring'],[bd,'pop']].forEach(([el,c])=>{el.classList.remove(c);void el.offsetWidth;el.classList.add(c);});});
bell.addEventListener('click',()=>{n=0;show();live.textContent='';});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>Notification Bell with Badge — 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}}
.wrap{display:grid;justify-items:center;gap:12px}
.bell{position:relative;width:48px;height:48px;border-radius:14px;border:1px solid var(--line);background:var(--surface);cursor:pointer;display:grid;place-items:center;transition:background .15s}
.bell:hover{background:var(--bg)}
.bell:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
.bell svg{fill:none;stroke:var(--text);stroke-width:1.8;stroke-linecap:round;stroke-linejoin:round;transform-origin:50% 8%}
.bell.ring svg{animation:ring .7s ease-in-out}
@keyframes ring{0%{transform:rotate(0)}20%{transform:rotate(-14deg)}40%{transform:rotate(12deg)}60%{transform:rotate(-8deg)}80%{transform:rotate(5deg)}100%{transform:rotate(0)}}
.bd{position:absolute;right:-6px;top:-6px;min-width:20px;height:20px;padding:0 5px;border-radius:10px;background:#e11d48;color:#fff;font-size:11px;font-weight:700;display:grid;place-items:center}
.bd.pop{animation:bp .26s ease-out}
.bd[hidden]{display:none}
@keyframes bp{0%{transform:scale(0)}70%{transform:scale(1.2)}100%{transform:scale(1)}}
.sim{height:32px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12.5px;cursor:pointer}
.sim:hover{border-color:var(--accent)}
.sim:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.live{font-size:12px;color:var(--muted);min-height:16px}
@media (prefers-reduced-motion:reduce){.bell.ring svg{animation:none}.bd.pop{animation:fd .15s}@keyframes fd{from{opacity:0}}}
</style></head>
<body><div class="stage">
<div class="wrap">
<button class="bell" id="bell" aria-label="Notifications"><svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true"><path d="M6 17V11a6 6 0 0 1 12 0v6l1.500 2h-15zM10 21h4"/></svg><b class="bd" id="bd" hidden>0</b></button>
<button class="sim" id="sim" type="button">Simulate notification</button>
<p class="live" id="live" aria-live="polite"></p>
</div>
</div>
<script>
const bell=document.getElementById('bell'),bd=document.getElementById('bd'),live=document.getElementById('live');let n=0;
function show(){bd.hidden=n===0;bd.textContent=n>9?'9+':n;bell.setAttribute('aria-label',n?'Notifications, '+n+' unread':'Notifications');}
document.getElementById('sim').addEventListener('click',()=>{n++;show();live.textContent='New notification';
[[bell,'ring'],[bd,'pop']].forEach(([el,c])=>{el.classList.remove(c);void el.offsetWidth;el.classList.add(c);});});
bell.addEventListener('click',()=>{n=0;show();live.textContent='';});
</script>
</body></html>