Skip to content
TabShedKitsMy desk

When to Use Base64 and When Not To: Data URIs, base64url and the Size Cost

Updated 5 min readBy

What Base64 is for, why it makes data about a third bigger, how base64url differs, and how to use the Base64 tool for text, files and images.

Base64 Encoder & DecoderEncode and decode Base64
Open Base64 Encoder & Decoder
On this page
  1. What Base64 does
  2. Text, UTF-8 and emoji
  3. Standard Base64 and base64url
  4. Encode a file or an image
  5. Decode
  6. When to use Base64
  7. When not to use it
  8. Common mistakes
  9. A quick size check
  10. Frequently asked questions

Base64 shows up in a lot of places: an image inside a CSS file, the middle part of a login token, an e-mail attachment, a value in a JSON response that looks like random letters ending in an equals sign. It is often mistaken for a way to hide data, which it is not. This guide explains what Base64 does, when it helps, when it hurts, and how to use the Base64 Encoder and Decoder for each case.

What Base64 does

Some channels can only carry text: JSON, URLs, e-mail bodies, HTML attributes. Binary data, such as an image or a file, contains bytes that those channels would corrupt. Base64 rewrites any bytes as plain letters, digits and two symbols, so the data survives the trip. Every three bytes become four characters, which is why Base64 text is roughly a third larger than the original. The tool shows the change in size under the output, for example "+33%" when encoding.

It is an encoding, not encryption. Anyone can decode it in a second, without a key. Never use it to hide a password, a key or personal information.

Text, UTF-8 and emoji

The tool first turns your text into UTF-8 bytes and then encodes those bytes. That is why accents, Chinese, Japanese and Korean text and emoji come back unchanged after decoding. Older tools that use the browser's plain function for strings fail on such characters. Try the sample in the tool: it contains accented letters and a check mark and round-trips correctly.

Standard Base64 and base64url

Standard Base64 uses the characters + and /, and pads the end with = signs. Those characters have special meanings in URLs and file names. The URL-safe variant, base64url, replaces + with - and / with _, and drops the padding. Choose it for tokens, query values and file names. In the tool, switch on URL-safe (base64url) when encoding. JSON Web Tokens use base64url for their header and payload; you can read them with the JWT Decoder.

Standard Base64base64url
Characters 62 and 63+ and /- and _
Padding with =YesNo
Typical useE-mail, data URIs, many APIsJWTs, URLs, file names

Encode a file or an image

  1. Choose Encode to Base64 and switch the input to File.
  2. Choose a file. The size limit is 10 MB; a larger file is refused with a message.
  3. Copy the result, or download it as encoded.base64.

To make a data URI, put the prefix in front of the encoded text yourself: data:image/png;base64, followed by the characters. It is useful for tiny images, such as a small icon in CSS, because it saves one request. For anything larger it is a bad trade: the data is a third bigger, it cannot be cached separately from the page, and it makes the HTML or CSS file heavy. If an image is large, serve it as a file, and make it smaller first with the Image Compressor.

Decode

Choose Decode from Base64 and paste the text, or load a text file with the encoded data. What you see depends on what the bytes are:

  • Text: shown in the output box.
  • An image: the tool recognises common image types and shows a preview; the download is named decoded with the right extension.
  • Other binary data: it cannot be shown as text, so you download it as decoded.bin.

The tool also accepts the base64url form when decoding. If it says "Invalid Base64 string", the input has characters outside the alphabet or a broken length. Remove spaces, the data: prefix and any quotes around the text, and try again.

When to use Base64

  • Putting a tiny image or font into CSS or HTML.
  • Sending a small binary value inside JSON.
  • Reading a value in a header, token or log that you know is encoded.
  • Moving short binary data through a system that only takes text.

When not to use it

  • To protect data. It protects nothing. For passwords use a proper hash: see the Password Hash tool and the guide about it.
  • For large files in JSON or HTML. The size cost and parsing time add up; upload the file and refer to it by URL instead.
  • To shrink data. It does the opposite.

Common mistakes

  • Pasting the whole data URI into the decoder. Remove the prefix up to and including the comma.
  • Decoding a URL-safe string with a decoder that wants standard characters. Convert - and _ back to + and /, or use this tool, which handles both.
  • Treating the equals signs as part of the data. They are only padding.
  • Double encoding. If the result of decoding looks like more Base64, it was encoded twice. Swap direction and decode again.

A quick size check

Because every three bytes become four characters, you can estimate the result before encoding: multiply the original size by four thirds. A 30 KB icon becomes about 40 KB of text, and a 3 MB photo about 4 MB, which is a poor thing to embed in a page. The "+33%" figure in the tool is this effect. If your encoded text is much longer than that, something is wrong: for example, the input was already Base64 and you encoded it a second time. A string that is shorter than the original after decoding is normal; that is the reverse of the same rule, and the tool shows it as a percentage smaller.

Frequently asked questions

Is my data uploaded?

No. Encoding and decoding happen in your browser.

Why is the encoded text longer?

Three bytes become four characters, so about a third more.

Can I use Base64 in a URL?

Use the URL-safe form, or percent-encode the result with the URL Encoder and Decoder.

What is the swap button for?

It moves the output to the input and flips the direction, so you can check a round trip.