Loader Variants
Four compact loaders (dots, bars, ring, pulse) with a switch that starts and stops loading and announces it.
AI prompt
Design a small loading indicator component with four variants chosen by a segmented control: three bouncing dots, five vertical bars scaling in a wave, a thin rotating ring, and a softly pulsing circle. Monochrome near-black on a light card, 32 pixel size, centred with a short "Loading" label beneath. A toggle switch starts and stops loading; when stopped, show a check icon and the text "Loaded". Use role status with an aria-live message, aria-busy on the container, and a prefers-reduced-motion version that blinks opacity slowly instead of moving. No external libraries; plain HTML, CSS and a few lines of JavaScript.About this pattern
Definition
A loader tells people the system is working. Dots suit chat and short waits, bars suit audio or data, rings suit general waiting, and pulses suit calm background activity.
Purpose & UX principles
- Use an indeterminate loader when the duration is unknown; use a progress bar when it is known.
- Show a loader only after about 300ms so quick actions do not flash; keep it visible for at least 400ms once shown.
- Always pair it with text for screen readers (role status) and stop it when the work finishes.
Interaction states
- Four buttons choose the variant; the switch turns loading on or off. While off, the area shows the loaded content.
Motion specification
- Dots bounce 1s loop with 150ms stagger; bars scale 0.9s loop; ring rotates 0.8s linear; pulse scales 1.4s ease-in-out.
- Reduced motion: loaders become a gentle opacity blink at a slow rate instead of moving shapes.
Responsive behaviour
- Loaders keep a fixed size; centre them in the area that is waiting.
- Place the label beside or under the loader, not inside it.
Code
<div class="bx"><div class="area" id="area" role="status" aria-live="polite"><div class="ld" id="ld"></div><p class="lb" id="lb">Loading</p></div>
<div class="pre" id="pre" role="group" aria-label="Loader variant"><button data-v="dots" aria-pressed="true">Dots</button><button data-v="bars" aria-pressed="false">Bars</button><button data-v="ring" aria-pressed="false">Ring</button><button data-v="pulse" aria-pressed="false">Pulse</button></div>
<label class="sw"><input id="on" type="checkbox" checked> Loading</label></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}
.bx{width:min(280px,100%);display:grid;gap:10px}
.area{height:120px;border-radius:12px;background:var(--surface);border:1px solid var(--line);display:grid;place-content:center;justify-items:center;gap:10px}
.lb{font-size:12px;color:var(--muted)}
.ld{height:32px;display:flex;align-items:center;gap:5px}
.ld i{display:block}
.dots i{width:9px;height:9px;border-radius:50%;background:var(--text);animation:bn 1s ease-in-out infinite;animation-delay:calc(var(--i) * 150ms)}
@keyframes bn{0%,80%,100%{transform:translateY(0);opacity:.4}40%{transform:translateY(-9px);opacity:1}}
.bars i{width:4px;height:26px;border-radius:2px;background:var(--text);animation:br .9s ease-in-out infinite;animation-delay:calc(var(--i) * 100ms)}
@keyframes br{0%,100%{transform:scaleY(.3)}50%{transform:scaleY(1)}}
.ring i{width:28px;height:28px;border-radius:50%;border:3px solid #d8d8d3;border-top-color:var(--text);animation:rg .8s linear infinite}
@keyframes rg{to{transform:rotate(360deg)}}
.pulse i{width:24px;height:24px;border-radius:50%;background:var(--text);animation:pl 1.4s ease-in-out infinite}
@keyframes pl{0%,100%{transform:scale(.55);opacity:.4}50%{transform:scale(1);opacity:1}}
.done{font-size:22px}
.pre{display:grid;grid-template-columns:repeat(4,1fr);gap:5px}.pre button{height:28px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:11.5px;cursor:pointer;padding:0}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.sw{display:flex;gap:6px;align-items:center;font-size:12px;color:var(--muted)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.ld i{animation:rm 1.6s ease-in-out infinite!important;transform:none!important}@keyframes rm{50%{opacity:.25}}}const area=document.getElementById('area'),ld=document.getElementById('ld'),lb=document.getElementById('lb'),on=document.getElementById('on'),bs=[...document.querySelectorAll('#pre button')];let v='dots';
const N={dots:3,bars:5,ring:1,pulse:1};
function draw(){area.setAttribute('aria-busy',String(on.checked));if(!on.checked){ld.className='ld done';ld.textContent='✓';lb.textContent='Loaded';return;}
ld.className='ld '+v;ld.innerHTML='';for(let i=0;i<N[v];i++){const e=document.createElement('i');e.style.setProperty('--i',i);ld.append(e);}lb.textContent='Loading';}
bs.forEach(b=>b.addEventListener('click',()=>{v=b.dataset.v;bs.forEach(x=>x.setAttribute('aria-pressed',String(x===b)));draw();}));on.addEventListener('change',draw);draw();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>Loader Variants — 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}}
.bx{width:min(280px,100%);display:grid;gap:10px}
.area{height:120px;border-radius:12px;background:var(--surface);border:1px solid var(--line);display:grid;place-content:center;justify-items:center;gap:10px}
.lb{font-size:12px;color:var(--muted)}
.ld{height:32px;display:flex;align-items:center;gap:5px}
.ld i{display:block}
.dots i{width:9px;height:9px;border-radius:50%;background:var(--text);animation:bn 1s ease-in-out infinite;animation-delay:calc(var(--i) * 150ms)}
@keyframes bn{0%,80%,100%{transform:translateY(0);opacity:.4}40%{transform:translateY(-9px);opacity:1}}
.bars i{width:4px;height:26px;border-radius:2px;background:var(--text);animation:br .9s ease-in-out infinite;animation-delay:calc(var(--i) * 100ms)}
@keyframes br{0%,100%{transform:scaleY(.3)}50%{transform:scaleY(1)}}
.ring i{width:28px;height:28px;border-radius:50%;border:3px solid #d8d8d3;border-top-color:var(--text);animation:rg .8s linear infinite}
@keyframes rg{to{transform:rotate(360deg)}}
.pulse i{width:24px;height:24px;border-radius:50%;background:var(--text);animation:pl 1.4s ease-in-out infinite}
@keyframes pl{0%,100%{transform:scale(.55);opacity:.4}50%{transform:scale(1);opacity:1}}
.done{font-size:22px}
.pre{display:grid;grid-template-columns:repeat(4,1fr);gap:5px}.pre button{height:28px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:11.5px;cursor:pointer;padding:0}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.sw{display:flex;gap:6px;align-items:center;font-size:12px;color:var(--muted)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.ld i{animation:rm 1.6s ease-in-out infinite!important;transform:none!important}@keyframes rm{50%{opacity:.25}}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="area" id="area" role="status" aria-live="polite"><div class="ld" id="ld"></div><p class="lb" id="lb">Loading</p></div>
<div class="pre" id="pre" role="group" aria-label="Loader variant"><button data-v="dots" aria-pressed="true">Dots</button><button data-v="bars" aria-pressed="false">Bars</button><button data-v="ring" aria-pressed="false">Ring</button><button data-v="pulse" aria-pressed="false">Pulse</button></div>
<label class="sw"><input id="on" type="checkbox" checked> Loading</label></div>
</div>
<script>
const area=document.getElementById('area'),ld=document.getElementById('ld'),lb=document.getElementById('lb'),on=document.getElementById('on'),bs=[...document.querySelectorAll('#pre button')];let v='dots';
const N={dots:3,bars:5,ring:1,pulse:1};
function draw(){area.setAttribute('aria-busy',String(on.checked));if(!on.checked){ld.className='ld done';ld.textContent='✓';lb.textContent='Loaded';return;}
ld.className='ld '+v;ld.innerHTML='';for(let i=0;i<N[v];i++){const e=document.createElement('i');e.style.setProperty('--i',i);ld.append(e);}lb.textContent='Loading';}
bs.forEach(b=>b.addEventListener('click',()=>{v=b.dataset.v;bs.forEach(x=>x.setAttribute('aria-pressed',String(x===b)));draw();}));on.addEventListener('change',draw);draw();
</script>
</body></html>