← Back to Blog

Image to Base64: Embedding Images in HTML, CSS, and JSON

Published on April 11, 2026 by RiseTop

What Does "Image to Base64" Mean?

Converting an image to Base64 means taking the raw binary data of an image file (PNG, JPEG, SVG, etc.) and encoding it into a long ASCII string using the Base64 alphabet. This string can then be embedded directly into HTML, CSS, or JSON — no separate file download required.

The result looks like a data: URI: data:image/png;base64,iVBORw0KGgoAAAANSUhEUg.... The browser decodes this inline and renders the image without making a separate HTTP request.

When Should You Use Base64 Images?

Good Use Cases

Bad Use Cases

How to Embed in HTML

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUg..." alt="Icon">

The data: URI format is: data:[mediatype][;base64], followed by the encoded string. The MIME type must match the image format — image/png, image/jpeg, image/svg+xml, image/webp, etc.

How to Embed in CSS

.icon {{
  background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0...");
  width: 24px;
  height: 24px;
}}

This is especially useful for small SVG icons in component libraries where you want zero external dependencies.

How to Embed in JSON

{{
  "image": "data:image/png;base64,iVBORw0KGgo...",
  "name": "product-photo.png"
}}

APIs sometimes return Base64-encoded images. Keep in mind this roughly triples the JSON payload size compared to a binary response.

Performance Impact

The key trade-off is requests vs. size. Base64 eliminates an HTTP request but increases the document size by ~33%. For tiny images, the request elimination wins. For large images, the size increase hurts — especially on slow connections where downloading a large HTML document blocks rendering.

Modern best practice: use Base64 only for images under 2–4 KB. For everything else, use separate files with proper caching headers and lazy loading.

SVG Special Case

SVGs can be embedded as Base64, but you can also use UTF-8 encoding directly: data:image/svg+xml,%3Csvg...%3E. This is smaller than Base64 and more readable, though you must URL-encode special characters. RiseTop's image-to-Base64 tool handles both approaches automatically.