Image to Base64: Data URIs, Size Overhead and When to Inline

Last updated: · 7 min read

Converting an image to Base64 means writing the bytes of the file as text, using an alphabet of 64 characters. Put a short prefix starting with data: in front and you have a data URI, which can go straight into HTML or CSS in place of a file address. For tiny icons that saves a network request; for large images it makes the page heavier. The Image ↔ Base64 Converter works in both directions: it turns an image you choose into a data URI, a CSS line and an <img> tag, and it turns pasted Base64 back into an image you can download. Everything happens in your browser and the file is never uploaded. This guide covers the data URI syntax, the size arithmetic, the trade-offs of inlining and the limits of the tool; the outputs in the example are the real text it produces.

Formula

data:[<media type>][;base64],<data>
  example prefix: data:image/png;base64,

Base64: every 3 bytes → 4 characters (A–Z, a–z, 0–9, +, /), an incomplete group is padded with =
  Base64 length = 4 × ⌈bytes ÷ 3⌉
  data URI length = prefix length + Base64 length
  overhead ≈ 33 % (more for very small files because of the prefix)

The data URI scheme is defined in RFC 2397: the word data: is followed by a media type such as image/png, the marker ;base64 saying the data is Base64, a comma and the data itself. Base64 is defined in RFC 4648; it splits every 24-bit group into four 6-bit pieces and writes each piece as one character, which is why the output is a third longer than the input. Base64 is neither compression nor encryption; it only makes data safe to carry as text, and anyone can decode it. The tool determines the type from the signature in the first bytes of the content, not from the file name or extension, and writes the prefix accordingly.

What the inputs mean

Image → Base64: the image file
Choose a file or drop it on the area. PNG, JPEG, GIF, WebP, AVIF, BMP, ICO and SVG are accepted, up to 10 MB. A file whose content is not a recognised image is rejected whatever its extension says.
Image → Base64: the outputs
Type, original size and Base64 size (with the percentage increase) are shown, followed by three texts: Data URI, CSS background and HTML <img> tag. Text longer than 4,000 characters is shortened on screen, but the Copy button copies all of it. Above 100 KB the tool shows a warning.
Base64 → Image: the pasted text
Paste a complete data URI or bare Base64. Spaces and line breaks are ignored, the URL-safe alphabet (- and _) is accepted and trailing = signs are optional. Percent-encoded data URIs without Base64, as often used for SVG, are decoded too.
Base64 → Image: the result
A preview, the type, the size in bytes and the pixel dimensions are shown; the download button saves the file with the matching extension, such as image.png or image.gif.

Worked example: A 42-byte transparent GIF: decode, download, encode again

  1. On the “Base64 → Image” tab, paste this text: R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
  2. The tool decodes it and recognises the GIF89a signature in the first bytes. Result: Type: image/gif, Size: 42 B · 1 × 1 px. The download button saves the file as image.gif (its single pixel is transparent, so the preview looks empty).
  3. Switch to the “Image → Base64” tab and choose the image.gif you just downloaded. The tool shows: Type: image/gif, Original size: 42 B, Base64 size: 78 B (+86%).
  4. Data URI: data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
  5. CSS background: background-image: url("data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7");
  6. HTML <img> tag: <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="">
  7. The arithmetic: 42 bytes ÷ 3 = 14 groups, 14 × 4 = 56 Base64 characters; the prefix “data:image/gif;base64,” is 22 characters, 78 in total. Because the file is so small the increase shows as 86 %. For a 100 KB PNG (102,400 bytes) the same sum gives 136,558 characters, an increase of about 33 %.
  8. The safety check: paste the same data with the prefix data:image/png;base64, and the tool refuses to display it, with the message “The content does not match the declared type, so it is not shown for safety. Actual content type: image/gif”. A data URI that is not an image, such as data:text/html;base64,PGgxPkhlbGxvPC9oMT4=, is rejected with “This data type is not allowed.”

Result: The 42-byte GIF becomes a 78-character data URI (data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7), and decoding it returns the same 42 bytes.

🧬 Image to Base64 Converter and Decoder

Free, no sign-up; your data stays in your browser.

Open the calculator →

How to read the result

Inlining trades a network request for a larger document. For icons of a few hundred bytes, placeholder images and HTML documents that must travel as a single file, the trade pays off. As the image grows the balance tips the other way: the data grows by a third, the image cannot be cached on its own like a separate file and is downloaded again whenever the HTML or CSS that contains it changes; if the same image appears on several pages it is carried in each of them. Large images inlined in CSS also delay the download and parsing of the stylesheet. Server compression wins back part of the overhead, not all of it. RFC 2397 itself notes that data URLs are useful for short values. SVG is text, so it can also be inlined with percent-encoding instead of Base64; the tool always produces Base64 when encoding and accepts both forms when decoding. The generated <img> tag has an empty alt attribute; if the image carries meaning, write the alt text yourself.

Common mistakes

  • Assuming Base64 shrinks or compresses the file. It does the opposite: the data grows by about a third.
  • Treating Base64 as encryption. Encoding provides no secrecy; anyone who sees the text can recover the image.
  • Inlining large photos in CSS or HTML. The image cannot be cached separately and travels with every load of the document; serve images above 100 KB as separate files.
  • Forgetting the prefix or declaring the wrong type. Bare Base64 does nothing inside src or url(); you need the full prefix, such as data:image/png;base64, and the type has to match the content.
  • Inlining the same image in many places. Every use copies the data again; for repeated images use a file address or a CSS class.
  • Pasting text broken across lines into url() or src. The tool ignores whitespace when decoding, but for inlined use keeping the value on one line is the safe choice.
  • Leaving alt="" on an image that conveys meaning. An empty alt marks the image as decorative, and a screen reader skips it.

Frequently asked questions

How much does Base64 increase the size of an image?

About 33 %, because every 3 bytes become 4 characters. The data URI prefix comes on top; for files of only a few bytes that prefix makes the ratio look much higher (86 % in the 42-byte example).

Does inlining an image as Base64 make a page faster?

Only for very small images, where it saves a network request. With large images the document grows, the image cannot be cached separately and the page gets slower. The tool warns above 100 KB.

What is a data URI and how is it written?

An address that carries the data itself: data:, then the media type, ;base64 if Base64 is used, a comma and the data. Example: data:image/gif;base64,R0lGODlh…

Is the image I select sent to a server?

No. The file is read and converted in your browser and is not transmitted anywhere.

I pasted Base64 but no image appears. Why?

The text may not be valid Base64, the decoded data may not be a recognised image format, or the type declared in the data URI may not match the content. The tool shows a separate message for each case; check that the start and end of the text were not cut off when copying.

Do SVGs need Base64?

No. SVG is text, so it can be inlined with percent-encoding (data:image/svg+xml,%3Csvg…), which is often shorter. The tool produces Base64 when encoding and reads both forms when decoding; SVG is displayed purely as an image, so scripts inside it do not run.

The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.

Sources

Choose your cookie preference to continue

Records the site needs to work (theme, language, favourites) stay in your browser.

This site is free and funded by ads.

With your consent, Google Analytics cookies measure visits and Google AdSense cookies are used to show ads; the data is processed on Google servers abroad.

If you reject, the site works just the same.

Cookie Policy · KVKK notice