Web & Encoding
Base64 Images and Data URIs Explained
Base64 turns image bytes into text. A Data URI adds the media type and encoding marker so the resulting string can be used directly where a URL is accepted.
Published
Raw Base64 and a Data URI are not the same string
Raw Base64 contains only encoded bytes. A Data URI is a URL with a scheme, media type, optional parameters, a comma and the data. For binary image bytes represented as Base64, include the ;base64 marker.
data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...Data URI syntax
The general form is data:[media-type][;base64],data. Use the image's actual MIME type; changing the label does not convert PNG bytes into JPEG or another format.
data:[<media-type>][;base64],<data>Embed a Base64 image in HTML
Use the complete Data URI as the src value and still provide meaningful alt text. The HTML document becomes responsible for delivering those image bytes.
<img src="data:image/png;base64,iVBORw0KGgo..." alt="Status icon">Use a Data URI in CSS
A Data URI can be used anywhere the CSS property accepts a URL. Keep generated strings out of hand-edited styles when their length makes reviews and diffs difficult.
.status-icon {
background-image: url("data:image/png;base64,iVBORw0KGgo...");
}Choose the correct image MIME type
| Format | MIME type | Prefix |
|---|---|---|
| PNG | image/png | data:image/png;base64, |
| JPEG | image/jpeg | data:image/jpeg;base64, |
| WebP | image/webp | data:image/webp;base64, |
| SVG | image/svg+xml | data:image/svg+xml;base64, |
Base64 increases the encoded byte count
Base64 represents each three input bytes with four encoded characters, so the Base64 portion is roughly one third larger before the Data URI prefix. HTTP compression may change transfer cost, but it does not remove parsing, caching or maintenance trade-offs.
When to inline an image
- Consider small, stable assets when avoiding a request materially helps the page.
- Prefer normal image files for large, reusable or independently cached assets.
- Measure the final compressed document rather than comparing raw sizes alone.
- Treat SVG and other active-capable media according to your content security policy.
- Never confuse Base64 encoding with sanitization or encryption.
Try the example
Generate a Data URI from the safe sample image
The built-in 1×1 PNG demonstrates raw Base64, the MIME prefix and the complete browser-ready URL.
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=Expected result: The tool loads the local sample and displays Data URI, raw Base64, HTML and CSS representations with size diagnostics.
Generate the real formats
Convert an image to Base64 and Data URI
Load the safe sample or your own image locally, then compare raw Base64, Data URI, HTML and CSS output.