Accessibility for Image Preview UI
What you'll learn: How to make your image-upload previews usable by screen-reader users and keyboard navigators, not just mouse users.
When you build a file-upload interface with image previews, visual users see thumbnails and click "X" buttons. But someone using a screen reader experiences nothing useful unless you add semantic information. Someone unable to use a mouse needs keyboard alternatives to every action.
Core Accessibility Requirements
Alt Text for Preview Images
Every preview image needs descriptive alt text. Use the filename or user-provided description:
const img = document.createElement('img');
img.src = objectURL;
img.alt = `Preview of ${file.name}`;
ARIA Labels for Interactive Elements
Remove buttons need clear labels, not just visual icons:
const removeBtn = document.createElement('button');
removeBtn.textContent = '×';
removeBtn.setAttribute('aria-label', `Remove ${file.name}`);
Keyboard Controls
Every mouse action needs a keyboard equivalent. Remove buttons should be focusable <button> elements (not <div> elements), so Tab navigation works. Handle Enter and Spacebar:
removeBtn.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
removePreview(fileId);
}
});
Status Announcements
When previews load or files are removed, announce changes to screen readers using aria-live regions:
<div role="status" aria-live="polite" aria-atomic="true" class="sr-only">
<!-- Populated dynamically: "Image preview loaded for photo.jpg" -->
</div>
Think of accessibility as providing "narration and subtitles" for your interface—every visual cue needs a text equivalent, and every mouse action needs a keyboard path.
Key Takeaway: Accessible image preview UIs require meaningful alt text, ARIA labels on controls, full keyboard operability, and dynamic status announcements for screen-reader users.