HTML to Base64 Encoder – Free

Paste HTML markup and get it back as a standard Base64 string. This HTML to Base64 encoder converts the text as UTF-8 and changes nothing else, so every tag, space and attribute survives the round trip.

How to use the HTML to Base64 tool

  1. Paste the markup into the HTML box. Select Example to load a one-paragraph sample instead.
  2. Select Encode. The text is converted to UTF-8 bytes and then to Base64.
  3. Read the encoded string in the Result box.
  4. Select Copy and paste the value into your data URL, JSON field or template.

What HTML to Base64 encoding does

HTML is full of characters that mean something in other formats: double quotes, angle brackets, ampersands and line breaks. Putting markup inside a JSON string, an XML attribute or a URL means escaping each of them correctly. Base64 avoids the problem, because its output contains only letters, digits, +, / and =.

The tool works in two steps. It encodes the text as UTF-8, where an ASCII character is one byte and a character such as é is two. Then it applies RFC 4648 Base64 to those bytes. The markup is never parsed, so it is not validated, minified or sanitized, and unclosed tags encode as readily as a complete document.

A worked example

The sample paragraph is 12 ASCII characters, so 12 bytes, which fill four groups of three and give 16 characters without padding. The heading with é is 13 characters but 14 bytes, which leaves a final group of two bytes and one = sign.

Note the + in the first result. Angle brackets often produce + and / in standard Base64, and that matters as soon as the string goes into a URL.

<p>Hello</p>    ->  PHA+SGVsbG88L3A+
<h1>Café</h1>   ->  PGgxPkNhZsOpPC9oMT4=

Turning the result into an HTML data URL

Put data:text/html;base64, in front of the string and you have a URL that carries the whole document. It works as the src of an iframe. Browsers block links that send the top-level page to a data URL, so do not rely on it for navigation.

A data URL without a charset parameter is not guaranteed to be read as UTF-8. If the markup has non-ASCII characters, write data:text/html;charset=utf-8;base64, or include a meta charset tag in the document. The Data URI builder on this site joins a media type and the Base64 in one step.

data:text/html;base64,PHA+SGVsbG88L3A+

Things to know before you encode HTML

A few details of how this page works affect the bytes you get.

  • Leading and trailing spaces are kept. The encoder does not trim the box.
  • The browser's text box stores each line break as a single line feed, so markup pasted from a file with Windows line endings is encoded with LF, not CRLF. Use File to Base64 when you need the file's exact bytes.
  • The output is one line. MIME email parts need Base64 wrapped at 76 characters, which you must add yourself.
  • Base64 is not a security filter. Script tags and event handlers come back intact when the string is decoded.

Frequently asked questions

Does Base64 encoding protect against XSS?

No. Base64 only changes how the markup is written. Once decoded and inserted into a page, a script tag or an onerror attribute runs as usual. Sanitize untrusted HTML with a proper sanitizer before you render it.

Why does my encoded HTML contain + and / characters?

They are the last two characters of the standard Base64 alphabet and appear whenever a 6-bit group has the value 62 or 63. Inside a URL query, percent-encode them or convert the string to Base64URL with the Normalize Base64 tool.

Are indentation and line breaks preserved?

Yes. Spaces, tabs and line breaks are bytes like any other and are encoded. The only change is that the text box normalizes line endings to a single line feed character before encoding.

Can I encode a full page with images and scripts?

You can encode any markup, but only the text you paste is included. External images, stylesheets and scripts stay external references, and relative paths will not resolve once the document is loaded from a data URL.

How much longer is the Base64 than the original markup?

Every three bytes of UTF-8 become four characters. A 30 KB template grows to 40 KB. Characters outside ASCII count as two to four bytes each before that ratio applies.

Related tools