Base64 is an encoding that turns any data into text using 64 characters (A–Z, a–z, 0–9, + and /). It works in groups of 3 bytes, which become 4 characters, so encoded data is about 33% larger. It's used for email attachments, data URIs and API tokens. It isn't encryption: anyone can decode it.
How does Base64 encoding work?
Computers store data as bytes of 8 bits. Base64 takes three bytes at a time (24 bits) and splits them into four groups of 6 bits. Six bits can hold a number from 0 to 63, and each number maps to one character in the Base64 alphabet. If the data doesn't divide into groups of three, the output is padded with one or two = signs.
01001000i01101001!00100001010010S000110G100100k100001hWhere Base64 is used
- Data URIs: small images or fonts embedded straight into HTML or CSS, like
data:image/png;base64,…. - Email: attachments are Base64-encoded so they can travel through systems built for text.
- APIs and auth: HTTP Basic auth sends
username:passwordas Base64, and JSON Web Tokens use URL-safe Base64 for their parts. - Config and JSON: certificates, keys and binary values stored in text files.
Standard vs URL-safe Base64
| Standard | URL-safe | |
|---|---|---|
| Characters 62 and 63 | + and / | - and _ |
| Padding | = at the end | Usually removed |
| Used in | Email, data URIs, most APIs | URLs, file names, JWTs |
The decoder accepts both, so you can paste either kind.
How to encode and decode Base64 in code
JavaScript (browser)
// btoa/atob only handle Latin-1, so convert text to UTF-8 bytes first
const bytes = new TextEncoder().encode("Hi! ✓");
const b64 = btoa(String.fromCharCode(...bytes));
const back = new TextDecoder().decode(Uint8Array.from(atob(b64), (c) => c.charCodeAt(0)));Node.js
Buffer.from("Hi!").toString("base64"); // "SGkh"
Buffer.from("SGkh", "base64").toString("utf8"); // "Hi!"Python
import base64
base64.b64encode("Hi!".encode()).decode() # 'SGkh'
base64.b64decode("SGkh").decode() # 'Hi!'Command line (macOS and Linux)
echo -n 'Hi!' | base64 # SGkh
echo 'SGkh' | base64 --decodeShould I embed images as Base64?
Only very small ones, like tiny icons. A Base64 image is about a third bigger than the file, can't be cached separately from the page, and makes your HTML or CSS heavier. For anything over a few kilobytes, a normal image file in a modern format like WebP or AVIF loads faster. Check page weight with the Website SEO Audit.