CSS to Base64 Encoder – Free

Paste a stylesheet or a few rules and get the Base64 form. This CSS to Base64 encoder reads the source as UTF-8 text and keeps every selector, comment and space as typed.

How to use the CSS to Base64 tool

  1. Paste your stylesheet or rule set into the CSS box, or select Example to load body { color: #333; }.
  2. Select Encode to convert the stylesheet text.
  3. Read the Base64 string in the Result box. It uses the standard alphabet and keeps = padding.
  4. Select Copy, then add data:text/css;base64, in front if you need a data URL.

How this CSS to Base64 encoder works

A stylesheet is plain text, so the conversion has two stages. The source is turned into UTF-8 bytes, then the bytes are written in Base64 as defined by RFC 4648. Most CSS is ASCII and costs one byte per character. Characters outside ASCII, which turn up in content strings, font family names and comments, take two to four bytes each.

Nothing is parsed along the way. The tool does not check syntax, strip comments, minify, or follow @import and url() references. What you paste is what gets encoded, so run your minifier first if size matters.

A worked example

The sample rule is 21 characters long. Twenty-one bytes divide evenly into seven groups of three, so the result is 28 characters with no padding. The minified heading rule is 13 bytes, which leaves one byte in the last group and two = signs.

Whitespace is data. Removing the spaces from the first rule would change its Base64 from the sixth character on, even though a browser applies both versions identically.

body { color: #333; }  ->  Ym9keSB7IGNvbG9yOiAjMzMzOyB9
h1{color:red}          ->  aDF7Y29sb3I6cmVkfQ==

Loading a Base64 stylesheet from a data URL

Browsers accept a text/css data URL anywhere a stylesheet URL is allowed, including a link element and an @import rule. That lets you ship a page and its styles as one file, or inject a stylesheet from a script without a second request.

Two things break in practice. Relative url() references inside the encoded stylesheet have no folder to resolve against, so fonts and images must use absolute URLs. And a Content Security Policy must list data: under style-src, or the browser refuses to load the sheet.

<link rel="stylesheet" href="data:text/css;base64,aDF7Y29sb3I6cmVkfQ==">

@import url("data:text/css;base64,aDF7Y29sb3I6cmVkfQ==");

What this encoder is not for

Two nearby tasks look similar and need a different tool or value.

  • Embedding an image in CSS. That needs the image file encoded, not the stylesheet. The CSS Data URI Converter on this site returns a ready background-image declaration.
  • A CSP hash for an inline style. The value in style-src 'sha256-...' is the Base64 of a SHA-256 digest of the style text, not the Base64 of the CSS itself.
  • Making CSS smaller. Base64 output is a third larger than its input. Compression on the server recovers part of that, never all of it.

Frequently asked questions

Can a browser load CSS from a Base64 string?

Yes, through a data URL. Write data:text/css;base64, followed by the encoded string and use it as the href of a stylesheet link or inside @import url(). The browser decodes it and applies the rules.

Should I minify CSS before encoding it?

Usually yes. The encoder keeps comments, indentation and blank lines, and each of those bytes costs about 1.33 characters of output. Minify first, then encode the minified text.

How do I embed an image in CSS as Base64?

Encode the image, not the CSS. Use the CSS Data URI Converter, choose a PNG, JPEG, GIF or WebP file, and paste the declaration it returns into your rule.

Does the encoder check my CSS for errors?

No. Any text is accepted, including rules with missing braces or unknown properties. Validation is left to your linter or the browser, which reports problems only after the stylesheet is decoded and parsed.

Why do relative paths stop working in an encoded stylesheet?

A relative path is resolved against the stylesheet's own URL. A data URL has no directory, so there is nothing to resolve against. Rewrite references to fonts and images as absolute URLs before encoding.

Related tools