How Base64 Encoding and Data URIs Work
The 64-character alphabet, why output is 33 percent larger, what padding is for, and how a data URI is structured.
Primary Browser Tool
Image to Base64 Converter
Encode images as Base64 data URIs with ready-to-paste HTML, CSS, and Markdown snippets, or decode a Base64 string back into an image file.
Why Binary Needs a Text Disguise
Image files are binary: any of the 256 possible byte values can appear anywhere. Many systems were designed for text and treat certain bytes as control signals or cannot represent them at all. HTML, CSS, JSON, and email bodies are all text formats. Base64 solves the mismatch by representing arbitrary bytes using only 64 safe printable characters.
Three Bytes In, Four Characters Out
Three bytes hold 24 bits. Base64 splits those 24 bits into four groups of 6 bits. Each 6-bit group is a number from 0 to 63, which is looked up in the alphabet: A–Z for 0–25, a–z for 26–51, 0–9 for 52–61, then + and /. Four characters therefore carry what three bytes carried, which is where the one-third size increase comes from.
When the input length is not a multiple of three, the final group is padded. One leftover byte produces two characters and “==”; two leftover bytes produce three characters and “=”. The padding lets a decoder know exactly how many bytes the last group represents.
| Item | Value |
|---|---|
| Alphabet size | 64 characters (6 bits each) |
| Expansion | 4 output characters per 3 input bytes (+33.3%) |
| Padding character | = |
| URL-safe variant | - and _ replace + and / |
| A 3,000-byte PNG | 4,000 Base64 characters |
Anatomy of a Data URI
A data URI packages the encoded content with a description of what it is: the scheme “data:”, a media type such as image/png, the marker “;base64”, a comma, and then the encoded text. A browser that meets one where it expected a URL decodes the text and treats the result as if it had been downloaded.
The media type matters. If it says image/jpeg but the bytes are PNG, most browsers will still display the image by inspecting the content, but other software may not. When decoding, RapidPNG ignores the declared type in favor of the file signature found in the decoded bytes, which is the more trustworthy of the two.
Practical Examples
Recognizing formats by their first characters
Because every file type starts with fixed bytes, its Base64 starts with fixed characters: PNG data begins with “iVBORw0KGgo”, JPEG with “/9j/”, and GIF with “R0lGOD”.
Common Mistakes & Honest Limitations
Mistakes to Avoid
- Calling Base64 “encryption” or “compression”. It is neither; it makes data larger and is trivially reversible.
Technical Limitations
- Base64 output is larger before transport compression. Gzip or Brotli recovers part of the overhead for text-like content but little for already-compressed images.