Lightbox Image Viewer
A thumbnail grid opens a dark overlay viewer with previous, next, a counter, Escape to close and arrow-key navigation.
AI prompt
Clean UI mockup of an image lightbox: a near-black translucent backdrop over a photo grid, one large landscape photo centred with rounded corners, a circular close button at the top right, circular previous and next arrow buttons at the vertical middle on both sides, a small counter 3 / 6 below the image in white, flat modern style. Variants: the closed state with the thumbnail grid, and the first image with the previous button disabled. Negative: heavy chrome, text errors, watermarks, clutter. Reduced motion: static design.About this pattern
Definition
A lightbox shows one image large over a dimmed page. It needs four things: a clear close button, previous and next controls that also work with arrow keys, a counter so people know where they are, and focus that stays inside until it closes.
Purpose & UX principles
- Open from the thumbnail that was activated and return focus to it on close.
- Arrow keys move between images; Escape and a click on the backdrop close.
- Preload the neighbours so moving feels instant.
Interaction states
- Six thumbnails, a viewer with Previous, Next and Close, and a counter.
Motion specification
- The backdrop fades in over 150ms and images cross-fade over 200ms.
- Reduced motion: no fades.
Responsive behaviour
- On touch, add swipe; keep controls at least 44px.
- Give every image a text description.
Code
<div class="bx" id="bx"><div class="gr" id="gr"></div><div class="lb" id="lb" role="dialog" aria-modal="true" aria-label="Image viewer" hidden><button class="x" id="cx" type="button" aria-label="Close">×</button><button class="n p" id="pv" type="button" aria-label="Previous image">‹</button><div class="bg" id="bg"></div><button class="n q" id="nx" type="button" aria-label="Next image">›</button><span class="ct" id="ct"></span></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}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;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)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.bx{position:relative}.gr{display:grid;grid-template-columns:repeat(3,1fr);gap:6px}.gr button{aspect-ratio:4/3;border-radius:8px;border:0;cursor:zoom-in}
.lb{position:absolute;inset:0;border-radius:12px;background:rgba(10,10,12,.88);display:grid;place-items:center;animation:fi .15s;z-index:5}.lb[hidden]{display:none}@keyframes fi{from{opacity:0}}
.bg{width:76%;height:76%;border-radius:10px;transition:background .2s}.x,.n{position:absolute;width:34px;height:34px;border-radius:50%;background:rgba(255,255,255,.14);color:#fff;font-size:22px;line-height:1;display:grid;place-items:center}.x:hover,.n:hover{background:rgba(255,255,255,.28)}.x{right:8px;top:8px}.n{top:50%;margin-top:-17px}.p{left:8px}.q{right:8px}.n:disabled{opacity:.3;cursor:default}
.ct{position:absolute;bottom:8px;color:#fff;font-size:12px}.lb button:focus-visible{outline:2px solid #fff}
@media (prefers-reduced-motion:reduce){.lb{animation:none}.bg{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const C=['linear-gradient(135deg,#f4c9a0,#c9748a)','linear-gradient(135deg,#8ec6e8,#3f6fb0)','linear-gradient(135deg,#bfe0b0,#3f8a5a)','linear-gradient(135deg,#f2d37a,#d98a3a)','linear-gradient(135deg,#d7c4f0,#7a5ac0)','linear-gradient(135deg,#9fe0d6,#2f8a86)'],gr=document.getElementById('gr'),lb=document.getElementById('lb'),bg=document.getElementById('bg'),pv=document.getElementById('pv'),nx=document.getElementById('nx'),ct=document.getElementById('ct');let i=0,from=null;
gr.innerHTML=C.map((c,k)=>'<button type="button" style="background:'+c+'" aria-label="Open photo '+(k+1)+' of 6"></button>').join('');
function show(){bg.style.background=C[i];ct.textContent=(i+1)+' / '+C.length;pv.disabled=i===0;nx.disabled=i===C.length-1;}
function open(k,el){i=k;from=el;lb.hidden=false;show();document.getElementById('cx').focus();}function close(){lb.hidden=true;if(from)from.focus();}
gr.addEventListener('click',e=>{const b=e.target.closest('button');if(b)open([...gr.children].indexOf(b),b);});
pv.addEventListener('click',()=>{if(i>0){i--;show();}});nx.addEventListener('click',()=>{if(i<C.length-1){i++;show();}});document.getElementById('cx').addEventListener('click',close);lb.addEventListener('click',e=>{if(e.target===lb)close();});
document.addEventListener('keydown',e=>{if(lb.hidden)return;if(e.key==='Escape')close();else if(e.key==='ArrowLeft'&&i>0){i--;show();}else if(e.key==='ArrowRight'&&i<C.length-1){i++;show();}else if(e.key==='Tab'){const f=[...lb.querySelectorAll('button:not(:disabled)')],a=document.activeElement,k=f.indexOf(a);if(e.shiftKey&&k<=0){e.preventDefault();f[f.length-1].focus();}else if(!e.shiftKey&&k===f.length-1){e.preventDefault();f[0].focus();}}});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>Lightbox Image Viewer — 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}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;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)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.bx{position:relative}.gr{display:grid;grid-template-columns:repeat(3,1fr);gap:6px}.gr button{aspect-ratio:4/3;border-radius:8px;border:0;cursor:zoom-in}
.lb{position:absolute;inset:0;border-radius:12px;background:rgba(10,10,12,.88);display:grid;place-items:center;animation:fi .15s;z-index:5}.lb[hidden]{display:none}@keyframes fi{from{opacity:0}}
.bg{width:76%;height:76%;border-radius:10px;transition:background .2s}.x,.n{position:absolute;width:34px;height:34px;border-radius:50%;background:rgba(255,255,255,.14);color:#fff;font-size:22px;line-height:1;display:grid;place-items:center}.x:hover,.n:hover{background:rgba(255,255,255,.28)}.x{right:8px;top:8px}.n{top:50%;margin-top:-17px}.p{left:8px}.q{right:8px}.n:disabled{opacity:.3;cursor:default}
.ct{position:absolute;bottom:8px;color:#fff;font-size:12px}.lb button:focus-visible{outline:2px solid #fff}
@media (prefers-reduced-motion:reduce){.lb{animation:none}.bg{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx" id="bx"><div class="gr" id="gr"></div><div class="lb" id="lb" role="dialog" aria-modal="true" aria-label="Image viewer" hidden><button class="x" id="cx" type="button" aria-label="Close">×</button><button class="n p" id="pv" type="button" aria-label="Previous image">‹</button><div class="bg" id="bg"></div><button class="n q" id="nx" type="button" aria-label="Next image">›</button><span class="ct" id="ct"></span></div></div>
</div>
<script>
const C=['linear-gradient(135deg,#f4c9a0,#c9748a)','linear-gradient(135deg,#8ec6e8,#3f6fb0)','linear-gradient(135deg,#bfe0b0,#3f8a5a)','linear-gradient(135deg,#f2d37a,#d98a3a)','linear-gradient(135deg,#d7c4f0,#7a5ac0)','linear-gradient(135deg,#9fe0d6,#2f8a86)'],gr=document.getElementById('gr'),lb=document.getElementById('lb'),bg=document.getElementById('bg'),pv=document.getElementById('pv'),nx=document.getElementById('nx'),ct=document.getElementById('ct');let i=0,from=null;
gr.innerHTML=C.map((c,k)=>'<button type="button" style="background:'+c+'" aria-label="Open photo '+(k+1)+' of 6"></button>').join('');
function show(){bg.style.background=C[i];ct.textContent=(i+1)+' / '+C.length;pv.disabled=i===0;nx.disabled=i===C.length-1;}
function open(k,el){i=k;from=el;lb.hidden=false;show();document.getElementById('cx').focus();}function close(){lb.hidden=true;if(from)from.focus();}
gr.addEventListener('click',e=>{const b=e.target.closest('button');if(b)open([...gr.children].indexOf(b),b);});
pv.addEventListener('click',()=>{if(i>0){i--;show();}});nx.addEventListener('click',()=>{if(i<C.length-1){i++;show();}});document.getElementById('cx').addEventListener('click',close);lb.addEventListener('click',e=>{if(e.target===lb)close();});
document.addEventListener('keydown',e=>{if(lb.hidden)return;if(e.key==='Escape')close();else if(e.key==='ArrowLeft'&&i>0){i--;show();}else if(e.key==='ArrowRight'&&i<C.length-1){i++;show();}else if(e.key==='Tab'){const f=[...lb.querySelectorAll('button:not(:disabled)')],a=document.activeElement,k=f.indexOf(a);if(e.shiftKey&&k<=0){e.preventDefault();f[f.length-1].focus();}else if(!e.shiftKey&&k===f.length-1){e.preventDefault();f[0].focus();}}});
</script>
</body></html>