Inline Edit Text
A title that becomes an input when clicked: Enter or blur saves, Escape cancels, and an empty name is rejected.
AI prompt
Clean UI design of an editable project title: the heading Quarterly roadmap in bold with a small grey pencil icon at its right, below it a second state where the heading has turned into a text input with a dark one pixel border and the text selected in blue, a small hint Enter to save, Esc to cancel, light theme, flat modern style, plenty of white space. Variants: an error state with a red border and the message Name can't be empty. Negative: modal dialogs, heavy shadows, text errors, clutter. Reduced motion: static design.About this pattern
Definition
Inline editing lets people change a value where they see it instead of opening a form. The text looks plain until hovered, shows a pencil to hint it can change, turns into an input on click or Enter, and returns to text when saved or cancelled.
Purpose & UX principles
- Enter saves, Escape cancels, blur saves; select all text on entering edit mode.
- Validate before saving; show an error and keep editing if the value is empty.
- Use a real button around the text so it is focusable and can be opened with the keyboard.
Interaction states
- A project title you can click, plus a second field for a short description.
Motion specification
- The input appears instantly; the Saved message fades after 1.5s.
- Reduced motion: no fade.
Responsive behaviour
- Return focus to the text button after saving or cancelling.
- Announce Saved or the error in a status message.
Code
<div class="bx"><div class="fd"><span class="lb">Project name</span><span id="slot"></span></div><div class="fd"><span class="lb">Description</span><span id="slot2"></span></div><p class="out" id="out" role="status"></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}
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}
.fd{display:grid;gap:3px;margin-bottom:12px}.lb{font-size:11px;color:var(--muted);text-transform:uppercase;letter-spacing:.06em;font-weight:600}.ie{display:inline-flex;align-items:center;gap:8px;padding:4px 8px;margin-left:-8px;border-radius:8px;font-size:20px;font-weight:700;letter-spacing:-.01em;text-align:left}.ie.sm{font-size:14px;font-weight:400}.ie:hover{background:var(--pv)}.ie svg{opacity:0;transition:opacity .15s}.ie:hover svg,.ie:focus-visible svg{opacity:.6}
.ei{width:100%;font:inherit;font-size:20px;font-weight:700;padding:3px 8px;margin-left:-8px;border:1px solid #171717;border-radius:8px;outline:none;background:var(--surface);box-shadow:0 0 0 3px rgba(23,23,23,.1)}.ei.sm{font-size:14px;font-weight:400}.ei.bad{border-color:#e5484d;box-shadow:0 0 0 3px rgba(229,72,77,.15)}
.out{font-size:12px;color:var(--muted);min-height:18px}.out.err{color:#c62f2f}@media (prefers-reduced-motion:reduce){.ie svg{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const out=document.getElementById('out');
function field(slot,val,sm,req){let v=val;const pen='<svg viewBox="0 0 24 24" width="14" height="14" aria-hidden="true"><path d="M4 20h4L19 9l-4-4L4 16z" fill="none" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/></svg>';
function show(f){slot.innerHTML='';const b=document.createElement('button');b.type='button';b.className='ie'+(sm?' sm':'');b.setAttribute('aria-label','Edit: '+v);b.innerHTML='<span></span>'+pen;b.firstChild.textContent=v;b.addEventListener('click',edit);slot.appendChild(b);if(f)b.focus();}
function edit(){slot.innerHTML='';const i=document.createElement('input');i.className='ei'+(sm?' sm':'');i.value=v;i.setAttribute('aria-label','Edit value');slot.appendChild(i);i.focus();i.select();out.className='out';out.textContent='Enter to save, Esc to cancel.';let done=false;
function save(){const n=i.value.trim();if(req&&!n){i.classList.add('bad');out.className='out err';out.textContent='Name can\u2019t be empty.';i.focus();return;}done=true;v=n;show(true);out.className='out';out.textContent='Saved.';setTimeout(()=>{if(out.textContent==='Saved.')out.textContent='';},1500);}
i.addEventListener('keydown',e=>{if(e.key==='Enter'){e.preventDefault();save();}else if(e.key==='Escape'){e.preventDefault();done=true;show(true);out.textContent='Cancelled.';}});i.addEventListener('blur',()=>{if(!done){const n=i.value.trim();if(req&&!n){done=true;show(false);out.className='out err';out.textContent='Name can\u2019t be empty, so the old name was kept.';}else{done=true;v=n;show(false);out.className='out';out.textContent='Saved.';}}});}
show(false);}
field(document.getElementById('slot'),'Quarterly roadmap',false,true);field(document.getElementById('slot2'),'Plans and goals for the next three months.',true,false);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>Inline Edit Text — 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}
.fd{display:grid;gap:3px;margin-bottom:12px}.lb{font-size:11px;color:var(--muted);text-transform:uppercase;letter-spacing:.06em;font-weight:600}.ie{display:inline-flex;align-items:center;gap:8px;padding:4px 8px;margin-left:-8px;border-radius:8px;font-size:20px;font-weight:700;letter-spacing:-.01em;text-align:left}.ie.sm{font-size:14px;font-weight:400}.ie:hover{background:var(--pv)}.ie svg{opacity:0;transition:opacity .15s}.ie:hover svg,.ie:focus-visible svg{opacity:.6}
.ei{width:100%;font:inherit;font-size:20px;font-weight:700;padding:3px 8px;margin-left:-8px;border:1px solid #171717;border-radius:8px;outline:none;background:var(--surface);box-shadow:0 0 0 3px rgba(23,23,23,.1)}.ei.sm{font-size:14px;font-weight:400}.ei.bad{border-color:#e5484d;box-shadow:0 0 0 3px rgba(229,72,77,.15)}
.out{font-size:12px;color:var(--muted);min-height:18px}.out.err{color:#c62f2f}@media (prefers-reduced-motion:reduce){.ie svg{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="fd"><span class="lb">Project name</span><span id="slot"></span></div><div class="fd"><span class="lb">Description</span><span id="slot2"></span></div><p class="out" id="out" role="status"></p></div>
</div>
<script>
const out=document.getElementById('out');
function field(slot,val,sm,req){let v=val;const pen='<svg viewBox="0 0 24 24" width="14" height="14" aria-hidden="true"><path d="M4 20h4L19 9l-4-4L4 16z" fill="none" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/></svg>';
function show(f){slot.innerHTML='';const b=document.createElement('button');b.type='button';b.className='ie'+(sm?' sm':'');b.setAttribute('aria-label','Edit: '+v);b.innerHTML='<span></span>'+pen;b.firstChild.textContent=v;b.addEventListener('click',edit);slot.appendChild(b);if(f)b.focus();}
function edit(){slot.innerHTML='';const i=document.createElement('input');i.className='ei'+(sm?' sm':'');i.value=v;i.setAttribute('aria-label','Edit value');slot.appendChild(i);i.focus();i.select();out.className='out';out.textContent='Enter to save, Esc to cancel.';let done=false;
function save(){const n=i.value.trim();if(req&&!n){i.classList.add('bad');out.className='out err';out.textContent='Name can\u2019t be empty.';i.focus();return;}done=true;v=n;show(true);out.className='out';out.textContent='Saved.';setTimeout(()=>{if(out.textContent==='Saved.')out.textContent='';},1500);}
i.addEventListener('keydown',e=>{if(e.key==='Enter'){e.preventDefault();save();}else if(e.key==='Escape'){e.preventDefault();done=true;show(true);out.textContent='Cancelled.';}});i.addEventListener('blur',()=>{if(!done){const n=i.value.trim();if(req&&!n){done=true;show(false);out.className='out err';out.textContent='Name can\u2019t be empty, so the old name was kept.';}else{done=true;v=n;show(false);out.className='out';out.textContent='Saved.';}}});}
show(false);}
field(document.getElementById('slot'),'Quarterly roadmap',false,true);field(document.getElementById('slot2'),'Plans and goals for the next three months.',true,false);
</script>
</body></html>