Skip to content

Image Upload with Preview

Bootstrap 5 drag-and-drop image uploader with thumbnail previews, remove buttons, type and size validation and simulated upload progress.

Also known as: drag and drop file upload, image uploader with preview, multiple image upload, photo upload form

Image Upload with Preview live demo

Open in new tab

Bootstrap 5 code (HTML, CSS, JS)

A Bootstrap 5 image uploader with a drag-and-drop zone, instant thumbnail previews, per-file size labels, remove buttons, type and size validation and a simulated upload with progress bars. Use it for profile galleries, product photo forms, listing editors and any form that collects several images.

How it works

The root is section.iu-uploader, which holds the .iu-card. A visually hidden input.iu-input (type="file", multiple, with an accept list) is followed by label.iu-drop, so clicking the drop zone or pressing Space on the focused input opens the file dialog. The zone also listens for dragenter, dragover, dragleave and drop; while files hover over it the root gets is-dragover and the illustration lifts.

Every chosen or dropped file goes through addFiles(). It checks the type against the ACCEPTED array, the size against data-iu-max-size and the count against data-iu-max-files, and lists rejected files in the .iu-feedback line. Accepted files are cloned from the template.iu-template into the .iu-grid: the preview uses URL.createObjectURL(), so nothing is read or sent before upload, and formatSize() writes the size. Removing an item calls URL.revokeObjectURL() to free memory. The .iu-summary line, the .iu-counter__num badge and the state of the Clear all and Upload buttons are updated by refresh().

Clicking .iu-upload marks the pending items with is-uploading, fills their Bootstrap .progress bars step by step and finally adds is-done, which shows a green check on the thumbnail.

Customization

  • data-iu-max-files on .iu-uploader: change the image limit; also update the "/6" in .iu-counter and the subtitle.
  • data-iu-max-size on .iu-uploader: change the size limit per image in MB.
  • ACCEPTED in script.js and the accept attribute of .iu-input: allow other formats such as image/avif.
  • --iu-accent and --iu-accent-rgb in .iu-uploader: recolor the drop zone, counter, progress bars and Upload button.
  • --iu-thumb-min in .iu-uploader: make the preview tiles larger or smaller; the grid adds columns automatically.
  • aspect-ratio in .iu-thumb: use 4 / 3 or 16 / 9 instead of square previews.
  • The window.setInterval block in the .iu-upload click handler: replace it with fetch() or XMLHttpRequest that sends each entry.file in a FormData to your server.

Use cases

  • Product photo forms: drag-and-drop uploads show instant thumbnails with file sizes
  • Listing editors: remove buttons let sellers delete a photo before saving the listing
  • Profile galleries: type and size validation rejects files that are not allowed
  • Forms with several images: simulated progress bars show each upload moving along

Accessibility

The file input stays a real, focusable control; its focus ring is drawn on the drop zone through .iu-input:focus-visible + .iu-drop. Drag and drop is optional, the dialog works with the keyboard. Each preview has a descriptive alt, each remove button an aria-label with the file name, and after removal focus moves to the next remove button or back to the input. Validation errors and progress messages are announced through the aria-live="polite" feedback line, which also describes the input. The progress bars use role="progressbar" with aria-valuenow. Entry animations and transitions are disabled under prefers-reduced-motion: reduce.

Usage notes

Requires Bootstrap 5 and Bootstrap Icons. Client-side checks only help users: validate type and size again on the server. For several uploaders on one page, give each input its own id and update the label's for.

Image Upload with Preview at a glance

Framework
Bootstrap 5
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
Bootstrap 5, Bootstrap Icons
Layout
Responsive, works on mobile
Category
File Uploads
License
Free for personal and commercial use

Questions about this template

Can I use the Image Upload with Preview code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

Load Bootstrap 5 and Bootstrap Icons from a CDN (the links are under Requirements). Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

Add these to your page (already included in the download):

  • <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
  • <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  • <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
Image Upload with Preview – free Bootstrap 5 file upload template
Image Upload with Preview · free Bootstrap 5 file upload template

More File Uploads

Image Upload with Preview – free Bootstrap 6 file upload template

Image Upload with Preview

Bootstrap 6 image uploader that shows thumbnails before upload, with a cover photo star, alt text Dialog, remove buttons, size checks and per-photo progress.

Drag and Drop File Upload – free Bootstrap 6 file upload template

Drag and Drop File Upload

Dark Bootstrap 6 drag and drop upload zone with a file list, type icons, file sizes, progress bars, size and type validation and remove buttons.

Profile Photo Upload with Preview – free Tailwind CSS file upload template

Profile Photo Upload with Preview

Tailwind CSS avatar uploader with drag and drop, a circular live preview, zoom slider, size previews, file validation and a simulated save.

File Upload Dropzone – free Tailwind CSS file upload template

File Upload Dropzone

Tailwind CSS drag-and-drop file upload zone with keyboard support, type and size validation, a file list and per-file progress bars.