How to convert an image to Base64
- Click Choose file in the tool above and pick a PNG, JPG, GIF, WebP, SVG or ICO file.
- Keep Output as data: URI ticked to get a string that starts with
data:image/png;base64,(or the matching type). Untick it if you only need the Base64 characters, for example for a JSON field. - Click Copy, or Download to save the result as a text file.
Using the result in HTML and CSS
A data URI puts the picture inside your markup, so the browser does not make a separate request for it:
<img src="data:image/png;base64,iVBORw0KGgo..." alt="Logo" width="48" height="48">
.icon { background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0..."); }
For JSON or an API, send only the part after the comma and store the MIME type in its own field.
When to embed an image as Base64
Base64 makes an image about 33% larger, and an embedded image cannot be cached on its own. That trade is worth it for small, frequently used images: icons, a logo in an email signature, a placeholder, or an image inside a single-file HTML report. For photos and anything over a few dozen kilobytes, a normal image file is usually faster.
Common problems
- The image does not show. Check that the data URI kept its prefix and that the MIME type matches the file. A JPEG labelled
image/pngmay fail in some apps. - The string has line breaks. Turn off Wrap at 76 chars; browsers do not accept line breaks inside a data URI in an
srcattribute. - SVG looks wrong. SVG files are text, so you can also use them in CSS without Base64, but Base64 avoids problems with quotes and
#characters.
To turn a Base64 string back into a picture, use Base64 to image. For plain text, go back to the Base64 encoder.
Converting SVG to a data URI
SVG is different from PNG or JPEG: it is a text file of XML, not binary pixels. Base64 still works, and the tool gives you data:image/svg+xml;base64,..., but you have a second option. Because SVG is text, it can go into a data URI with URL encoding instead of Base64, for example data:image/svg+xml,%3Csvg xmlns=.... That version is usually smaller, since Base64 always adds a third to the size while URL encoding only escapes a handful of characters such as <, >, # and quotes.
Base64 is the safer choice when you do not want to think about escaping, for instance when the SVG contains # colour values, quotes inside attributes, or non-ASCII text. A # left unescaped in a URL-encoded data URI is read as the start of a fragment and cuts the image off.
Two more things to check before embedding an SVG. First, make sure the file has an xmlns="http://www.w3.org/2000/svg" attribute on the root element; browsers will not render a data URI SVG without it. Second, if the SVG contains text in Chinese or other non-Latin scripts, keep the file saved as UTF-8. The encoder here reads the file bytes exactly as they are, so a UTF-8 file stays correct, and you can add ;charset=utf-8 after the MIME type if a tool needs it spelled out.