What is Base64 and When to Use It
Base64 is a binary-to-text encoding scheme that represents binary data using a 64-character ASCII alphabet (A-Z, a-z, 0-9, +, /). It works by taking every three bytes (24 bits) of input and splitting them into four 6-bit groups, each mapped to one of the 64 characters. When the input length is not a multiple of three, = padding characters are appended to signal the decoder how many bytes to discard.
This 33% size overhead is the cost of safe transport through text-only channels. Common use cases include:
- Data URIs: Embedding small images directly in HTML or CSS (
data:image/png;base64,...) eliminates an extra HTTP request, improving performance for icons and sprites under ~2 KB. - Email attachments (MIME): SMTP is a 7-bit protocol, so binary files must be Base64-encoded before being attached. The MIME standard (RFC 2045) mandates this encoding.
- API payloads: When you need to transmit binary blobs inside JSON — which only supports text — Base64 is the standard approach. JWTs use a URL-safe variant called Base64URL that replaces
+/with-_and omits padding.