JA EN

Image Error Handling Best Practices - Fallbacks and UX Improvement

· 9 min read

Types and Causes of Image Loading Errors

Images fail to display on web pages for many reasons. Understanding error types is essential for implementing proper error handling.

Main error causes:

Error impact: Unhandled image errors display the browser's default broken image icon, giving users the impression the site is broken and damaging credibility. For e-commerce product images or profile pictures, this directly impacts conversion rates.

According to HTTP Archive research, approximately 3-5% of web page image requests return errors. For a page with 100 images, 3-5 won't display. This number isn't negligible and requires proper fallback strategies.

Basic Fallback Implementation with onerror Events

HTML's <img> element fires an error event when image loading fails. Handling this event enables switching to fallback images or displaying placeholders.

Inline fallback:

<img src="product.jpg" onerror="this.src='/images/fallback.png'; this.onerror=null;" alt="Product image">

this.onerror=null is critical. It prevents infinite loops if the fallback image also fails to load.

JavaScript implementation:

document.querySelectorAll('img').forEach(img => {
img.addEventListener('error', function() {
this.src = '/images/fallback.svg';
this.classList.add('img-error');
this.removeEventListener('error', arguments.callee);
});
});

React implementation:

function ImageWithFallback({ src, fallback, alt }) {
const [hasError, setHasError] = useState(false);
return <img src={hasError ? fallback : src} onError={() => setHasError(true)} alt={alt} />;
}

Note: onerror fires not only for HTTP error responses but also decode failures. Also, some browsers don't fire error events for empty src attributes, so avoid empty src.

Fallback Image Design - Optimal Approaches by Use Case

Fallback images should communicate that an image couldn't load while not significantly degrading page appearance. Design optimal fallbacks for each use case.

Generic fallback (SVG recommended): Site-wide generic fallback images should be SVG. SVGs display sharply at any size with minimal file size (typically under 1KB), minimizing additional network request overhead.

User avatar fallback: When profile images fail, CSS-based fallbacks showing user initials are effective. Generating background colors from username hashes creates unique colored avatars per user.

Product image fallback: E-commerce sites should display "image preparing" placeholders while continuing to show product info (name, price), not blocking purchase behavior. Category-specific fallbacks (clothing silhouettes, electronics icons) add helpfulness.

Data URI inline fallback: When even external file requests might fail (offline environments), Base64-encoded SVG as Data URI is the most reliable approach:

const FALLBACK = 'data:image/svg+xml;base64,PHN2ZyB...';

CSS Error State Styling - Gracefully Hiding Broken Images

CSS alone can improve image error display. This works even with JavaScript disabled, making it valuable for progressive enhancement.

Hiding broken image icons: When loading fails, browsers show alt text and a broken image icon. CSS can apply custom styles for error states:

img { font-family: sans-serif; font-size: 0.875rem; color: #666; text-align: center; }

::before and ::after pseudo-elements: When images load successfully, pseudo-elements aren't displayed. On failure, they become active. This characteristic enables showing custom content only on error:

img::after {
content: attr(alt) ' (image unavailable)';
display: block; position: absolute; inset: 0;
padding: 1rem; background: #f9f9f9; border: 1px dashed #ddd;
}

Note: Pseudo-element behavior varies across browsers. Works in Chrome and Firefox but Safari may not support <img> pseudo-elements. For cross-browser support, combine with JavaScript onerror handling.

object-fit combination: When images using object-fit: cover error, the same property applies to fallback images. Create square fallback images to avoid unintended cropping from aspect ratio differences.

Retry Strategies and Progressive Loading - Handling Temporary Errors

Image loading errors from temporary network instability can sometimes be resolved with retries. However, unlimited retries burden servers, requiring appropriate limits and strategies.

Exponential backoff retry:

function loadImageWithRetry(img, src, maxRetries = 3) {
let retries = 0;
img.onerror = () => {
if (retries < maxRetries) {
retries++;
setTimeout(() => { img.src = src + '?retry=' + retries; }, Math.pow(2, retries) * 1000);
} else { img.src = '/images/fallback.svg'; img.onerror = null; }
};
img.src = src;
}

Retry intervals increase exponentially: 2s, 4s, 8s. Query parameters (?retry=N) bypass CDN-cached error responses.

Intersection Observer combination: With lazy loading, start loading when entering viewport and retry on error. Retries execute only when images are visible, minimizing unnecessary requests.

Service Worker offline fallback: Service Workers can automatically return cached fallback images when offline, serving pre-cached placeholders when network requests fail.

When not to retry: 404 and 403 errors won't resolve with retries. Use fetch API to check status codes first - switch immediately to fallback for 4xx errors. Limit retries to 5xx and network errors.

Monitoring and Error Reporting - Visualizing and Improving Image Errors

Build monitoring infrastructure to detect image errors and continuously improve. Visualizing error occurrence enables early problem detection and response.

Error event collection:

window.addEventListener('error', (event) => {
if (event.target.tagName === 'IMG') {
navigator.sendBeacon('/api/image-errors', JSON.stringify({
src: event.target.src, page: location.href, timestamp: Date.now()
}));
}
}, true);

navigator.sendBeacon ensures data transmission even during page navigation. The true third argument captures events in the capture phase.

Metrics to monitor:

Alert configuration: Set alerts when image error rate exceeds 2x normal levels. This enables early detection of CDN outages or deployment mistakes causing mass errors.

Periodic health checks: Implement synthetic monitoring for critical images (logos, hero images, key product images) with periodic HTTP requests verifying 200 responses. Services like Uptime Robot or AWS CloudWatch Synthetics are available.

Related Articles

Image Lazy Loading Implementation Guide - Choosing Between loading=lazy and IntersectionObserver

Learn how to improve web page initial load speed with image lazy loading, covering both native API and JavaScript approaches with practical examples.

Image Loading Strategy Design - Mastering preload, fetchpriority, and decoding

Deep dive into three HTML attributes that optimize image loading. Learn the correct usage and combinations of preload, fetchpriority, and decoding for LCP improvement.

Embedding Images with Data URIs - Base64 Encoding Mechanics and Best Practices

Learn how Data URI scheme embeds images directly in HTML/CSS. Understand Base64 encoding mechanics, performance implications, appropriate use cases, and when to avoid inline images.

Image Placeholder Techniques Compared - LQIP, BlurHash, and SQIP Implementation Guide

Compare LQIP, BlurHash, and SQIP techniques for improving user experience during image loading. Learn the pros, cons, and optimal use cases for each placeholder method.

Creating Retina Display-Ready Images - Achieving Sharp Display on High-DPI Screens

Learn why images appear blurry on Retina and high-DPI displays and how to fix it. Covers srcset attribute, image-set(), SVG usage, and optimal export settings for crisp rendering.

How to Create HTML Image Maps and Modern Alternatives - Clickable Map Implementation Guide

Learn how to implement image maps using HTML map and area elements. Covers responsive design challenges and modern alternatives using SVG and CSS with practical code examples.

Related Terms