Image to Base64: data URLs and when to use them
What Base64 image encoding really does, how data URLs work in HTML and CSS, whether to keep the prefix or wrap lines, and why inline images are not always a good idea.
Base64 is not an image format. It is a way to write the raw bytes of any file using only ordinary text characters, so that binary data can travel through systems designed for text. When you paste an image into HTML or CSS as a data URL, you are embedding those bytes directly in the document instead of pointing to a separate file.

What a data URL contains
A complete inline image looks like this in source: data:image/png;base64, followed by a long string of characters. The first part tells the browser what kind of file is coming. The middle names the encoding. Everything after the comma is the file itself, written in Base64. Together they form a URL that contains the resource rather than locating it elsewhere.
The prefix matters. An <img src> needs the full data URL, including the data:image/... part, or the browser will treat the string as a relative path and fail to load anything. A JSON API that expects only the encoded payload, on the other hand, usually wants the bare string without the prefix. The same applies to some build pipelines that add the prefix themselves. Keeping the prefix toggle in mind avoids the most common copy-paste failure: a long string that looks right but produces a broken image icon.
The MIME type in the prefix follows the source file. A PNG becomes image/png, a JPG becomes image/jpeg, and a WebP becomes image/webp. If you rename the file without changing its actual contents, the declared type will be wrong and some browsers will refuse to render it.
When inlining helps
Inlining is genuinely useful in a few specific situations. A single-file HTML deliverable — a report, a demo, an offline cheat sheet — can include its icons and logos as data URLs and remain one portable file. A tiny decorative icon can be inlined to avoid a separate network request. A CSS background that must travel with a component, such as in an email template or a self-contained widget, is another good case.
The common thread is that the image is small, fixed and part of the document rather than content that changes independently. For those images, avoiding an extra file can simplify deployment and remove a class of broken-path errors.
When it hurts
Base64 is roughly 33 percent larger than the original binary file. A 100 KB image becomes a string of about 133 KB, and that overhead is repeated on every page that includes the document. Inline images also cannot be cached as separate resources. If ten pages share the same logo stored as a data URL, a visitor downloads that logo ten times inside ten HTML files. A regular image file is fetched once and reused.
Large inline strings also make HTML hard to read and edit, inflate the document that the browser has to parse, and can clash with content security policies that restrict inline resources. For photographs, hero images or anything above a few kilobytes, a normal file path is the better answer. If you must inline a larger image, prepare it first: resize it to the size it will actually be displayed at with the resizing tool, and consider a format conversion to WebP where supported. Encoding a 4000-pixel photo is just a very long way to move unnecessary bytes.
Wrapped or single line
Line breaks in Base64 output are a readability choice, not a change to the data. Wrapping the string into fixed-width lines makes it easier to scan in an editor and prevents a single enormous line from stretching horizontally. Browsers tolerate whitespace inside a data URL, so wrapped output works in an HTML attribute or a CSS file.
JSON is different. A JSON string cannot contain a literal newline, so a wrapped value will break parsing unless the line breaks are escaped. When the destination is a JSON payload, a configuration file or an environment variable, choose single-line output. When the destination is a source file a human will read, wrapping is more comfortable. The encoder offers both, and copying the wrong variant is a frequent source of mysterious parse errors.
There is also a size field to check before you paste. If the string is far larger than expected, the source image is probably much bigger than you realised. That is a signal to go back and prepare the file rather than to trim the string by hand — removing characters from a Base64 value corrupts the image.
Encoding locally
The encoder works as soon as an image is dropped in. There is no upload step and no waiting for a server: the file is read in the browser and the output appears immediately. That makes it safe for internal documents, mockups and anything not yet ready to be seen publicly. A text download is available if you need the string as a file rather than from the clipboard.
Because encoding is instant, it is cheap to compare versions. Export a smaller image, encode it, and check the resulting string length before deciding which one to ship. The length is a direct proxy for the bytes you will add to the document.
Mistakes worth avoiding
Missing the prefix. A bare Base64 string in an img src is treated as a path, not as image data.
Wrong MIME type. The declared format must match the actual file bytes.
Inlining large photographs. Expect a 33 percent overhead and no caching benefit across pages.
Pasting wrapped output into JSON. Use single-line output for structured data.
Editing the string to shorten it. Never truncate or reformat the payload by hand; prepare a smaller source image instead.
A short checklist
Decide whether the image truly needs to travel inside the document. Reduce its dimensions first if it does. Choose the prefix or the bare payload based on where it is going. Pick wrapped lines for source files and single line for JSON. Confirm the length, then paste.
Used for small, fixed assets, Base64 is a tidy solution to a narrow problem. Used for everything, it turns a fast page into a heavy one. More practical write-ups are available from the blog index.