Skip to content

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.

Also known as: avatar upload with preview, profile picture uploader, drag and drop avatar upload, image upload with crop zoom, profile image upload form

Profile Photo Upload with Preview live demo

Open in new tab

Tailwind CSS code (HTML, CSS, JS)

A profile photo uploader built with Tailwind CSS and vanilla JavaScript. The selected image appears in a large circle before anything is saved, together with three smaller avatars that show how it will look in a profile, a comment and a menu. It suits account settings, onboarding steps and team directory pages.

How it works

The root is section.tpu-uploader with a data-tpu-max-mb attribute. Inside the form.tpu-form, the left column holds .tpu-circle with the .tpu-main image, a round camera button (.tpu-browse), the .tpu-state pill and the .tpu-zoom range input. The right column holds the label.tpu-drop drop zone with a visually hidden input.tpu-input, the .tpu-error message, the "Preview sizes" list and the Remove, Cancel and Save buttons.

The script initializes each .tpu-uploader on DOMContentLoaded and keeps two objects: saved (what the server has) and draft (what the user is previewing). Choosing a file, either through the input, the camera button or by dropping it anywhere on the component, checks the type against TYPES and the size against data-tpu-max-mb. A valid file is turned into an object URL with URL.createObjectURL and copied into every .tpu-img. The zoom slider sets the --tpu-zoom custom property, which style.css uses to scale all previews. "Remove photo" adds is-empty to the root, which hides the images and shows the .tpu-initials. Cancel restores saved, and Save shows the .tpu-spinner for SAVE_MS milliseconds before the draft becomes the saved photo. Object URLs that are no longer shown are released with URL.revokeObjectURL.

Customization

  • data-tpu-max-mb on .tpu-uploader: change the size limit; the error message uses the same value.
  • TYPES in script.js: allow other formats such as image/gif, and update the accept attribute of .tpu-input to match.
  • The submit handler in script.js: replace the timeout with the commented FormData and fetch() example to upload the file and the zoom value.
  • h-44 w-44 on .tpu-circle: make the main preview larger or smaller.
  • min="100" max="200" on #tpu-zoom: change the zoom range, or remove the slider block entirely.
  • .tpu-state[data-state="pending"] and the other rules in style.css: restyle the pill for the current, pending, removed and saved states.
  • The AO text in each .tpu-initials: set the user's initials for the empty state.

Use cases

  • Account settings pages: users drop a photo and see a circular preview before saving
  • Onboarding flows: a zoom slider lets people adjust their picture during sign-up
  • Team directories: three small previews show how the photo looks in a profile, comment and menu
  • Upload forms: type and size validation shows clear errors before anything is sent

Accessibility

The file input stays in the tab order inside the drop zone label, which shows a focus ring through has-[input:focus-visible]. The camera button has an aria-label, and the input is described by the format hint and the error message. Errors are announced through aria-live="assertive", and preview, removal and save results through a role="status" region. The main image alt text changes with the state; the small previews are decorative with empty alt. The zoom slider has a label and an output. Cancel and Save stay disabled until there is a change. Transitions are removed under prefers-reduced-motion.

Usage notes

The sample portrait comes from Unsplash; replace it with the user's current photo. The zoom is only a visual preview: send its value to the server or crop the image there.

Profile Photo Upload with Preview at a glance

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

Questions about this template

Can I use the Profile Photo 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 Tailwind CSS 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):

  • <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

The Tailwind browser build is for prototyping. In production, compile Tailwind with its CLI or your bundler.

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

More File Uploads

Image Upload with Preview – free Bootstrap 5 file upload template

Image Upload with Preview

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

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.