Images & PDF

Base64 Images: When to Inline Them and When Not To

Embedding an image as a Base64 data URI saves one request. But it costs a third more bytes and all of the caching. Here is the maths, the syntax, and a table to help you decide.

Base64 Images: When to Inline Them and When Not To
Photo by Bibek ghosh on Pexels

Key takeaways

  • Base64 turns every 3 bytes of binary into 4 text characters. So an encoded image is always about 33 per cent bigger than the file.
  • A data URI puts that text straight into HTML or CSS. It saves one HTTP request, but the browser can no longer cache that image.
  • Inlining pays off for tiny icons, SVG symbols and single-file documents. It hurts for photos, shared images and anything over about 10 KB.
  • Most email apps, including Gmail, do not show data-URI images. For email, use hosted or attached images.
  • Always compress the image before you encode it. Base64 makes bigger whatever you give it, and the text compresses badly afterwards.

Base64 is a way to write binary data, such as an image file, using only 64 ordinary text characters. This lets you paste it into places that only accept text. A Base64 image placed in HTML or CSS as a data URI loads with the page. It is not a separate request. But it is one third bigger than the original file. It is also downloaded again with every page that contains it. That trade is worth making for tiny icons and self-contained documents. It is almost never worth it for photos.

What Base64 actually is

Binary files can hold 256 different byte values. Many of these are control codes, or characters that HTML, JSON, email and URLs cannot carry safely. Base64 solves this with an alphabet of 64 characters that every system treats as plain text. These are the capital letters A to Z, the small letters a to z, the digits 0 to 9, and the two symbols + and /. Sixty-four values need exactly 6 bits each. That number is the key to everything else.

The encoder reads the input 3 bytes at a time. Three bytes are 24 bits. That divides neatly into four groups of 6 bits. Each group becomes one character from the alphabet. Sometimes the input does not divide by three. Then the last group is padded with one or two = signs. This tells the decoder how many real bytes to expect.

The text Hi! is three bytes: 72, 105 and 33. In binary that is 01001000 01101001 00100001. Regroup the bits into sixes: 010010 000110 100100 100001. These are 18, 6, 36 and 33. Those places in the alphabet are S, G, k and h. So Hi! encodes to SGkh. Drop the exclamation mark and the two bytes Hi encode to SGk=, with padding.

You can try this yourself with the Base64 Encoder and Decoder. It encodes any text, or decodes a string back. Note that Base64 is an encoding, not encryption. Anyone can reverse it in a millisecond, so it hides nothing.

The 33 per cent overhead, with the maths

Every 3 bytes become 4 characters. Each character takes 1 byte in the page. So the encoded form is 4 divided by 3 times the original size. That is a 33.3 per cent increase before anything else is added. Padding adds up to 2 bytes. The data:image/png;base64, prefix adds about 22 more. This only matters for very small files.

Original imageBase64 charactersIncrease
600 bytes (small SVG icon)800200 bytes
1,500 bytes (16-pixel PNG icon)2,000500 bytes
10 KB (small logo)13.3 KB3.3 KB
120 KB (compressed photo)160 KB40 KB
1 MB (uncompressed photo)1.33 MB333 KB

Older email standards also break Base64 into lines of 76 characters. Each line ending adds two more bytes. That pushes the overhead to about 36 per cent in that setting.

Some people say web servers compress HTML and CSS with gzip or Brotli, so the extra size vanishes in transit. It mostly does not. A JPEG, PNG or WebP is already compressed, so its bytes look almost random. The Base64 text of random bytes also looks almost random. Compression usually wins back a few per cent. You would need 25 per cent to break even. SVG is the exception. It is text to begin with. An SVG embedded as plain UTF-8, not Base64, compresses very well.

A developer checking website speed on a laptop
Photo by Christina Morillo on Pexels

Data URIs in HTML, CSS and email

A data URI is a URL that carries its own content. The format is data:, then the media type, then ;base64, then a comma and the encoded data. It goes anywhere a normal URL would.

In HTML, the image tag becomes <img src="data:image/png;base64,iVBORw0KGgo..." alt="Logo">. In CSS it is background-image: url(data:image/svg+xml;base64,PHN2Zy...). For SVG you can skip Base64. Put the markup straight after the comma, with a few characters percent-encoded. This is shorter and compresses better. The Image to Base64 Converter makes the data URI, a ready-made image tag and a CSS rule from any file. Paste whichever form you need.

Email is the awkward case. Apple Mail and the iOS and macOS mail apps show data-URI images. Gmail, Outlook on the web and most desktop Outlook versions do not show them, at the time of writing. They show a broken image or nothing. For email, there are two safe options. Host the image on a server and link to it. Or attach it and point to the attachment by a content ID. Expect a data URI in an email to fail for most readers.

Images in Markdown files, chat messages and documentation systems follow the same rule as email. Some tools allow data URIs, and many remove them for safety. Test in the real destination before you rely on it.

Caching and performance: what you gain and what you lose

The case for inlining is that it removes a request. Normally the browser opens a connection, sends headers, waits for the server and gets the file. With an inlined image, it is simply there when the HTML or CSS arrives. On a page with twenty tiny icons, twenty requests become none. The icons can show as soon as the stylesheet is read, with no flash of missing graphics.

The case against is caching. An external image is downloaded once. After that, the browser cache serves it on every other page that uses it, for as long as the cache headers allow. An inlined image is part of the page. So it is downloaded again each time a page with it loads. If the image lives in a shared stylesheet, that is better, because the stylesheet is cached. But a stylesheet also blocks rendering. The browser will not paint anything until it has the whole file, including every embedded image inside it.

A 150 KB hero photo is inlined into the HTML of a ten-page site. Each page is now 200 KB heavier. A visitor who reads all ten pages downloads 2 MB of Base64. With a normal file, they would download one cached 150 KB file. Now take a site with twenty 1 KB icons inlined in the stylesheet. A visitor downloads about 27 KB of icon data once. They also save twenty round trips on the first visit.

Two more points tip the balance. HTTP/2 and HTTP/3 send many small files over one connection at very little cost per request. So removing requests saves less than it did ten years ago. Also, an inlined image cannot be lazy-loaded, which means loaded only when needed. Nor can it be served in a different format to different browsers. It is a fixed lump of bytes in the document. Everyone gets it, whether they scroll to it or not.

HTML code displayed on a computer screen
Photo by César Gaviria on Pexels

When inlining helps

  • Tiny icons and UI glyphs. Think of anything under about 2 KB. For these, the HTTP headers alone are a big part of the transfer. Small SVG icons embedded as UTF-8 are the ideal case.
  • Single-file documents. This means a report, an invoice, an email-safe HTML attachment or an offline manual. It must open from one file, with no folder of assets beside it. Here inlining is not an optimisation. It is a must.
  • Placeholders. A blurred copy of a photo, 20 pixels wide, is a few hundred bytes. It makes a good low-quality placeholder while the real image loads.
  • Generated images. QR codes, charts drawn on a canvas and captcha-style graphics made in the browser are data URIs by nature. They never existed as files on a server.
  • Data payloads. An image stored inside a JSON record or a database field has to be text. Base64 is the standard way to do that.
  • Critical first paint. This is a small logo in the header that must show with the first byte of content. It must appear before any stylesheet or image request finishes.

When inlining hurts

  • Photographs and anything over about 10 KB. You pay the full overhead and lose the cache. A 150 KB photo should be a file.
  • Images used on more than one page. Shared logos, backgrounds and product images belong in the cache, downloaded once.
  • Images below the fold. These are images that start off screen. They should be lazy-loaded, and a data URI cannot be.
  • Email. Most major email apps will not show them.
  • Images you want found in image search. Search engines index image files at URLs. Images embedded as data URIs are generally not indexed in image search.
  • Pages with a strict Content Security Policy. This is a set of rules that limits what a page may load. The policy must allow data: in the img-src directive. If not, the images are blocked.
  • Anything you edit by hand. A 40,000-character string in the middle of a template is slow to scroll past. It is also easy to break.

A decision table

SituationInline as data URI?Why
SVG icon under 2 KB used in a stylesheetYesSaves a request; tiny overhead; cached with the CSS
PNG icon under 2 KB on one pageUsuallyThe 600-byte overhead is smaller than the request headers
Logo of 8 KB in the site header on every pageNoShared across pages; better cached once as a file
Hero photo of 150 KBNoAdds 50 KB per view and blocks rendering if in CSS
Blurred 20-pixel placeholder for a photoYesA few hundred bytes; shows at once while the real image loads
HTML invoice sent as a single fileYesNo outside files allowed; the overhead is the price of portability
Image in a marketing emailNoGmail and Outlook will not show it; host or attach it instead
Image stored in a JSON API responseYes, if smallJSON is text; for large images send a URL instead
Product image that should appear in image searchNoData URIs are not indexed as images

A sensible workflow

When an image does qualify for inlining, three steps keep the cost down.

  1. Shrink it first. Base64 multiplies whatever you give it by 1.33. So every byte you save before encoding saves 1.33 bytes in the page. Run photos and other pixel images through the Image Compressor. Resize icons to the exact pixel size they are shown at. Strip the metadata too. The EXIF Data Remover clears camera and location data from a photo. That data can add tens of kilobytes, and Base64 then makes it a third bigger. For an SVG, remove editor metadata and extra decimal places in the coordinates.
  2. Choose the right encoding. Pixel formats (PNG, JPEG, WebP, GIF) must use Base64. SVG is usually smaller as a plain UTF-8 data URI, with the few unsafe characters percent-encoded. It also compresses far better in transit.
  3. Measure the result. Check the page or stylesheet size before and after. If the inlined image adds more than a few kilobytes to something that loads on every page, move it back to a file.

Here is a good rule for the whole topic. Inline when the image is so small that the request would cost more than the bytes. Or inline when the document must stand alone. Otherwise, let the browser cache do its job.

Where to go next. Make a data URI, an image tag or a CSS rule from any image with the Image to Base64 Converter. Encode and decode plain text with the Base64 Encoder and Decoder. Make the image as small as possible first with the Image Compressor. All three run in your browser. Nothing is uploaded.

Frequently asked questions

How much bigger is a Base64 image than the original?

About 33 per cent bigger. Base64 turns every 3 bytes into 4 characters, so the text is four thirds the size of the file. It also adds up to 2 bytes of padding and a short data URI prefix. A 120 KB JPEG becomes roughly 160 KB of text. Server compression wins back only a few per cent. The image bytes are already compressed, and their Base64 form looks almost random.

Is it faster to use Base64 images?

Only for very small images. Inlining removes an HTTP request. That helps for icons of a kilobyte or two, where the request costs more than the bytes. For bigger images it is slower. The image is a third larger, and it is downloaded again with every page instead of being cached once. In a stylesheet it also delays the first paint. With HTTP/2, fewer requests save less than they used to.

Do Base64 images work in email?

Not reliably. Apple Mail and the built-in mail apps on iPhone and Mac show them. Gmail, Outlook on the web and most desktop Outlook versions do not show data-URI images, at the time of writing. You see a broken image or nothing. For email, host the image on a web server and link to it. Or attach it and point to it by content ID. Most readers will not see an embedded data URI.

Is Base64 encoding secure?

No. Base64 is an encoding, not encryption. It only writes bytes using 64 text characters. Anyone can decode it in an instant with a free tool or one line of code. It is used to carry binary data safely through systems that only accept text. It is not used to hide data. To keep an image or file private, encrypt it first. You can then Base64-encode the encrypted bytes for transport.

Should I use Base64 or a plain data URI for SVG images?

Use a plain UTF-8 data URI for SVG. SVG is already text, so you can put the markup straight after the comma. Just percent-encode a few unsafe characters, such as hashes and quotes. That is shorter than the Base64 version. It also compresses much better with gzip or Brotli. You only need Base64 for binary formats such as PNG, JPEG, WebP and GIF. Their bytes cannot appear in text.

Are Base64 images indexed by Google Images?

Generally not. Image search lists image files that have their own URL. An image embedded in the page as a data URI has no address of its own. So it is not treated as an image resource. Do you want product photos, diagrams or illustrations to show in image search results? Then serve them as normal files, with clear file names and alt text. Keep data URIs for icons and decorative graphics.

Try the tools from this guide

Keep reading

Related guides

All guides