Image Formats Explained: JPEG, PNG, WebP, SVG & Optimization
Choosing the right image format can dramatically reduce file sizes, improve page load times, and enhance user experience. This guide covers every major format in depth, explains how compression works, and provides practical strategies for resizing, optimizing, and encoding images for the web.
Lossy vs Lossless Compression
Before diving into specific formats, it is essential to understand the two fundamental categories of image compression and the tradeoffs between them.
How Lossy Compression Works
Lossy compression permanently discards some image data to achieve smaller file sizes. The most common algorithm is DCT (Discrete Cosine Transform), which converts pixel values into frequency components and then quantizes them, reducing precision for high-frequency details that the human eye is less sensitive to. Each time you re-save a lossy image, more data is lost through cumulative generation loss.
The key tradeoff is visual quality versus file size. At low compression levels, the loss is imperceptible. At high levels, you will see artifacts: blockiness in smooth areas, ringing near sharp edges, and color banding in gradients.
How Lossless Compression Works
Lossless compression reduces file size without any data loss. It uses algorithms like DEFLATE (used in PNG) that find and encode repeating patterns efficiently. The original image can be perfectly reconstructed, pixel for pixel, from the compressed file. This is critical for screenshots, graphics with text, and images that will be edited further.
The downside is that lossless files are significantly larger than lossy alternatives for photographic content, because every detail is preserved regardless of visual importance.
JPEG Deep Dive
JPEG (Joint Photographic Experts Group) has been the dominant image format on the internet since the 1990s. It was specifically designed for photographs and complex images with smooth color gradients, using lossy DCT-based compression.
JPEG supports a quality scale from 1 to 100. In practice, the sweet spot for web use is between 75 and 85, where file sizes are small enough for fast loading but quality loss is difficult to detect without zooming in. Below quality 50, blocking artifacts become clearly visible. Above quality 90, file sizes grow dramatically with negligible visual improvement.
Common JPEG artifacts include blocking (8x8 pixel blocks become visible in smooth areas), ringing(halos near sharp edges), and mosquito noise (noise around fine details). These are inherent to the DCT algorithm and worsen with each re-save.
JPEG does not support transparency or animation. It supports 24-bit color (over 16 million colors). Each re-save degrades quality further due to cumulative lossy compression.
Best for: Photographs, complex illustrations with gradients, large background images, and any situation where file size matters more than pixel-perfect accuracy.
PNG Deep Dive
PNG (Portable Network Graphics) was created as a lossless, patent-free replacement for GIF. It supports both lossless compression and full alpha-channel transparency, making it the go-to format for images that must be pixel-perfect.
PNG comes in two main variants:
- PNG-8 (indexed): Supports up to 256 colors with an optional palette. Similar to GIF but with better compression. Ideal for simple graphics, logos, and icons with limited colors.
- PNG-24 / PNG-32 (truecolor): Supports 16.7 million colors (24-bit) plus a full 8-bit alpha channel for partial transparency (32-bit). Ideal for screenshots, complex graphics, and images requiring smooth transparency.
PNG's lossless DEFLATE compression preserves every pixel exactly. Sharp edges and text remain crisp, unlike JPEG which introduces artifacts around high-contrast boundaries. However, PNG files are typically 3 to 10 times larger than equivalent JPEG files for photographic content, because the compression cannot discard visually redundant information.
PNG does not support animation (use APNG or WebP instead).
Best for: Screenshots, UI elements, logos, graphics with text overlays, images requiring transparency, and any image that will be edited or re-saved.
WebP Deep Dive
WebP is a modern image format developed by Google that supports both lossy and lossless compression in a single file format. It was designed from the ground up for the web, and consistently outperforms JPEG and PNG in compression efficiency.
WebP produces 25-35% smaller files than JPEG at equivalent visual quality (lossy mode) and 26% smaller files than PNG (lossless mode). It supports transparency (alpha channel) with both lossy and lossless compression, and it supports animation as a direct replacement for animated GIFs.
The lossy mode uses VP8 intra-frame coding with predictive compression, which often produces fewer visible artifacts than JPEG at the same file size. The lossless mode uses a variant of DEFLATE that can reference already-decoded pixels for better compression.
Browser support is now effectively universal at 96%+ global coverage, including Chrome, Firefox, Edge, and Safari 14+. Only Internet Explorer lacks support, which is largely irrelevant for modern web development. For older browsers, you can serve WebP with JPEG/PNG fallbacks using the HTML <picture> element.
Best for: Practically everything on the web. Use lossy WebP for photographs and lossless WebP for graphics and screenshots. WebP should be your default format for all web images.
SVG Deep Dive
SVG (Scalable Vector Graphics) is fundamentally different from raster formats like JPEG, PNG, and WebP. Instead of storing pixel data, SVG stores a description of the image using XML markup — defining shapes, paths, text, and colors as mathematical instructions. This means SVG images can be scaled to any size without any quality loss whatsoever.
For simple graphics like logos and icons, SVG files are often dramatically smaller than equivalent PNG files — sometimes just a few hundred bytes. The format supports CSS styling, JavaScript interaction, SMIL animation, and CSS animation, making it incredibly versatile for interactive web elements.
SVG text is selectable and searchable by search engines, which is a significant SEO advantage for diagrams and infographics. Since SVGs are resolution-independent, they look perfectly crisp on all displays, including high-DPI (Retina) screens, without needing separate 2x assets.
SVG is not suitable for photographs or complex raster images — converting a photo to SVG would produce an enormous file with no quality benefit. Very complex illustrations can also produce large SVGs. Additionally, SVG can contain JavaScript, so always sanitize SVG files from untrusted sources to prevent XSS attacks.
Best for: Logos, icons, illustrations, charts, diagrams, and any graphic that needs to scale to different sizes while remaining crisp.
When to Use Which Format
The following comparison table summarizes the key differences between each format at a glance:
| Feature | JPEG | PNG | WebP | SVG |
|---|---|---|---|---|
| Type | Raster, lossy | Raster, lossless | Raster, both | Vector |
| Transparency | No | Yes (alpha) | Yes (alpha) | Yes (alpha) |
| Animation | No | No | Yes | Yes (SMIL/CSS) |
| Typical Size | Small | Large | Smallest | Varies |
| Best For | Photos | Screenshots, text | Everything web | Logos, icons |
| Browser Support | 100% | 100% | 96%+ | 99%+ |
Image Compression Strategies
Quality vs Size Curves
Every lossy format has a quality-size curve. For JPEG and WebP, the relationship is not linear: dropping from quality 90 to 80 saves far more bytes than dropping from 50 to 40, while the quality difference between 90 and 80 is barely perceptible. The practical sweet spot is usually between quality 75 and 85. Always compress to the lowest quality where artifacts are not noticeable at normal viewing size.
Responsive Images with srcset and sizes
Serving a 2000-pixel-wide hero image to a 375-pixel mobile screen wastes bandwidth. The HTML srcset attribute and sizes attribute let the browser choose the most appropriate image for the current viewport:
<img
src="photo-800.webp"
srcset="photo-400.webp 400w,
photo-800.webp 800w,
photo-1200.webp 1200w"
sizes="(max-width: 600px) 100vw, 50vw"
alt="Description"
loading="lazy"
/>Combine this with the <picture> element to serve different formats to different browsers:
<picture>
<source srcset="photo.webp" type="image/webp" />
<source srcset="photo.jpg" type="image/jpeg" />
<img src="photo.jpg" alt="Description" />
</picture>Lazy Loading
Add loading="lazy" to images that are below the fold so the browser defers loading them until the user scrolls near them. This reduces initial page weight and speeds up Largest Contentful Paint (LCP). Avoid lazy loading above-the-fold images, as they should load immediately.
CDN-Based Optimization
Content delivery networks like Cloudflare Images, imgix, and Cloudinary can automatically convert, resize, compress, and serve images in modern formats (WebP, AVIF) on the fly. This eliminates the need to pre-generate multiple image variants and ensures every visitor gets the optimal format for their browser.
WebP and AVIF with Fallbacks
AVIF offers even better compression than WebP (typically 20% smaller at equivalent quality), but browser support is still catching up. The best practice is to serve AVIF with WebP fallback and JPEG/PNG as the final fallback, using nested <source> elements in a <picture> tag.
Resizing Best Practices
Proper resizing is one of the most impactful optimizations you can make. Here are the key principles:
- Never upscale raster images. Enlarging a 200x200 PNG to 800x800 in HTML or CSS does not add detail — it just makes the existing pixels bigger and blurry. Always serve images at or above their display size.
- Use proper aspect ratios. Avoid stretching images by ensuring the width and height attributes (or CSS) maintain the original aspect ratio. Supplying both
widthandheightattributes also prevents layout shift (CLS). - Consider Retina / 2x displays. On high-DPI screens, a 400 CSS-pixel image needs 800 actual pixels to look sharp. Prepare 1x and 2x variants, or use
srcsetto serve the appropriate resolution automatically. - CSS resizing is not a substitute for actual resizing. Setting a large image to display at 200px wide via CSS still downloads the full file. Always resize the source image to match its maximum display size.
Base64 Encoding Images
Base64 encoding converts binary image data into an ASCII text string using a 64-character alphabet. This string can be embedded directly in HTML or CSS as a data URI, eliminating the need for a separate HTTP request:
<!-- Inline base64 image -->
<img src="data:image/png;base64,iVBORw0KGgo..." alt="Icon" />
/* Base64 in CSS */
background-image: url("data:image/svg+xml;base64,...");When to Use Base64
- Tiny icons and sprites (under 2 KB): Embedding a small icon directly avoids an extra HTTP request, which can be beneficial for critical above-the-fold content.
- Email templates: Some email clients block external images, so inline base64 ensures they display.
- Single-file HTML exports: When you need a fully self-contained HTML file with no external dependencies.
When NOT to Use Base64
- Large images: Base64 encoding increases file size by roughly 33% because every 3 bytes of binary data become 4 characters of ASCII text. A 100 KB JPEG becomes a 133 KB base64 string embedded in your HTML.
- When caching matters: Base64 images embedded in HTML or CSS cannot be cached independently by the browser. If the same image appears on multiple pages, it will be re-downloaded with each page.
- When initial render speed matters: Large base64 strings in your HTML block the parser and delay rendering.
In general, prefer external image files over base64 for anything larger than a few kilobytes. The reduced HTTP request benefit is outweighed by the size increase and lost caching.
Image Utilities & Resources
KnowKit offers free, client-side utilities for common image operations. All processing happens in your browser — no files are uploaded to any server.
- Image Compressor — Reduce file sizes while maintaining visual quality with adjustable compression settings.
- Image Converter — Convert between JPEG, PNG, WebP, and other formats instantly.
- Image Resizer — Resize images to exact dimensions with proper aspect ratio handling.
- Image to Base64 — Encode images as base64 strings for data URIs, CSS backgrounds, or email templates.
- Screen Resolution — Check your display resolution and pixel density for proper image sizing decisions.
Nelson
Developer and creator of KnowKit. Building browser-based tools since 2024.