Skip to content
← All Tools
๐Ÿ”’All processing in your browser ๐ŸšซNo uploads stored ๐Ÿ›ก๏ธPrivacy-first conversion tools โœ“No login required
Guide

Base64 Encoding Explained: When and Why Developers Use It

Bill Crawford — Developer Guide — 2026  ยท  Last updated September 09, 2025

If you've been developing for any length of time you've encountered Base64 โ€” a long string of seemingly random letters, numbers, plus signs, and slashes with an = or == at the end. It appears in auth headers, CSS files, JSON payloads, and email attachments. This guide explains what Base64 is, why it exists, and every situation where you'll encounter it.

Connect on LinkedIn โ†’

Encode or decode Base64 instantly: Our free Base64 tool runs entirely in your browser โ€” your data never leaves your device.

Open Base64 Encoder โ†’

Table of Contents

  1. What Base64 Is
  2. Why Base64 Exists
  3. The = Padding Characters
  4. Base64URL vs Standard Base64
  5. Where You'll Encounter Base64
  6. What Base64 Is NOT
  7. Code Examples
  8. Data URLs: Embedding Files in CSS and HTML

What Base64 Is

Base64 converts binary data into a string of 64 printable ASCII characters: Aโ€“Z (26), aโ€“z (26), 0โ€“9 (10), plus + and /, with = for padding. Three bytes (24 bits) of input become exactly four Base64 characters, increasing size by approximately 33%.

Why Base64 Exists

The internet's core protocols were designed for 7-bit ASCII text. Email (SMTP), HTTP headers, and many other protocols couldn't reliably transmit binary data โ€” certain byte values would be misinterpreted as control characters or protocol delimiters, corrupting the data. Base64 encodes binary into a subset of ASCII that is safe for any text-based protocol.

The = Padding Characters

Base64 encodes three bytes at a time. If the input isn't divisible by 3, padding characters are added: one = if 2 bytes remain, two == if 1 byte remains.

Base64URL vs Standard Base64

Standard Base64 uses + and / which are special characters in URLs. Base64URL replaces them with - and _ and often omits padding. You'll see Base64URL in JWTs, OAuth tokens, and URL-safe cookies.

Where You'll Encounter Base64

HTTP Basic Authentication

The string username:password is Base64-encoded in the Authorization header: Authorization: Basic dXNlcjpwYXNz. Decode it and you get the credentials. This is why Basic Auth is insecure over HTTP โ€” Base64 is not encryption.

JSON Web Tokens (JWTs)

A JWT's three parts are Base64URL-encoded. The header and payload are readable by anyone with the token โ€” only the signature requires the secret to verify. Never store sensitive data in a JWT payload.

Data URIs in CSS

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

Small images embedded directly in CSS eliminate HTTP requests. Useful for icons under 4โ€“5KB; for larger images a separate file is more efficient.

Email Attachments

MIME email attachments are Base64-encoded within the message body. This is why raw email files are larger than their attachments and why opening a raw email shows encoded content.

Storing Binary Data in JSON

JSON is a text format and cannot contain raw binary. APIs that transfer files, images, or encrypted payloads via JSON Base64-encode them. This is common in document processing and image recognition APIs.

PEM Certificates and Keys

PEM files (.pem for SSL certificates, SSH keys) are Base64-encoded DER binary wrapped in -----BEGIN CERTIFICATE----- headers. When you copy an SSH public key, you're looking at Base64.

What Base64 Is NOT

Base64 is not encryption. Decoding requires no key โ€” it's a completely reversible transformation that anyone can perform. Never use it as a security measure. Base64 is not compression โ€” it makes data larger. Use gzip for compression; use Base64 when embedding binary in text.

Code Examples

// JavaScript (browser)
btoa("Hello, World!")           // "SGVsbG8sIFdvcmxkIQ=="
atob("SGVsbG8sIFdvcmxkIQ==")   // "Hello, World!"

// Node.js
Buffer.from("Hello").toString("base64")    // "SGVsbG8="
Buffer.from("SGVsbG8=", "base64").toString() // "Hello"

# Python
import base64
base64.b64encode(b"Hello").decode()   # "SGVsbG8="
base64.b64decode("SGVsbG8=")         # b"Hello"

Data URLs: Embedding Files in CSS and HTML

One of the most practical uses of Base64 in web development is Data URLs โ€” embedding binary file content directly into HTML or CSS as a text string, eliminating a network request entirely.

/* Embedding a small PNG icon directly in CSS */
.icon {
  background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...");
}

/* Embedding a font in CSS */
@font-face {
  src: url("data:font/woff2;base64,d09GMgABAAAAAAK...") format("woff2");
}

/* Inline image in HTML */
<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0i..." alt="Logo">

The format is: data:[mediatype];base64,[encoded-data]. The media type tells the browser how to interpret the data โ€” image/png, image/svg+xml, application/pdf, etc.

When to use Data URLs: Small images (icons under ~1 KB) where eliminating a round-trip HTTP request is worth the ~33% size overhead. Critical above-the-fold resources in the <head> that you want to load with zero extra requests.

When NOT to use Data URLs: Large files โ€” Base64 inflates size by ~33% and the browser cannot cache a Data URL separately from the page. Images over 1โ€“2 KB are almost always better served as separate files.

URL-Safe Base64

Standard Base64 uses + and / characters which have special meaning in URLs. Base64url (URL-safe Base64) replaces these with - and _ respectively and omits the = padding. This variant is used in JWT tokens, OAuth tokens, and anywhere Base64 appears in a URL or HTTP header:

// Standard Base64
SGVsbG8gV29ybGQ=

// Base64url (URL-safe)
SGVsbG8gV29ybGQ   โ† no padding, - and _ instead of + and /

Further reading: MDN โ€” Base64 Encoding ยท RFC 4648 โ€” Base Encodings

BC
Bill Crawford
Founder, Data Conversion Center

Bill Crawford is a data systems developer and technical founder with over 30 years of professional experience in accounting, finance, and business operations.

He holds a Bachelor's degree in Accounting and has spent more than three decades working within financial and operational environments. Over the past 10 years, he has been heavily involved in the development, implementation, and refinement of financial and enterprise data systems for both Fortune 500 companies and smaller organizations.

His work bridges finance and technology — combining deep domain knowledge in structured reporting and accounting workflows with hands-on SQL development and database architecture experience.

Bill founded DataConversionCenter.com to build practical, browser-based tools that simplify complex data challenges, including:

Rather than focusing on theoretical examples, his tools and articles are informed by real-world challenges encountered in enterprise reporting systems, financial databases, and operational data environments.

Professional Background

Bill's mission is to reduce friction in data workflows — particularly for professionals working with structured financial, operational, and reporting data.