Every external image on a page is a file the browser has to fetch. For a full photo that is obviously worth it. For a tiny icon that appears on every page, a bullet marker, a small repeating texture, the fetch can feel like overhead out of proportion to the image. CSS offers a way around it: skip the file, and carry the image inside the stylesheet itself, encoded as a base64 data URL.

This guide shows the workflow with the free Base64 Image Encoder and Decoder on Ovatly: encode the image, paste the resulting text into a background-image declaration, and judge the result with open eyes. The costs are real and they are stated plainly below, because this technique is excellent for tiny repeated icons and a poor idea for almost everything else.

Contents

  • Quick answer
  • What a data URI in CSS actually is
  • The size cost, in real numbers
  • How to put an image inside CSS, step by step
  • Worked example: reading the encoder's numbers
  • Limitations and honest tradeoffs
  • Common mistakes
  • FAQs
  • Conclusion
  • References

Quick answer

Choose your image in the Base64 Image Encoder and Decoder and the complete data URL appears, with the original size and the exact character count. Copy it and paste it as the value of a CSS background-image property, inside url("..."). The browser reads the embedded bytes directly, with no separate file to host. Reserve this for very small images: base64 text runs about one third larger than the file, and the embedded image cannot be cached separately from the stylesheet.

What a data URI in CSS actually is

A data URL is a URL that carries its own content. Instead of pointing at a file on a server, it holds the file's bytes, encoded as base64 text, after a short prefix like data:image/png;base64, that names the type. Browsers accept data URLs anywhere a URL is accepted: in an img element's src, in JSON, and in CSS, which is where this guide uses one.

In a stylesheet the image sits inside a declaration like this:

.icon-star {
  background-image: url("data:image/png;base64,...");
}

The real thing replaces the ellipsis with the full payload, which for even a small icon runs to hundreds or thousands of characters. The rule is still an ordinary CSS background rule in every other respect: sizing, position and repeat behave exactly as they do with a file URL.

The size cost, in real numbers

Base64 encodes 3 bytes into 4 characters, so the text form is always about one third larger than the file it came from, before the data:image prefix is added. The entry example for the encoder makes the scale concrete: a 30,000 byte JPEG becomes about 40,000 base64 characters. That is the whole trade in one line. You save a file fetch and pay roughly a third more bytes, delivered as text, every time the containing file is downloaded.

This is why the honest rule is about size, not cleverness. Data URLs are convenient for very small images. Anything beyond a few kilobytes usually belongs in a real file, because the encoded text cannot be cached separately. A 40,000 character payload pasted into a stylesheet is not an optimisation; it is a photo wearing a heavy coat.

How to put an image inside CSS, step by step

  1. Start from the smallest honest version of the image. Resize or compress it first if it is bigger than the job needs; encoding preserves every byte you feed it, including the waste.
  2. Open the Base64 Image Encoder and Decoder and choose the image. The complete data URL appears, with the original size and the exact character count.
  3. Read those two numbers before copying anything. If the character count is more than a few thousand, stop and use a real file instead.
  4. Copy the data URL, or download it as a text file if you want to keep it. Paste it into your stylesheet as the url("...") value of a background-image declaration, keeping the whole string, prefix included, inside the quotes.
  5. Load the page and check the background renders. If it does not, the usual cause is a truncated paste; compare the pasted length against the character count the tool showed you.

Worked example: reading the encoder's numbers

Suppose your icon file is a small PNG and you want it as a repeating background. You choose it in the encoder and the tool reports the original size and the exact character count of the data URL. Those two numbers are the decision. The relationship between them is fixed by the encoding: the character count will be about 4/3 of the byte size, plus the short prefix, the same arithmetic by which a 30,000 byte JPEG becomes about 40,000 characters.

Say the count comes back at a few thousand characters for a genuinely tiny icon. That is the green-light zone: one stylesheet carries it, no extra file is hosted, and every page using that stylesheet shows the icon without another fetch. If the count comes back in the tens of thousands, the encoder has just saved you from a mistake, because those bytes would ride along with the CSS on every page that loads it.

Limitations and honest tradeoffs

The costs deserve their own list, because they are the reason this technique stays a niche tool. The CSS file grows by the full encoded size of every image you embed in it. The embedded image cannot be cached separately: it is cached only as part of the stylesheet, so updating one icon means every visitor downloads the whole stylesheet again, where a separate image file would be cached and reused on its own schedule. And every page that uses the stylesheet pays the bytes, including pages where that particular background never appears on screen.

Set against that, the benefits are narrow but real: no separate file to host or lose, no extra request for a tiny asset, and a single stylesheet that can be dropped into a page or an email-safe asset and simply work. For a handful of tiny repeated icons, that trade wins. For anything larger, it loses, and a normal image file is the better engineering.

Common mistakes

  • Embedding a photo. The one-third size penalty on a large file dwarfs any saved fetch. Photos belong in files.
  • Skipping the character count. The encoder shows the exact cost up front. Read it before pasting, not after the stylesheet balloons.
  • Truncating the paste. Data URLs are long and editors wrap lines. A shortened string renders nothing, and a single missing character changes or breaks the payload.
  • Embedding the same large asset in several stylesheets. Each copy is paid for separately, on every page, with no shared cache.
  • Forgetting the original file. Keep the source image. The day the icon needs editing, you want the file, not a wall of base64 to decode first.

FAQs

Why is the data URL so much longer than the file? Base64 encodes 3 bytes into 4 characters, so the text form is always about one third larger, before the data:image prefix. That overhead is the price of embedding binary data in text, and it is why the technique suits tiny images only.

Which images are worth embedding in CSS? Very small ones, such as tiny icons that repeat across a site. Anything beyond a few kilobytes usually belongs in a real file, because the encoded text cannot be cached separately and every page loading the stylesheet downloads those bytes again.

Can the browser cache the embedded image on its own? No. The image lives inside the CSS as text, so it is cached only as part of the stylesheet. Change one embedded icon and the whole stylesheet is downloaded again; a separate image file would be cached and reused independently.

Where else can I paste a data URL? In an img src, a CSS background, or anywhere a URL is accepted. Browsers read the embedded bytes directly, with no separate file to host.

Is anything uploaded when I encode an image? No. Reading and encoding happen entirely in your browser. The image never leaves your device, and you can copy the data URL or download it as a text file.

Conclusion

Putting an image inside CSS is a two-step job with a judgement call in the middle: encode with the Base64 Image Encoder and Decoder, check the character count it shows you, and paste only if the image is small enough to earn its place in the stylesheet. When a string comes back the other way and you need the file out of it, our companion guide on decoding a base64 string back into an image covers the reverse trip. For more guides in this series, see our Image Tools guides.

References