Skip to content

HTML to Base64 Converter

Paste HTML to get a Base64 string. Leave Data URI Header on for an iframe URL, or turn it off if your API expects only the encoded string.

Runs in your browser Free, no sign-up
Loading tool workspace...

What is HTML to Base64?

This HTML to Base64 converter encodes HTML markup as Base64. With the Data URI Header option on, the output is a complete data:text/html;base64,… URI that represents a whole HTML page in a single string, which you can use as an iframe src, open in some tools as a page, or include in test fixtures.

Turn the header off to get the raw Base64 string, which is what many APIs expect for HTML content, such as email-sending and PDF-rendering services. The markup is encoded as UTF-8, so non-English text and symbols are preserved exactly.

How to use HTML to Base64

  1. Paste your HTML into the input panel.
  2. Keep Data URI Header on for a data:text/html;base64 URI, or turn it off for raw Base
  3. Copy the output.
  4. Use it as an iframe src, in an API request body or in your test fixtures.

Key features

  • Encodes HTML to Base64 with UTF-8 support
  • Optional data:text/html;base64 header
  • Instant output as you paste
  • Copy or download the result
  • Runs entirely in your browser

Common use cases

  • Sending HTML bodies to email and PDF-generation APIs that require Base64
  • Loading test pages in iframes without hosting files
  • Embedding HTML templates in JSON or configuration
  • Creating self-contained fixtures for browser tests
  • Encoding HTML for systems that only accept ASCII-safe text

Technical details & standards

The HTML is converted to UTF-8 bytes and encoded with standard Base64 (RFC 4648), which is about 33% larger than the markup. With the header enabled, the text/html MIME type is included so the data URI is treated as an HTML document.

Browsers block top-level navigation to data: URLs for security reasons, so typing the URI into the address bar or linking to it may not work; it does work as an iframe src, subject to the page's Content Security Policy (frame-src must allow data:). An HTML data URI also runs in a unique origin, so it cannot access the embedding page's cookies or storage.

Convert HTML to Base64 in JavaScript and Python

To build the same data:text/html;base64 URI in your own code, encode the markup as UTF-8 and add the prefix.

JavaScript

const html = "<h1>Hello</h1>";
const uri = "data:text/html;base64," + btoa(html);
// "data:text/html;base64,PGgxPkhlbGxvPC9oMT4="

If the HTML contains characters outside Latin-1, such as emoji, encode it with TextEncoder first, or btoa throws an error.

Python

import base64

html = "<h1>Hello</h1>"
uri = "data:text/html;base64," + base64.b64encode(html.encode("utf-8")).decode("ascii")
# 'data:text/html;base64,PGgxPkhlbGxvPC9oMT4='

Frequently asked questions

How do I display Base64 HTML?

Set it as the src of an iframe: <iframe src="data:text/html;base64,…"></iframe>. Opening a data: URL directly in the address bar is blocked by most browsers.

How do I decode Base64 HTML back to markup?

Remove the data:text/html;base64, prefix and decode the rest with the Base64 Decode & Encode tool, or preview it by pasting the result into the HTML Viewer.

Why do APIs ask for HTML in Base64?

Base64 keeps the markup intact inside JSON or form fields without escaping quotes, angle brackets and line breaks, and avoids character-encoding problems.

Does Base64 encoding make HTML safe?

No. It only changes how the markup is written. Decoded HTML runs exactly as before, so sanitize any untrusted HTML before rendering it.

How do I make an HTML data URI?

Keep Data URI Header switched on and the output already starts with data:text/html;base64,. Without it, put data:text/html;base64, in front of the Base64 output.

Does Base64 make HTML smaller?

No. Base64 makes the content about 33% larger. It is used for safe transport, not compression.

Is my data safe?

Processing happens locally in your browser. Tool input is not sent to CodeUnpack servers.

Does it work offline?

Once the page has loaded, this tool can process input without an internet connection.

More encoders & decoders tools

View all