Skip to main content

Image to Base64: Embed Images in Code

Convert any image to Base64: Data URLs, raw strings, HTML img snippets, and CSS url() output. Learn the 33% overhead, when to embed, and when to link instead.

CompressMyPic Editorial Team · Published Nov 17, 2026 · 10 min read

Image to Base64: Embed Images in Code

Developers often need an image as text rather than as a file. An icon inside a stylesheet, a logo inside an HTML email, or a picture inside a JSON payload all need the image converted into characters.

Base64 is the encoding that makes that possible. It turns the binary bytes of an image into a plain text string that can live inside code, markup, and messages.

This guide explains how the CompressMyPic image to Base64 converter works, what each output format is for, and when embedding an image is smart and when it is a mistake.

What Is Base64?

Base64 is a way of representing binary data as text. It takes the raw bytes of a file and rewrites them using 64 safe characters: the letters A to Z, a to z, the digits 0 to 9, plus, and slash, with equals signs as padding.

The characters are safe because they appear in every text system. An email, a JSON file, a stylesheet, and a database can all hold a Base64 string without corrupting it, which is not true of raw image bytes.

Base64 is not encryption and not compression. Anyone who receives the string can decode it back into the original image, and the string is always longer than the file it came from.

Think of it as a translation, not a transformation. The image stays the same image; only the alphabet used to write it down has changed.

How Base64 Encoding Works

Encoding reads the image file three bytes at a time. Each group of three bytes, 24 bits in total, is split into four groups of six bits, and each six-bit group becomes one Base64 character.

When the file length is not a multiple of three, the encoder adds equals signs as padding. One equals sign means the last group held two bytes, and two equals signs mean it held only one.

The result is a string of text that contains the whole image. Decoding reverses the process exactly, turning the characters back into the original bytes with no loss at all.

The converter does all of this locally in your browser. The image is read, encoded, and displayed without ever leaving your device, so private diagrams and client assets stay private.

The 33 Percent Overhead

Base64 output is about 33 percent larger than the original file. A 90 KB image becomes roughly a 120 KB string, because four text characters carry only three bytes of data.

The overhead is a fixed property of the encoding, not a flaw in the tool. Every Base64 encoder in every language produces the same size increase for the same input.

HTML and CSS outputs are slightly larger still, because they add a wrapper around the string. The image tag adds markup, and the CSS function adds its own syntax, on top of the 33 percent.

This overhead is the first thing to consider before embedding. A small icon grows from 3 KB to 4 KB, which nobody notices, but a 2 MB photo grows to 2.7 MB, which everyone notices.

Four Output Formats in One Tool

The converter offers four output formats, each matched to a different job. You choose the tab that fits your code, and the tool generates the exact snippet you need.

The Data URL tab gives you the complete data URI, ready to paste into an image source or a stylesheet. It includes the media type, the encoding marker, and the Base64 string in one piece.

The Raw Base64 tab gives you only the string itself, with no prefix. That is the form API payloads and databases usually want, because the receiving code adds its own context.

The HTML image tab gives you a complete image element with the Data URL as the source, plus width and height attributes filled in automatically. The CSS tab gives you a url() function wrapping the Data URL, ready for a background or a mask.

Data URLs Explained

A Data URL starts with the word data, followed by the media type, the encoding marker, and the Base64 string. A typical one looks like data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA.

Browsers understand Data URLs anywhere a normal URL would work. You can use one as an image source, a stylesheet background, or a link target, and the browser will decode and render it.

The advantage is self-containment. A page that uses Data URLs has no separate image files to fetch, so it works offline, in a single file, and in contexts where external requests are blocked.

The disadvantage is size. The 33 percent overhead applies to every embedded image, and a page with many Data URLs becomes heavy and slow to parse.

Raw Base64 Explained

Raw Base64 is the string without the data prefix. It is what you send when the receiver already knows the image type, or when the type travels in a separate field.

API payloads are the main use case. A JSON object can carry a field called image with the raw string, plus a field called mimeType with the format, and the server reconstructs the file from the two.

Databases use raw Base64 the same way. Storing an image as text in a text column avoids binary column types, which some systems handle poorly.

When you copy raw Base64, keep the padding. The equals signs at the end are part of the encoding, and removing them can break decoders that expect a complete string.

The HTML Image Snippet

The HTML tab generates a complete image element with the Data URL as its source. It also fills in the width and height attributes automatically from the image dimensions.

Those attributes matter more than they look. Without them, the browser does not know the image size until it decodes the string, and the page layout shifts when the image finally appears.

With width and height present, the browser reserves the right amount of space immediately. The layout stays stable, which is better for readers and for layout shift scores.

The snippet is ready to paste into any HTML file. Add an alt attribute describing the image before you ship it, since the tool cannot know what the picture shows.

The CSS url() Snippet

The CSS tab wraps the Data URL in a url() function, producing a value you can paste into a background-image or similar property. It is the fastest way to put a small image into a stylesheet.

Embedded CSS images are common for icons, patterns, and decorative backgrounds. They load with the stylesheet, so there is no separate request and no flash of missing decoration.

The trade-off is stylesheet weight. A stylesheet full of embedded images grows large, and browsers cannot cache the images separately from the CSS that contains them.

Use the CSS form for small, stable images that rarely change. A logo or a repeating pattern qualifies; a photo gallery does not, because the stylesheet would become enormous.

Quality: Encoding Changes Nothing

Encoding an image to Base64 does not change a single pixel. The process is fully reversible, and decoding the string returns the exact original bytes.

Animated GIFs keep their animation, SVG files keep their code, and photographs keep every detail. The encoding is a different representation, not a different image.

This is an important difference from converting formats. Changing a PNG to a JPG loses data, but encoding a PNG to Base64 loses nothing at all.

The only thing that changes is the size. The string is about a third larger than the file, and that cost is the price of carrying binary data as text.

Which Formats and Sizes Are Supported

The converter accepts PNG, JPG, WebP, GIF, SVG, BMP, and ICO files, up to 15 MB each. That covers logos, icons, photos, and animated images in a single tool.

SVG files encode cleanly because they are already text. The tool reads the SVG code and encodes it like any other file, producing a string that decodes back into the same vector graphic.

Favicon ICO files work too, which is handy when a site needs its icon embedded in a single-file page or an email template. The encoded ICO decodes back into a valid icon file.

For files near the 15 MB limit, consider shrinking first. A large PNG compressed to a smaller JPG before encoding produces a much shorter string, and the quality loss is often invisible.

When to Embed a Base64 Image

Embed small images that are essential to the page. Icons, logos, and tiny decorative graphics under a few kilobytes are ideal, because the overhead is negligible and the saved request is real.

Embed images in single-file documents. An HTML email, a standalone report, or an offline page cannot fetch external files, so embedding is the only way the images appear at all.

Embed images in API payloads and configuration files. When the image must travel inside JSON or inside a database record, Base64 is the standard way to carry it.

Embed when the image changes rarely. A logo that stays the same for years costs nothing to embed, while an image that updates weekly forces a code change every time.

When Not to Embed

Do not embed large photos. A 2 MB photograph becomes a 2.7 MB string inside your HTML, and the browser must download and parse all of it before the page can render.

Do not embed images that appear on many pages. A logo embedded in every page is downloaded every time, while a linked logo is cached once and reused across the whole site.

Do not embed images in emails without checking the client. Some email clients block inline Base64 images, and your message can arrive with broken pictures for those readers.

Do not embed images that need to be indexed or shared. A search engine cannot easily discover an embedded image, and a reader cannot right-click and save it the way they can with a normal file.

Base64 in Email and APIs

HTML emails are one of the oldest uses of Base64 images. An email is a single message, so an embedded image travels with it and displays even when the reader is offline.

Not all email clients agree, though. Some block inline Base64 images for security reasons, so test your template in the clients your audience actually uses before you rely on embedding.

APIs use Base64 for a different reason: JSON cannot carry binary data. An image upload endpoint that accepts JSON expects the image as a Base64 string, often with the media type in a separate field.

When you build such a payload, use the raw Base64 tab. It gives you the string without the data prefix, which is the form most APIs expect.

Privacy: Encoding Happens in Your Browser

Encoding an image online would mean uploading it to a server, which is a bad idea for proprietary icons, confidential diagrams, or client assets. This tool never does that.

The file is read by your browser, encoded on your device, and the string is shown to you locally. No copy of the image or the string leaves your computer.

That also means the tool works offline once the page has loaded. You can encode icons on a plane or in a secure office without any network access at all.

For teams handling sensitive visual material, local encoding is the deciding feature. The string you copy is yours, and no server ever sees either the image or its encoded form.

How to Use the Image to Base64 Tool

Open the image to Base64 converter and drop your image into the upload area, or paste a screenshot straight from the clipboard. Files up to 15 MB are accepted.

Choose the output tab that fits your job: Data URL for a complete URI, Raw for API payloads, HTML for a ready image element, or CSS for a stylesheet background.

Copy the output with the copy button, or download it as a file when the string is long and copying feels risky. The download saves the exact text, with no truncation.

For the step-by-step version, read How to Convert an Image to Base64, and for the decision framework, see Base64 Images: When to Embed and When Not To. To reverse the process, use the Base64 to image decoder.

Frequently asked questions

Is Base64 encoding the same as compressing an image?

No, it is the opposite in terms of size. Compression makes a file smaller, while Base64 makes it about 33 percent larger. Encoding changes the representation from binary to text without changing any pixels. If you need a smaller file, compress the image first and then encode the compressed result.

Can I embed a Base64 image in a Word document or PDF?

Not directly as a Base64 string. Word and PDF files store images as binary data inside the document, not as Base64 text. Base64 is for web code, emails, JSON payloads, and other text-based formats. Convert the image to a normal file first for document use.

Why is my Base64 string so long?

Because it carries the entire image as text, and text is a less efficient carrier than binary. A 90 KB image becomes about 120 KB of characters, and a 1 MB photo becomes about 1.4 MB of text. The length is normal. For very long strings, use the download button instead of copying.

Does Base64 encoding work for animated GIFs?

Yes. Encoding does not touch the pixel data, so an animated GIF encodes into a string that decodes back into the same animated file. The animation, frame count, and timing are all preserved exactly.

What is the largest image I can convert to Base64 here?

Files up to 15 MB are accepted. Larger files produce extremely long strings that are awkward to copy and slow to parse in a browser. Shrink or compress large images before encoding, and consider linking instead of embedding for anything over a few hundred kilobytes.

Can search engines see images embedded as Base64?

Mostly no. A search engine can technically decode a Data URL, but embedded images are not indexed, ranked, or shown in image search the way linked image files are. If image search traffic matters, link the image as a file instead of embedding it.

Related guides

Developer Tools

How to Extract a Colour Palette

Extract a colour palette from any photo in five steps: upload, pick 3–12 swatches, read the percentages, and copy CSS or JSON — free, in your browser.

Nov 17, 2026 · 10 min readRead the guide →

Share this guide

← Back to the blog