Pagination
Page controls with previous and next, numbered pages and smart ellipsis.
AI prompt
Build a pagination control for 20 pages inside a nav with aria-label="Pagination". Render Previous and Next buttons and numbered buttons computed by a function that always shows page 1, the last page, the current page and its two neighbours on each side, inserting a non-interactive ellipsis where numbers are skipped. The current page is a filled accent chip with aria-current="page". Disable Previous on the first page and Next on the last without removing them. Above the control show a mock list of five items that changes with the page, fading in over 140ms. Add a visually hidden aria-live="polite" region that announces "Page N of 20". On widths under 480px hide the numbered buttons and show "Page N of 20" text between Previous and Next. Minimum target size 40px. Honour prefers-reduced-motion.About this pattern
Definition
A set of controls for moving between pages of a long list: previous and next buttons, the first and last page, and a window of numbers around the current page, with ellipses for the skipped ranges.
Purpose & UX principles
- Splits a long list into pages the reader can reason about, and lets them jump to a known position.
- Always show the current page, the first, the last, and one or two neighbours of the current page.
- Disabled previous on page 1 and next on the last page, but keep them in place so the layout does not move.
Interaction states
- Current page: filled accent chip with
aria-current="page". - Disabled buttons: reduced opacity,
disabledattribute. - Hover: surface lightens. Focus-visible: ring.
- Live region announces "Page N of M" after each change.
Motion specification
- Chip fill: 180ms background transition.
- List content swaps with a 140ms fade.
- Reduced motion: no fade.
Responsive behaviour
- Phones: show previous, next and a text status such as "Page 4 of 20"; drop the number buttons.
- Touch targets stay at least 40px.
Code
<div class="pg">
<ul class="list" id="list" aria-live="off"></ul>
<nav aria-label="Pagination" class="nav">
<button id="prev" aria-label="Previous page">‹</button>
<div class="nums" id="nums"></div>
<span class="status" id="status"></span>
<button id="next" aria-label="Next page">›</button>
</nav>
<p class="sr" 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}
.pg{width:min(480px,100%);border:1px solid var(--line);border-radius:18px;background:var(--surface);padding:20px}
.list{list-style:none;display:grid;gap:8px;margin-bottom:18px;animation:in .14s ease-out}
.list li{padding:12px 14px;border-radius:10px;background:var(--surface-2);font-size:14px;color:var(--muted)}
.nav{display:flex;align-items:center;justify-content:center;gap:6px}
.nav button{min-width:40px;height:40px;border-radius:10px;border:1px solid var(--line);background:none;color:var(--text);font:inherit;cursor:pointer;transition:background .18s}
.nav button:hover:not(:disabled){background:var(--surface-2)}
.nav button:disabled{opacity:.35;cursor:default}
.nav button[aria-current=page]{background:var(--accent);border-color:var(--accent);color:var(--accent-ink);font-weight:700}
.nav button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.nums{display:flex;gap:6px}.dots{display:grid;place-items:center;min-width:24px;color:var(--muted)}
.status{display:none;color:var(--muted);font-size:14px;padding:0 10px}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
@keyframes in{from{opacity:0}}
@media (max-width:480px){.nums{display:none}.status{display:block}}
@media (prefers-reduced-motion:reduce){.list{animation:none}.nav button{transition:none}}const TOTAL=20;let page=4;
const list=document.getElementById('list'),nums=document.getElementById('nums'),prev=document.getElementById('prev'),next=document.getElementById('next'),status=document.getElementById('status'),live=document.getElementById('live');
function range(){const s=new Set([1,TOTAL]);for(let i=page-2;i<=page+2;i++)if(i>0&&i<=TOTAL)s.add(i);return [...s].sort((a,b)=>a-b);}
function render(){
nums.innerHTML='';let last=0;
range().forEach(n=>{if(last&&n-last>1){const d=document.createElement('span');d.className='dots';d.textContent='…';d.setAttribute('aria-hidden','true');nums.append(d);}
const b=document.createElement('button');b.textContent=n;b.setAttribute('aria-label','Page '+n);if(n===page)b.setAttribute('aria-current','page');b.onclick=()=>go(n);nums.append(b);last=n;});
prev.disabled=page===1;next.disabled=page===TOTAL;status.textContent='Page '+page+' of '+TOTAL;
list.innerHTML='';list.style.animation='none';list.offsetHeight;list.style.animation='';
for(let i=1;i<=5;i++){const li=document.createElement('li');li.textContent='Result '+((page-1)*5+i);list.append(li);}
live.textContent='Page '+page+' of '+TOTAL;
}
function go(n){page=Math.min(TOTAL,Math.max(1,n));render();}
prev.onclick=()=>go(page-1);next.onclick=()=>go(page+1);render();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>Pagination — 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}}
.pg{width:min(480px,100%);border:1px solid var(--line);border-radius:18px;background:var(--surface);padding:20px}
.list{list-style:none;display:grid;gap:8px;margin-bottom:18px;animation:in .14s ease-out}
.list li{padding:12px 14px;border-radius:10px;background:var(--surface-2);font-size:14px;color:var(--muted)}
.nav{display:flex;align-items:center;justify-content:center;gap:6px}
.nav button{min-width:40px;height:40px;border-radius:10px;border:1px solid var(--line);background:none;color:var(--text);font:inherit;cursor:pointer;transition:background .18s}
.nav button:hover:not(:disabled){background:var(--surface-2)}
.nav button:disabled{opacity:.35;cursor:default}
.nav button[aria-current=page]{background:var(--accent);border-color:var(--accent);color:var(--accent-ink);font-weight:700}
.nav button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.nums{display:flex;gap:6px}.dots{display:grid;place-items:center;min-width:24px;color:var(--muted)}
.status{display:none;color:var(--muted);font-size:14px;padding:0 10px}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
@keyframes in{from{opacity:0}}
@media (max-width:480px){.nums{display:none}.status{display:block}}
@media (prefers-reduced-motion:reduce){.list{animation:none}.nav button{transition:none}}
</style></head>
<body><div class="stage">
<div class="pg">
<ul class="list" id="list" aria-live="off"></ul>
<nav aria-label="Pagination" class="nav">
<button id="prev" aria-label="Previous page">‹</button>
<div class="nums" id="nums"></div>
<span class="status" id="status"></span>
<button id="next" aria-label="Next page">›</button>
</nav>
<p class="sr" id="live" aria-live="polite"></p>
</div>
</div>
<script>
const TOTAL=20;let page=4;
const list=document.getElementById('list'),nums=document.getElementById('nums'),prev=document.getElementById('prev'),next=document.getElementById('next'),status=document.getElementById('status'),live=document.getElementById('live');
function range(){const s=new Set([1,TOTAL]);for(let i=page-2;i<=page+2;i++)if(i>0&&i<=TOTAL)s.add(i);return [...s].sort((a,b)=>a-b);}
function render(){
nums.innerHTML='';let last=0;
range().forEach(n=>{if(last&&n-last>1){const d=document.createElement('span');d.className='dots';d.textContent='…';d.setAttribute('aria-hidden','true');nums.append(d);}
const b=document.createElement('button');b.textContent=n;b.setAttribute('aria-label','Page '+n);if(n===page)b.setAttribute('aria-current','page');b.onclick=()=>go(n);nums.append(b);last=n;});
prev.disabled=page===1;next.disabled=page===TOTAL;status.textContent='Page '+page+' of '+TOTAL;
list.innerHTML='';list.style.animation='none';list.offsetHeight;list.style.animation='';
for(let i=1;i<=5;i++){const li=document.createElement('li');li.textContent='Result '+((page-1)*5+i);list.append(li);}
live.textContent='Page '+page+' of '+TOTAL;
}
function go(n){page=Math.min(TOTAL,Math.max(1,n));render();}
prev.onclick=()=>go(page-1);next.onclick=()=>go(page+1);render();
</script>
</body></html>