File Drop Zone
A drop area that highlights on drag, validates type and size, and works with Browse and the keyboard.
AI prompt
Clean UI mockup of a file upload drop zone, a large rounded rectangle with a dashed border, an upload cloud icon, the text Drag files here or Browse and a smaller line PNG or JPG up to 2 MB, a second state with a solid blue border, light blue fill and the text Drop to upload, below it a list with one accepted file and one rejected file in red with the reason, light theme, flat modern style. Negative: heavy shadows, clutter, text errors. Reduced motion: static design.About this pattern
Definition
A drop zone makes uploading feel direct, but dragging is not possible for everyone. The zone should be a real file input underneath, so a click, Enter or Space opens the file picker. While files hover over it, the zone changes style to show it will accept them, and each file is checked for type and size with a clear message.
Purpose & UX principles
- Wrap a visually hidden input type=file in a label or button so keyboard and screen readers work.
- Use dragenter, dragover and dragleave to toggle a highlight class; call preventDefault on dragover and drop.
- Validate type and size before accepting and explain each rejection.
- List accepted files with size and a remove button.
Interaction states
- Drag image files in, or press Browse; PNG and JPG up to 2 MB are accepted.
Motion specification
- The highlight fades over 150ms.
- Reduced motion: no transition.
Responsive behaviour
- Avoid flicker by counting dragenter and dragleave pairs, or by checking relatedTarget.
- Mention accepted types and the size limit in the text of the zone.
Code
<div class="bx"><label class="dz" id="dz" for="fi"><b>Drag images here</b><span>or <u>Browse</u> · PNG or JPG, up to 2 MB</span><input id="fi" class="sr" type="file" multiple accept="image/png,image/jpeg"></label><ul class="fl" id="fl" aria-label="Files"></ul><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}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);opacity:0}.dz{position:relative;display:grid;gap:3px;place-items:center;text-align:center;padding:20px 10px;border:2px dashed #c4c4be;border-radius:14px;background:var(--surface);font-size:12.5px;cursor:pointer;transition:background .15s,border-color .15s}.dz b{font-size:14px}.dz span{color:var(--muted)}.dz u{color:#2f6bff}.dz:hover,.dz:focus-within{border-color:#2f6bff}.dz.ov{border-style:solid;border-color:#2f6bff;background:#eaf0ff}.fl{list-style:none;display:grid;gap:4px;margin:8px 0 0;font-size:12px}.fl li{display:flex;justify-content:space-between;gap:8px;align-items:center;padding:5px 9px;border:1px solid var(--line);border-radius:8px;background:#fff}.fl li.bad{border-color:#e7b3ae;background:#fff6f5;color:#c0261a}.fl li span{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.fl button{font-size:11px;color:#666;text-decoration:underline}.out{font-size:12px;color:var(--muted);min-height:16px;margin:0}@media (prefers-reduced-motion:reduce){.dz{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const dz=document.getElementById('dz'),fi=document.getElementById('fi'),fl=document.getElementById('fl'),out=document.getElementById('out');let n=0,items=[];
function add(files){let ok=0,bad=0;[...files].forEach(f=>{let why='';if(!/^image\/(png|jpeg)$/.test(f.type))why='not a PNG or JPG';else if(f.size>2*1024*1024)why='over 2 MB';items.push({n:f.name,s:f.size,why});why?bad++:ok++;});draw();out.textContent=ok+' accepted'+(bad?', '+bad+' rejected':'')+'.';}
function fmt(b){return b>1048576?(b/1048576).toFixed(1)+' MB':Math.max(1,Math.round(b/1024))+' KB';}
function draw(){fl.innerHTML=items.map((it,i)=>'<li class="'+(it.why?'bad':'')+'"><span>'+it.n.replace(/</g,'<')+' \u00b7 '+(it.why?'rejected: '+it.why:fmt(it.s))+'</span><button type="button" data-i="'+i+'" aria-label="Remove '+it.n.replace(/"/g,'')+'">Remove</button></li>').join('');}
['dragenter','dragover'].forEach(t=>dz.addEventListener(t,e=>{e.preventDefault();n=t==='dragenter'?n+1:n;dz.classList.add('ov');dz.querySelector('b').textContent='Drop to upload';}));
dz.addEventListener('dragleave',e=>{n=Math.max(0,n-1);if(!n||!dz.contains(e.relatedTarget)){dz.classList.remove('ov');dz.querySelector('b').textContent='Drag images here';}});
dz.addEventListener('drop',e=>{e.preventDefault();n=0;dz.classList.remove('ov');dz.querySelector('b').textContent='Drag images here';add(e.dataTransfer.files);});
fi.addEventListener('change',()=>{add(fi.files);fi.value='';});fl.addEventListener('click',e=>{const b=e.target.closest('button');if(b){items.splice(+b.dataset.i,1);draw();out.textContent='Removed.';}});out.textContent='No files yet.';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>File Drop Zone — 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}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);opacity:0}.dz{position:relative;display:grid;gap:3px;place-items:center;text-align:center;padding:20px 10px;border:2px dashed #c4c4be;border-radius:14px;background:var(--surface);font-size:12.5px;cursor:pointer;transition:background .15s,border-color .15s}.dz b{font-size:14px}.dz span{color:var(--muted)}.dz u{color:#2f6bff}.dz:hover,.dz:focus-within{border-color:#2f6bff}.dz.ov{border-style:solid;border-color:#2f6bff;background:#eaf0ff}.fl{list-style:none;display:grid;gap:4px;margin:8px 0 0;font-size:12px}.fl li{display:flex;justify-content:space-between;gap:8px;align-items:center;padding:5px 9px;border:1px solid var(--line);border-radius:8px;background:#fff}.fl li.bad{border-color:#e7b3ae;background:#fff6f5;color:#c0261a}.fl li span{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.fl button{font-size:11px;color:#666;text-decoration:underline}.out{font-size:12px;color:var(--muted);min-height:16px;margin:0}@media (prefers-reduced-motion:reduce){.dz{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><label class="dz" id="dz" for="fi"><b>Drag images here</b><span>or <u>Browse</u> · PNG or JPG, up to 2 MB</span><input id="fi" class="sr" type="file" multiple accept="image/png,image/jpeg"></label><ul class="fl" id="fl" aria-label="Files"></ul><p class="out" id="out" role="status"></p></div>
</div>
<script>
const dz=document.getElementById('dz'),fi=document.getElementById('fi'),fl=document.getElementById('fl'),out=document.getElementById('out');let n=0,items=[];
function add(files){let ok=0,bad=0;[...files].forEach(f=>{let why='';if(!/^image\/(png|jpeg)$/.test(f.type))why='not a PNG or JPG';else if(f.size>2*1024*1024)why='over 2 MB';items.push({n:f.name,s:f.size,why});why?bad++:ok++;});draw();out.textContent=ok+' accepted'+(bad?', '+bad+' rejected':'')+'.';}
function fmt(b){return b>1048576?(b/1048576).toFixed(1)+' MB':Math.max(1,Math.round(b/1024))+' KB';}
function draw(){fl.innerHTML=items.map((it,i)=>'<li class="'+(it.why?'bad':'')+'"><span>'+it.n.replace(/</g,'<')+' \u00b7 '+(it.why?'rejected: '+it.why:fmt(it.s))+'</span><button type="button" data-i="'+i+'" aria-label="Remove '+it.n.replace(/"/g,'')+'">Remove</button></li>').join('');}
['dragenter','dragover'].forEach(t=>dz.addEventListener(t,e=>{e.preventDefault();n=t==='dragenter'?n+1:n;dz.classList.add('ov');dz.querySelector('b').textContent='Drop to upload';}));
dz.addEventListener('dragleave',e=>{n=Math.max(0,n-1);if(!n||!dz.contains(e.relatedTarget)){dz.classList.remove('ov');dz.querySelector('b').textContent='Drag images here';}});
dz.addEventListener('drop',e=>{e.preventDefault();n=0;dz.classList.remove('ov');dz.querySelector('b').textContent='Drag images here';add(e.dataTransfer.files);});
fi.addEventListener('change',()=>{add(fi.files);fi.value='';});fl.addEventListener('click',e=>{const b=e.target.closest('button');if(b){items.splice(+b.dataset.i,1);draw();out.textContent='Removed.';}});out.textContent='No files yet.';
</script>
</body></html>