Base64 Encode & Decode

Encode an Image to Base64

Embed images in HTML & CSS with data URIs

Encoding an image to Base64 turns the file into a text string you can embed directly inside HTML or CSS — no separate image request needed. That string is usually wrapped as a data URI.

What a Base64 image (data URI) looks like

A data URI has three parts: the MIME type, the base64 marker, and the encoded data:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...

Encode an image in the browser (JavaScript)

FileReader.readAsDataURL() is the reliable way — it produces the full data URI, correctly handling the binary bytes:

const input = document.querySelector("input[type=file]");
input.addEventListener("change", () => {
  const reader = new FileReader();
  reader.onload = () => console.log(reader.result); // data:image/png;base64,...
  reader.readAsDataURL(input.files[0]);
});

Encode an image on the command line

# Linux (no line wrapping)
base64 -w0 logo.png > logo.txt

# macOS
base64 -i logo.png -o logo.txt

# Build a full data URI (Linux)
echo "data:image/png;base64,$(base64 -w0 logo.png)"

Use it in HTML and CSS

<img src="data:image/png;base64,iVBORw0KG..." alt="Logo" />

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

When to use — and avoid — Base64 images

  • Good for: tiny icons, sprites, and email templates, where cutting an HTTP request helps.
  • Costs ~33% more size: Base64 always inflates data by about a third.
  • Not cached separately: the image re-downloads with every page that inlines it.
  • Avoid for large images — a normal <img> file is faster and cacheable.

Working with text instead of images? Use the Base64 encoder/decoder, or the JSON formatter for JSON payloads.