GameParticles
Images

Base64 Image Data URIs: Syntax, Size Cost and Examples

A Base64 image is a file written as text inside a data URI. See the syntax, HTML and CSS examples, the 33 percent size overhead, and when inlining is worth it.

Updated

A Base64 image is an image file converted to text so it can be written directly inside HTML, CSS or JSON. The usual wrapper is a data URI, which looks like data:image/png;base64,iVBORw0KGgo... and works anywhere a normal image URL does. The trade-off is size: the text is about 33 percent larger than the file, so inlining pays off only for small images.

Data URI syntax

A data URI has four parts, defined in RFC 2397:

data:[media type][;base64],[data]

PartExampleMeaning
Schemedata:Says the content is inline, not fetched
Media typeimage/pngTells the browser how to interpret the bytes
Encoding flag;base64The data is Base64. Without it, the data is URL-encoded text
DataiVBORw0KGgo...The file content, after a comma

Common media types for images are image/png, image/jpeg, image/gif, image/webp, image/avif, image/svg+xml and image/x-icon. If the media type is left out, the default is plain text, so always include it for images.

You can often recognize the format from the first characters of the data, because every file type starts with fixed bytes:

Data starts withFormat
iVBORw0KGgoPNG
/9j/JPEG
R0lGODGIF
UklGRWebP
PHN2Zy or PD94bWwSVG

For how the encoding itself works, including the alphabet and the = padding, see Base64 encoding explained.

HTML and CSS examples

In HTML, the data URI goes where the file path would go:

<img src="data:image/png;base64,iVBORw0KGgo..." alt="Logo" width="32" height="32">

In CSS, it goes inside url():

.icon { background-image: url("data:image/png;base64,iVBORw0KGgo..."); }

It also works for a favicon, with <link rel="icon" href="data:image/png;base64,...">, and in JavaScript, where canvas.toDataURL("image/png") returns exactly this kind of string. Quote the URI in CSS. Do not add line breaks or spaces inside the data.

Convert an image to Base64

  1. Open Image to Base64 and keep the Image → Base64 mode selected.
  2. Drop an image onto the drop area, or click to browse. PNG, JPG, GIF, SVG, WebP, AVIF and ICO files are accepted.
  3. Pick the output format: Data URI, Base64 only, CSS background or HTML img. The last two wrap the data URI in a ready-made background-image rule or an img tag.
  4. Read the stats beside the preview: original size, Base64 size, output size and the size increase as a percentage.
  5. Click Copy output and paste it into your code.

To go the other way, switch to Base64 → Image and paste a data URI or a raw Base64 string. A data URI carries its own media type, so the format is detected for you. For a raw string, choose PNG, JPG or WebP under Save as. A preview appears if the data is valid, and the button downloads the decoded file.

The separate Base64 Encode / Decode tool is for text, such as an API credential or a JSON snippet. Use the image tool for files.

The size overhead, with numbers

Base64 represents every 3 bytes of the file as 4 text characters. The encoded length is the file size divided by 3, rounded up, times 4. That is an increase of one third.

Original fileBase64 lengthIncrease
1 KB (1,024 bytes)1,368 characters33.6 percent
10 KB (10,240 bytes)13,656 characters33.4 percent
100 KB (102,400 bytes)136,536 characters33.3 percent

The few extra tenths of a percent come from rounding up to a multiple of four, plus the short data:image/png;base64, prefix.

Servers usually compress HTML and CSS with gzip or Brotli, which claws back some of the overhead. It does not remove it. PNG, JPEG and WebP data is already compressed, so the text version of it compresses poorly, and the inlined file still ends up larger than the binary original.

When inlining an image makes sense

  • Tiny icons and placeholders, a few kilobytes at most, where avoiding an extra request is worth more than the added bytes.
  • Single-file documents: an HTML report, an exported invoice or an offline page that must work without an assets folder.
  • Blurred preview placeholders that show instantly while the full image loads.
  • Data passed through text-only channels, such as an image field in a JSON API request.
  • Generated images in JavaScript, where a data URI is the natural output of a canvas.

When it does not

  • Photos and large graphics. A 200 KB hero image adds about 267 KB of text to the page.
  • Images used on many pages. A normal file is downloaded once and cached. An inlined image is downloaded again inside every page or stylesheet that contains it.
  • CSS that blocks rendering. A stylesheet full of embedded images delays the first paint, because the browser must download all of it before showing the page.
  • Images that should be lazy-loaded or served in different sizes for different screens.
  • HTML email, with care. Some email clients, including Gmail's web interface, do not display data URI images, so test before relying on them.

With HTTP/2 and HTTP/3, many small files can share one connection, so the old argument for inlining to save requests is weaker than it used to be. A site with a strict Content Security Policy also needs data: allowed in img-src before inlined images will show.

Rule of thumb: inline an image only if it is small enough that you would not mind reading it in a diff. Shrink it first, then encode it.

To shrink an icon before encoding, run it through the Image Compressor.

SVG is a special case

SVG is already text, so it does not need Base64. You can URL-encode it instead, which is usually shorter and stays readable:

url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Ccircle cx='5' cy='5' r='4'/%3E%3C/svg%3E")

Note that there is no ;base64 flag. Characters such as <, > and # must be percent-encoded, and using single quotes inside the SVG avoids clashing with the double quotes around the URL. Base64 still works for SVG and is the safer choice when a tool or template mangles special characters.

Try it

Drop an icon into Image to Base64, switch between Data URI, CSS background and HTML img output, check the size increase, and copy the result straight into your stylesheet or markup.

Tools in this guide

Frequently asked questions

What is a Base64 image?

It is an image file whose bytes have been rewritten as plain text using the 64-character Base64 alphabet. The text can be pasted into HTML, CSS or JSON, usually inside a data URI, so the image travels with the document instead of as a separate file.

What is a data URI?

A data URI is a URL that contains the file itself instead of pointing to it. It starts with data:, then a media type such as image/png, then ;base64 and a comma, followed by the encoded content.

How much bigger is a Base64 image than the original?

About 33 percent. Base64 turns every 3 bytes into 4 characters, so a 30 KB image becomes roughly 40 KB of text. Gzip or Brotli compression on the server recovers part of that, but not all of it.

Should I use Base64 images on my website?

Only for very small images, such as icons of a few kilobytes, or when a document must be a single file. Larger images are better as normal files, which browsers can cache, load in parallel and lazy-load.

More images guides

JSON Formatter
Developer · Format, validate and minify JSON with syntax highlighting.
↵
Base64 Encode / Decode
Developer · Encode text to Base64 or decode Base64 back to plain text.
↵
URL Encoder / Decoder
Developer · Encode or decode URL components and query strings.
↵
Hash Generator
Developer · Generate MD5, SHA-1, SHA-256 and SHA-512 hashes.
↵
UUID Generator
Developer · Generate UUIDs v4 (random) in bulk with one click.
↵
Regex Tester
Developer · Test regular expressions against text with live match highlighting.
↵
Word Counter
Text · Count words, characters, sentences, paragraphs and reading time.
↵
Case Converter
Text · Convert text between UPPER, lower, Title, camelCase, snake_case and more.
↵
List Sorter
Text · Sort, reverse, deduplicate and shuffle lists of text.
↵
Markdown Preview
Text · Write Markdown and see a live rendered preview side by side.
↵
Percentage Calculator
Numbers · Calculate percentages, increases, decreases and what % one number is of another.
↵
Color Converter
Colors · Convert colors between HEX, RGB, HSL and HSV instantly.
↵
Snake
Arcade · Eat food, grow longer, don't hit the walls. A timeless classic.
↵
2048
Puzzle · Slide tiles and combine them to reach the legendary 2048 tile.
↵
Word Grid
Puzzle · Guess the hidden five letter word in six tries using color clues.
↵
Sudoku
Puzzle · Fill the 9x9 grid with logic. Four difficulties from easy to expert.
↵
Minesweeper
Strategy · Use number clues to clear the minefield. Classic logic, three levels.
↵
Breakout
Arcade · Smash every brick with paddle and ball. Power-ups, combos, endless levels.
↵
Typing Practice
Casual · Build your WPM and accuracy with timed drills from 30 seconds to 5 minutes. Find your weak keys.
↵
TypeRunner
Arcade · Multiplayer typing race. Race friends by link, beat computer runners, or chase your own ghost.
↵
Typing Blocks
Arcade · Type each word before its wooden block lands. Miss, and the pile climbs toward the line.
↵
Bubble Shooter
Arcade · Aim, bank shots off the walls, and pop groups of three or more bubbles.
↵
Road Rush
Arcade · Race seven rivals through traffic and punch your way to the front.
↵