Data URLs and Base64: When to Embed Resources Inline vs Link to Them
Data URLs embed files directly in HTML and CSS as Base64 strings — no separate HTTP request. Here's when that's faster, when it makes things worse, how JWT tokens use Base64URL, and how inline images in emails work around image blocking.
By sadiqbd · June 9, 2026
Data URLs let you embed files directly in HTML and CSS — and the trade-offs are worth understanding
A standard web page loads images, fonts, and stylesheets as separate HTTP requests. Each request has overhead: DNS lookup, TCP connection, TLS handshake, server processing, transmission. For small, critical resources — a tiny icon, a custom font subset, a small background image — this overhead can exceed the actual data transfer time.
Data URLs solve this by embedding the binary resource directly in the HTML or CSS as a Base64-encoded string. No separate request. No round trip. The resource is in the document.
What a data URL looks like
<!-- External image (separate HTTP request) -->
<img src="https://example.com/icon.png" alt="Icon">
<!-- Data URL (no separate request) -->
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4BCRmhoaGhlMQhqmpqSmsBgAqggF/AAAAAElFTkSuQmCC" alt="Icon">
The format: data:[mediatype];base64,[data]
Components:
data:— the scheme, identifying this as a data URLimage/png— the MIME type of the embedded resource;base64,— indicates the data is Base64-encodediVBORw0K...— the Base64-encoded binary data
Where data URLs are used in practice
Inline SVG icons in CSS
One of the most practical uses: embedding SVG icons directly in CSS for background images, without an external file request:
.icon-check {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41L9 16.17z'/%3E%3C/svg%3E");
}
Note the URL-encoding (%3C for <, %3E for >) — SVG in CSS data URLs uses URL encoding rather than Base64 for readability and slightly smaller size.
Font subset embedding
A custom font loaded from an external file requires an HTTP request before text renders. For a small character subset (a monogram, a small icon font), embedding the font as a Base64 data URL in the CSS prevents a render-blocking request:
@font-face {
font-family: 'CustomIcon';
src: url('data:font/woff2;base64,d09GMgABAAA...') format('woff2');
}
Inline images in HTML emails
Email clients have limited and inconsistent support for externally hosted images (many block them by default). Data URLs sidestep this by embedding the image directly in the email HTML. The trade-off: emails become significantly larger (Base64 adds ~33% size overhead), which affects deliverability and loading time.
JWT tokens (semi-related)
JWT (JSON Web Tokens) use URL-safe Base64 (Base64URL — substitutes + with - and / with _, and omits padding = characters) to encode the header and payload. This isn't a data URL, but it uses the same Base64 encoding mechanism.
A JWT looks like: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyMTIzIn0.signature
Decoding the first part: eyJhbGciOiJIUzI1NiJ9 → {"alg":"HS256"} — the JWT header in plain JSON.
The trade-offs of data URLs
Advantages
No additional HTTP request: eliminates connection overhead entirely. For very small resources (under ~1 KB), this can be faster than a separate file request.
Simplifies deployment: a single file contains everything it needs, reducing dependencies on external resources. Useful for self-contained HTML reports, email templates, and offline-capable applications.
Always available: the resource is part of the document — it can't fail to load because an external URL is unreachable, a CDN has issues, or CORS restricts the resource.
Disadvantages
Size overhead: Base64 encoding adds approximately 33% to the data size. A 100KB image becomes ~133KB as a Base64 string.
Not cached separately: browsers cache external resources and reuse them across pages. A data URL is embedded in the document — it's "cached" as part of the page, but if the same image appears in ten pages, it's downloaded ten times (once per page) rather than once and reused.
Makes HTML/CSS harder to read and maintain: a 500-character Base64 string in your source code is difficult to work with.
Blocked in some contexts: some browser security policies and Content Security Policy (CSP) configurations block data URLs or limit their use. data: URLs are not allowed in certain contexts (e.g., as src for <script> tags in modern browsers).
When to use vs. avoid data URLs
Good candidates for data URLs:
- SVG icons under ~1 KB embedded in CSS
- Tiny decorative images (loading spinners, small backgrounds)
- Font subsets under ~5 KB for critical icon fonts
- Inline images in emails where external image blocking is a concern
- Self-contained single-file HTML applications
Poor candidates:
- Photographs and large images (33% size overhead is significant; no caching benefit)
- Resources shared across many pages (caching would be more efficient)
- Any resource larger than ~10 KB (bulk of the page load should come from cached resources, not inlined data)
How to use the Base64 Encoder/Decoder on sadiqbd.com
Encoding (file to Base64):
- Input the text, binary content, or upload the file
- The encoder outputs the Base64 string
- Prepend the data URL prefix:
data:[mediatype];base64, - Use the complete data URL in HTML/CSS
Decoding (Base64 to content):
- Paste the Base64 string (without the
data:...;base64,prefix) - The decoder outputs the original content
For JWT inspection: paste the full JWT token, and decode each section to inspect the header and payload.
Frequently Asked Questions
Why does Base64 add 33% to the file size? Base64 represents 3 bytes of binary data as 4 ASCII characters. Each ASCII character is 1 byte, so 3 bytes → 4 bytes: 33% overhead. The encoding is necessary because binary data contains bytes that don't have ASCII representations and can't safely appear in text contexts.
What's the difference between Base64 and Base64URL?
Standard Base64 uses +, /, and = as special characters. These have reserved meanings in URLs, so Base64URL replaces + with -, / with _, and omits the = padding. JWTs, URL-safe tokens, and some APIs use Base64URL.
Is the Base64 Encoder/Decoder free? Yes — completely free, no sign-up required.
Data URLs are a useful tool in specific situations — primarily for small, critical resources where eliminating an HTTP request outweighs the size overhead and loss of separate caching.
Try the Base64 Encoder/Decoder free at sadiqbd.com — encode any text or file to Base64, or decode any Base64 string back to its original content.