Base64 Image Data URIs: Syntax, Size Cost and Examples
A Base64 image is a file written as text inside a data URI. See the syntax, HTML and CSS examples, the 33 percent size overhead, and when inlining is worth it.
Updated
A Base64 image is an image file converted to text so it can be written directly inside HTML, CSS or JSON. The usual wrapper is a data URI, which looks like data:image/png;base64,iVBORw0KGgo... and works anywhere a normal image URL does. The trade-off is size: the text is about 33 percent larger than the file, so inlining pays off only for small images.
Data URI syntax
A data URI has four parts, defined in RFC 2397:
data:[media type][;base64],[data]
| Part | Example | Meaning |
|---|---|---|
| Scheme | data: | Says the content is inline, not fetched |
| Media type | image/png | Tells the browser how to interpret the bytes |
| Encoding flag | ;base64 | The data is Base64. Without it, the data is URL-encoded text |
| Data | iVBORw0KGgo... | The file content, after a comma |
Common media types for images are image/png, image/jpeg, image/gif, image/webp, image/avif, image/svg+xml and image/x-icon. If the media type is left out, the default is plain text, so always include it for images.
You can often recognize the format from the first characters of the data, because every file type starts with fixed bytes:
| Data starts with | Format |
|---|---|
iVBORw0KGgo | PNG |
/9j/ | JPEG |
R0lGOD | GIF |
UklGR | WebP |
PHN2Zy or PD94bWw | SVG |
For how the encoding itself works, including the alphabet and the = padding, see Base64 encoding explained.
HTML and CSS examples
In HTML, the data URI goes where the file path would go:
<img src="data:image/png;base64,iVBORw0KGgo..." alt="Logo" width="32" height="32">
In CSS, it goes inside url():
.icon { background-image: url("data:image/png;base64,iVBORw0KGgo..."); }
It also works for a favicon, with <link rel="icon" href="data:image/png;base64,...">, and in JavaScript, where canvas.toDataURL("image/png") returns exactly this kind of string. Quote the URI in CSS. Do not add line breaks or spaces inside the data.
Convert an image to Base64
- Open Image to Base64 and keep the Image → Base64 mode selected.
- Drop an image onto the drop area, or click to browse. PNG, JPG, GIF, SVG, WebP, AVIF and ICO files are accepted.
- Pick the output format: Data URI, Base64 only, CSS background or HTML img. The last two wrap the data URI in a ready-made
background-imagerule or animgtag. - Read the stats beside the preview: original size, Base64 size, output size and the size increase as a percentage.
- Click Copy output and paste it into your code.
To go the other way, switch to Base64 → Image and paste a data URI or a raw Base64 string. A data URI carries its own media type, so the format is detected for you. For a raw string, choose PNG, JPG or WebP under Save as. A preview appears if the data is valid, and the button downloads the decoded file.
The separate Base64 Encode / Decode tool is for text, such as an API credential or a JSON snippet. Use the image tool for files.
The size overhead, with numbers
Base64 represents every 3 bytes of the file as 4 text characters. The encoded length is the file size divided by 3, rounded up, times 4. That is an increase of one third.
| Original file | Base64 length | Increase |
|---|---|---|
| 1 KB (1,024 bytes) | 1,368 characters | 33.6 percent |
| 10 KB (10,240 bytes) | 13,656 characters | 33.4 percent |
| 100 KB (102,400 bytes) | 136,536 characters | 33.3 percent |
The few extra tenths of a percent come from rounding up to a multiple of four, plus the short data:image/png;base64, prefix.
Servers usually compress HTML and CSS with gzip or Brotli, which claws back some of the overhead. It does not remove it. PNG, JPEG and WebP data is already compressed, so the text version of it compresses poorly, and the inlined file still ends up larger than the binary original.
When inlining an image makes sense
- Tiny icons and placeholders, a few kilobytes at most, where avoiding an extra request is worth more than the added bytes.
- Single-file documents: an HTML report, an exported invoice or an offline page that must work without an assets folder.
- Blurred preview placeholders that show instantly while the full image loads.
- Data passed through text-only channels, such as an image field in a JSON API request.
- Generated images in JavaScript, where a data URI is the natural output of a canvas.
When it does not
- Photos and large graphics. A 200 KB hero image adds about 267 KB of text to the page.
- Images used on many pages. A normal file is downloaded once and cached. An inlined image is downloaded again inside every page or stylesheet that contains it.
- CSS that blocks rendering. A stylesheet full of embedded images delays the first paint, because the browser must download all of it before showing the page.
- Images that should be lazy-loaded or served in different sizes for different screens.
- HTML email, with care. Some email clients, including Gmail's web interface, do not display data URI images, so test before relying on them.
With HTTP/2 and HTTP/3, many small files can share one connection, so the old argument for inlining to save requests is weaker than it used to be. A site with a strict Content Security Policy also needs data: allowed in img-src before inlined images will show.
Rule of thumb: inline an image only if it is small enough that you would not mind reading it in a diff. Shrink it first, then encode it.
To shrink an icon before encoding, run it through the Image Compressor.
SVG is a special case
SVG is already text, so it does not need Base64. You can URL-encode it instead, which is usually shorter and stays readable:
url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Ccircle cx='5' cy='5' r='4'/%3E%3C/svg%3E")
Note that there is no ;base64 flag. Characters such as <, > and # must be percent-encoded, and using single quotes inside the SVG avoids clashing with the double quotes around the URL. Base64 still works for SVG and is the safer choice when a tool or template mangles special characters.
Try it
Drop an icon into Image to Base64, switch between Data URI, CSS background and HTML img output, check the size increase, and copy the result straight into your stylesheet or markup.