Tag Input with Chips
Type a word and press Enter to make a chip; Backspace removes the last chip and duplicates are rejected.
AI prompt
Create a tag input field that holds up to five tags. Typing text and pressing Enter or comma adds a rounded chip with the text and a small X button; Backspace on an empty input removes the last chip; clicking X removes that chip; pasting "design, motion, ui" creates three chips. Duplicate tags are rejected with the inline message "Already added", and at five tags the input is disabled with "Limit reached (5)". Show a live counter "2 of 5". The field looks like a normal text input (1 pixel border, 10 pixel radius) with chips wrapping inside; focus shows a 2 pixel outline. Chips have aria-labels "Remove design", messages use aria-live polite. 150 millisecond scale-in with prefers-reduced-motion fallback. Plain HTML, CSS and JavaScript.About this pattern
Definition
A tag input turns typed words into removable chips inside one field. It collects several short values, such as keywords or recipients, without a separate list control.
Purpose & UX principles
- Use for tags, keywords, emails and skills where users add several short items.
- Add on Enter or comma, remove with Backspace on empty input or the chip's X; support paste of comma-separated lists.
- Validate gently: reject duplicates and over-limit with a short inline message and keep focus.
Interaction states
- Empty with placeholder, with chips, focused, at limit (5 tags), duplicate warning.
Motion specification
- A new chip scales in from 0.8 over 150ms; removed chips fade over 120ms.
- Reduced motion: chips appear and disappear instantly.
Responsive behaviour
- Chips wrap onto new lines inside the field; the input keeps a minimum width of 80px.
- Chip remove buttons must be at least 24px for touch.
Code
<div class="bx"><label for="in" class="lb">Keywords</label><div class="fld" id="fld"><input id="in" placeholder="Add a keyword…" autocomplete="off"></div><div class="meta"><span id="msg" aria-live="polite"></span><span id="cnt">0 of 5</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}
.bx{width:min(320px,100%);display:grid;gap:6px}
.lb{font-size:12px;font-weight:600}
.fld{display:flex;flex-wrap:wrap;gap:6px;padding:7px;min-height:44px;border-radius:10px;background:var(--surface);border:1px solid var(--line);cursor:text}
.fld:focus-within{outline:2px solid var(--accent);outline-offset:1px}
.fld input{flex:1;min-width:80px;border:0;outline:none;background:none;font-size:13.5px;height:28px}
.chip{display:flex;align-items:center;gap:2px;height:28px;padding:0 4px 0 10px;border-radius:99px;background:#ecece8;font-size:12.5px;animation:pop .15s ease-out}
@keyframes pop{from{opacity:0;transform:scale(.8)}}
.chip.out{opacity:0;transition:opacity .12s}
.chip button{width:22px;height:22px;border:0;border-radius:50%;background:none;cursor:pointer;font-size:14px;color:#555}.chip button:hover{background:rgba(0,0,0,.08)}
.meta{display:flex;justify-content:space-between;font-size:11.5px;color:var(--muted)}#msg.bad{color:var(--danger)}
button:focus-visible{outline:2px solid var(--accent);outline-offset:1px}
@media (prefers-reduced-motion:reduce){.chip{animation:none}.chip.out{transition:none}}const fld=document.getElementById('fld'),inp=document.getElementById('in'),msg=document.getElementById('msg'),cnt=document.getElementById('cnt'),MAX=5,tags=[];
function note(t,bad){msg.textContent=t;msg.className=bad?'bad':'';}
function sync(){cnt.textContent=tags.length+' of '+MAX;inp.disabled=tags.length>=MAX;inp.placeholder=tags.length>=MAX?'Limit reached (5)':'Add a keyword…';}
function add(v){v=v.trim();if(!v)return;if(tags.map(t=>t.toLowerCase()).includes(v.toLowerCase())){note('Already added',1);return;}if(tags.length>=MAX){note('Limit reached (5)',1);return;}
tags.push(v);const c=document.createElement('span');c.className='chip';const s=document.createElement('span');s.textContent=v;const b=document.createElement('button');b.type='button';b.textContent='×';b.setAttribute('aria-label','Remove '+v);
b.addEventListener('click',()=>rm(v,c));c.append(s,b);fld.insertBefore(c,inp);note('');sync();}
function rm(v,c){const i=tags.indexOf(v);if(i<0)return;tags.splice(i,1);c.classList.add('out');setTimeout(()=>c.remove(),matchMedia('(prefers-reduced-motion: reduce)').matches?0:120);sync();note('');inp.focus();}
inp.addEventListener('keydown',e=>{if(e.key==='Enter'||e.key===','){e.preventDefault();add(inp.value);inp.value='';}else if(e.key==='Backspace'&&!inp.value&&tags.length){const cs=fld.querySelectorAll('.chip:not(.out)');rm(tags[tags.length-1],cs[cs.length-1]);}else note('');});
inp.addEventListener('paste',e=>{const t=(e.clipboardData||window.clipboardData).getData('text');if(t.includes(',')){e.preventDefault();t.split(',').forEach(add);}});
fld.addEventListener('click',e=>{if(e.target===fld)inp.focus();});sync();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>Tag Input with Chips — 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(320px,100%);display:grid;gap:6px}
.lb{font-size:12px;font-weight:600}
.fld{display:flex;flex-wrap:wrap;gap:6px;padding:7px;min-height:44px;border-radius:10px;background:var(--surface);border:1px solid var(--line);cursor:text}
.fld:focus-within{outline:2px solid var(--accent);outline-offset:1px}
.fld input{flex:1;min-width:80px;border:0;outline:none;background:none;font-size:13.5px;height:28px}
.chip{display:flex;align-items:center;gap:2px;height:28px;padding:0 4px 0 10px;border-radius:99px;background:#ecece8;font-size:12.5px;animation:pop .15s ease-out}
@keyframes pop{from{opacity:0;transform:scale(.8)}}
.chip.out{opacity:0;transition:opacity .12s}
.chip button{width:22px;height:22px;border:0;border-radius:50%;background:none;cursor:pointer;font-size:14px;color:#555}.chip button:hover{background:rgba(0,0,0,.08)}
.meta{display:flex;justify-content:space-between;font-size:11.5px;color:var(--muted)}#msg.bad{color:var(--danger)}
button:focus-visible{outline:2px solid var(--accent);outline-offset:1px}
@media (prefers-reduced-motion:reduce){.chip{animation:none}.chip.out{transition:none}}
</style></head>
<body><div class="stage">
<div class="bx"><label for="in" class="lb">Keywords</label><div class="fld" id="fld"><input id="in" placeholder="Add a keyword…" autocomplete="off"></div><div class="meta"><span id="msg" aria-live="polite"></span><span id="cnt">0 of 5</span></div></div>
</div>
<script>
const fld=document.getElementById('fld'),inp=document.getElementById('in'),msg=document.getElementById('msg'),cnt=document.getElementById('cnt'),MAX=5,tags=[];
function note(t,bad){msg.textContent=t;msg.className=bad?'bad':'';}
function sync(){cnt.textContent=tags.length+' of '+MAX;inp.disabled=tags.length>=MAX;inp.placeholder=tags.length>=MAX?'Limit reached (5)':'Add a keyword…';}
function add(v){v=v.trim();if(!v)return;if(tags.map(t=>t.toLowerCase()).includes(v.toLowerCase())){note('Already added',1);return;}if(tags.length>=MAX){note('Limit reached (5)',1);return;}
tags.push(v);const c=document.createElement('span');c.className='chip';const s=document.createElement('span');s.textContent=v;const b=document.createElement('button');b.type='button';b.textContent='×';b.setAttribute('aria-label','Remove '+v);
b.addEventListener('click',()=>rm(v,c));c.append(s,b);fld.insertBefore(c,inp);note('');sync();}
function rm(v,c){const i=tags.indexOf(v);if(i<0)return;tags.splice(i,1);c.classList.add('out');setTimeout(()=>c.remove(),matchMedia('(prefers-reduced-motion: reduce)').matches?0:120);sync();note('');inp.focus();}
inp.addEventListener('keydown',e=>{if(e.key==='Enter'||e.key===','){e.preventDefault();add(inp.value);inp.value='';}else if(e.key==='Backspace'&&!inp.value&&tags.length){const cs=fld.querySelectorAll('.chip:not(.out)');rm(tags[tags.length-1],cs[cs.length-1]);}else note('');});
inp.addEventListener('paste',e=>{const t=(e.clipboardData||window.clipboardData).getData('text');if(t.includes(',')){e.preventDefault();t.split(',').forEach(add);}});
fld.addEventListener('click',e=>{if(e.target===fld)inp.focus();});sync();
</script>
</body></html>