100 Free AI UI Components

Image Upload with Preview

Add images by button or drag and drop and see thumbnails right away. Checks the file type and size and explains any problem.

One HTML file with the CSS and JavaScript inside. No library needed. MIT license.

Live preview

How to use it

  1. Click Copy the code, or download the file.
  2. Paste the CSS into your stylesheet, the HTML where you want the component, and the script before the closing body tag.
  3. Change --accent at the top of the CSS to your brand color, then connect the events to your AI service.

What you get

  • Works in light and dark mode automatically
  • Keyboard friendly, with labels for screen readers
  • Fits phones, tablets and desktops
  • Respects the reduced motion setting
  • No framework, so it works with any stack

The code

<!DOCTYPE html>
<!--
  Image Upload with Preview: a free AI UI component
  From 100 Free AI UI Components by MM Rahman Bappi, MIT license
  https://mmrahmanbappi.github.io/100-free-ai-ui-components/

  How to use: copy the CSS, HTML and JavaScript below into your page.
  Colors are CSS variables at the top. Change --accent to match your brand.
  Light and dark themes follow the visitor's system setting, or set
  data-theme="dark" or data-theme="light" on the html element.
-->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Image Upload with Preview | Free AI UI Component</title>
<meta name="description" content="Add images by button or drag and drop and see thumbnails right away. Checks the file type and size and explains any problem.">
<meta name="robots" content="noindex, follow">
<style>
:root{
  --accent:#5b5bd6; --accent-ink:#fff; --accent-soft:#ececfc;
  --bg:#f7f7f8; --surface:#fff; --surface-2:#f1f1f4; --ink:#17171c; --text:#3a3a44; --muted:#6c6c78;
  --line:#e3e3e8; --good:#15803d; --warn:#b45309; --bad:#c62828;
  --radius:14px; --shadow:0 1px 2px rgba(20,20,30,.06),0 8px 24px -12px rgba(20,20,30,.18);
  --font:system-ui,-apple-system,"Segoe UI",Roboto,Ubuntu,sans-serif; --mono:ui-monospace,"Cascadia Code",Menlo,Consolas,monospace;
}
@media (prefers-color-scheme:dark){:root:not([data-theme=light]){
  --accent:#8b8bff; --accent-ink:#0f0f16; --accent-soft:#25254a;
  --bg:#0f0f13; --surface:#18181f; --surface-2:#212129; --ink:#f3f3f6; --text:#d4d4dc; --muted:#9a9aa8;
  --line:#2c2c36; --good:#4ade80; --warn:#fbbf24; --bad:#f87171; --shadow:0 1px 2px rgba(0,0,0,.4),0 10px 30px -12px rgba(0,0,0,.6);
}}
:root[data-theme=dark]{
  --accent:#8b8bff; --accent-ink:#0f0f16; --accent-soft:#25254a;
  --bg:#0f0f13; --surface:#18181f; --surface-2:#212129; --ink:#f3f3f6; --text:#d4d4dc; --muted:#9a9aa8;
  --line:#2c2c36; --good:#4ade80; --warn:#fbbf24; --bad:#f87171; --shadow:0 1px 2px rgba(0,0,0,.4),0 10px 30px -12px rgba(0,0,0,.6);
}
*{box-sizing:border-box}
html,body{margin:0}
body{min-height:100vh;display:grid;grid-template-columns:minmax(0,1fr);place-items:center;padding:24px;background:var(--bg);color:var(--text);font:15px/1.5 var(--font);-webkit-font-smoothing:antialiased}
button,input,textarea,select{font:inherit;color:inherit}
:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
[hidden]{display:none!important}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
@media (prefers-reduced-motion:reduce){*,*::before,*::after{animation-duration:.01ms!important;animation-iteration-count:1!important;transition:none!important}}
/* ---- component ---- */
.wrap{width:min(100%,560px);display:flex;flex-direction:column;gap:14px}
.card{background:var(--surface);border:1px solid var(--line);border-radius:var(--radius);box-shadow:var(--shadow);padding:18px 20px}
.card h2{font-size:15.5px;margin:0 0 4px;color:var(--ink)}
.muted{color:var(--muted);font-size:13px}
.num{font-variant-numeric:tabular-nums}
.btn{display:inline-flex;align-items:center;justify-content:center;gap:6px;border:1px solid var(--line);background:var(--surface);color:var(--ink);border-radius:10px;padding:8px 14px;font-size:14px;font-weight:600;cursor:pointer}
.btn:hover{border-color:var(--accent)}.btn.pri{background:var(--accent);border-color:var(--accent);color:var(--accent-ink)}.btn:disabled{opacity:.45;cursor:not-allowed}
.btn.sm{padding:5px 10px;font-size:13px;border-radius:8px}
.note{font-size:13px;padding:9px 12px;border-radius:10px;background:var(--surface-2);color:var(--text)}
.note.warn{background:color-mix(in srgb,var(--warn) 10%,var(--surface));border:1px solid color-mix(in srgb,var(--warn) 35%,var(--line))}
.drop{margin-top:14px;border:2px dashed var(--line);border-radius:14px;padding:22px;text-align:center;color:var(--muted);transition:border-color .15s,background .15s}
.drop.over{border-color:var(--accent);background:var(--accent-soft)}
.drop svg{color:var(--accent);margin-bottom:6px}.drop p{margin:0 0 10px;font-size:14px}
.thumbs{list-style:none;margin:14px 0 0;padding:0;display:grid;grid-template-columns:repeat(auto-fill,minmax(100px,1fr));gap:10px}
.thumbs li{position:relative;aspect-ratio:1;border-radius:12px;overflow:hidden;border:1px solid var(--line);background:var(--surface-2)}
.thumbs img{width:100%;height:100%;object-fit:cover;display:block}
.thumbs button{position:absolute;top:6px;right:6px;width:26px;height:26px;border-radius:50%;border:0;background:rgba(0,0,0,.65);color:#fff;display:grid;place-items:center;cursor:pointer}
.thumbs small{position:absolute;left:0;right:0;bottom:0;background:linear-gradient(transparent,rgba(0,0,0,.7));color:#fff;font-size:11px;padding:12px 6px 4px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.errs{margin:10px 0 0;padding:0;list-style:none;font-size:13px;color:var(--bad)}
</style>
</head>
<body>
<div class="wrap"><div class="card">
  <h2>Add images</h2><p class="muted" style="margin:0">PNG, JPG or WebP, up to 5 MB each, 6 images at most.</p>
  <div class="drop" id="drop"><svg viewBox="0 0 24 24" width="26" height="26" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 16V4M6 10l6-6 6 6M4 20h16"/></svg><p>Drag images here, or</p><label class="btn">Choose images<input id="inp" type="file" accept="image/png,image/jpeg,image/webp" multiple hidden></label></div>
  <ul class="errs" id="errs" role="alert"></ul>
  <ul class="thumbs" id="thumbs" aria-label="Selected images"></ul>
</div></div>
<script>
// Demo helper: add ?theme=dark or ?theme=light to the address to preview a theme.
(function(){var t=new URLSearchParams(location.search).get('theme');if(t)document.documentElement.dataset.theme=t;})();
var MAX=5*1024*1024,LIMIT=6,TYPES=['image/png','image/jpeg','image/webp'],files=[],drop=document.getElementById('drop'),thumbs=document.getElementById('thumbs'),errs=document.getElementById('errs');
var X='<svg viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2.2\" stroke-linecap=\"round\" aria-hidden=\"true\"><path d=\"M6 6l12 12M18 6L6 18\"/></svg>';
function render(){thumbs.innerHTML='';files.forEach(function(f,i){var li=document.createElement('li'),img=document.createElement('img');img.src=f.url;img.alt=f.file.name;
  li.appendChild(img);li.insertAdjacentHTML('beforeend','<small></small><button type="button">'+X+'</button>');li.querySelector('small').textContent=f.file.name;
  var b=li.querySelector('button');b.setAttribute('aria-label','Remove '+f.file.name);b.onclick=function(){URL.revokeObjectURL(f.url);files.splice(i,1);render();};thumbs.appendChild(li);});}
function addFiles(list){errs.innerHTML='';[].forEach.call(list,function(f){var why=TYPES.indexOf(f.type)<0?'is not a PNG, JPG or WebP image':f.size>MAX?'is larger than 5 MB':files.length>=LIMIT?'was not added because the limit is 6 images':'';
  if(why){var li=document.createElement('li');li.textContent=f.name+' '+why+'.';errs.appendChild(li);return;}files.push({file:f,url:URL.createObjectURL(f)});});render();}
document.getElementById('inp').addEventListener('change',function(e){addFiles(e.target.files);e.target.value='';});
['dragenter','dragover'].forEach(function(n){drop.addEventListener(n,function(e){e.preventDefault();drop.classList.add('over');});});
['dragleave','drop'].forEach(function(n){drop.addEventListener(n,function(e){e.preventDefault();drop.classList.remove('over');});});
drop.addEventListener('drop',function(e){addFiles(e.dataTransfer.files);});
</script>
</body>
</html>

More voice and media components

  • Voice Recorder with Waveform

    Record a voice message with live bars that follow your voice. Uses the real microphone when allowed, and explains clearly when it is not.

  • Voice Mode Screen

    A full voice conversation screen with a glowing circle that reacts while the AI speaks, live captions, and mute and end buttons.

  • Audio Answer Player

    A compact player for spoken answers with play and pause, a seek bar, playback speed and the matching text highlighted as it plays.

  • Page Drop Zone with Upload Progress

    Drag files anywhere on the page to show a full screen drop area. Each file then uploads with its own progress bar and a cancel button.

  • Camera Photo Capture

    Take a photo with the device camera to ask the AI about it. Shows a live preview, a shutter button and retake, with a clear message if there is no camera.

  • Live Transcript

    Captions that appear as people talk, with speaker names and times. The newest line is highlighted, and the full text can be copied.