You’ve probably seen those strange-looking strings that start with \"data:image/png;base64,\" followed by a wall of letters and numbers. That’s Base64 encoding in action. It’s a fundamental technique for converting binary data into text—and understanding it unlocks everything from embedding images in CSS to handling API authentication tokens.
Key Takeaways
- 1Base64 converts binary data to text using 64 ASCII characters—it’s encoding, NOT encryption
- 2Output is always 33% larger than input (3 bytes → 4 characters)
- 3Data URLs embed small files inline: data:image/png;base64,... eliminates HTTP requests
- 4Base64URL (using - and _ instead of + and /) is safe for URLs and used in JWTs
- 5Use for assets < 10KB; larger files should remain as separate cacheable requests
What Is Base64 Encoding?
Scenario
Encode the text "Hello" to Base64
Solution
"Hello" → "SGVsbG8=" — Each character is converted to its binary representation, then grouped into 6-bit chunks and mapped to Base64 characters. The = is padding because the input isn't a multiple of 3 bytes.
| Original | Base64 Encoded | Size Increase |
|---|---|---|
| Hello | SGVsbG8= | 60% |
| Test | VGVzdA== | 100% |
| Base64 | QmFzZTY0 | 33% |
| A | QQ== | 300% |
2How Base64 Encoding Works
Encoding Process
Convert to binary
Each byte of input becomes 8 bits. "Hi" → 01001000 01101001
Group into 6-bit chunks
Concatenate all bits and split into 6-bit groups: 010010 000110 1001xx (pad with zeros if needed)
Map to Base64 alphabet
Each 6-bit value (0-63) maps to a character: A-Z (0-25), a-z (26-51), 0-9 (52-61), + (62), / (63)
Add padding
If input bytes aren't divisible by 3, add = padding. 1 remaining byte → ==, 2 remaining bytes → =
3Common Use Cases
- **Data URLs** – Embed images directly in HTML/CSS without separate HTTP requests
- **Email attachments** – MIME encoding uses Base64 to send binary files as text
- **API authentication** – HTTP Basic Auth encodes username:password in Base64
- **JWT tokens** – JSON Web Tokens use Base64URL encoding for header and payload
- **Storing binary in JSON/XML** – Text formats can\
- ,
Scenario
Embed a small icon directly in CSS without an HTTP request
Solution
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEU..."); — The browser decodes the Base64 string and renders the image inline.
Data URLs: Embedding Files Inline
| Use Case | Data URL Format | Best For |
|---|---|---|
| PNG image | data:image/png;base64,... | Icons, small graphics < 10KB |
| SVG image | data:image/svg+xml;base64,... | Vector icons, logos |
| JPEG photo | data:image/jpeg;base64,... | Thumbnails only (size overhead) |
| PDF document | data:application/pdf;base64,... | Inline PDF viewing |
| JSON data | data:application/json;base64,... | Embedded configuration |
| Plain text | data:text/plain;base64,... | Text content |
Convert Images to Data URLs
Use our Image to Base64 tool to instantly convert any image to a Data URL you can paste directly into your code.
Open Image to Base645Base64 in API Authentication
Scenario
Authenticate with username "admin" and password "secret123"
Solution
Authorization: Basic YWRtaW46c2VjcmV0MTIz — The value is Base64("admin:secret123"). The server decodes it to extract credentials.
| Method | Pros | Cons |
|---|---|---|
| Basic Auth (Base64) | Simple, widely supported, stateless | Credentials sent every request, not encrypted, no expiration |
| Bearer Token (JWT) | Contains claims, can expire, doesn't expose password | More complex, token can be stolen, larger header size |
6Base64 vs Base64URL
| Feature | Standard Base64 | Base64URL |
|---|---|---|
| Alphabet | A-Z, a-z, 0-9, +, / | A-Z, a-z, 0-9, -, _ |
| Padding | = (required) | = (often omitted) |
| URL safe | No (needs encoding) | Yes |
| Used in | Email, data URLs | JWTs, URL parameters |
Performance Considerations
- **DO** use Data URLs for critical above-the-fold icons that would otherwise block render
- **DO** use Base64 for small assets (< 10KB) in CSS to reduce HTTP round-trips
- **DON\
- ,
- T** embed Base64 images in JavaScript bundles (they can\
8Working with Base64
| Language | Encode | Decode |
|---|---|---|
| JavaScript | btoa(string) | atob(string) |
| Node.js | Buffer.from(str).toString("base64") | Buffer.from(b64, "base64").toString() |
| Python | base64.b64encode(bytes) | base64.b64decode(string) |
| PHP | base64_encode($data) | base64_decode($data) |
| Command line | echo -n "text" | base64 | echo "dGV4dA==" | base64 -d |
Encode & Decode Online
Use our free Base64 tool to quickly encode text, decode strings, or convert files—all in your browser with no data sent to servers.
Open Base64 Encoder