DeveloperApril 2026 ยท 5 min read

How to Convert an Image to Base64 for CSS and HTML

Base64 encoding converts an image file into a text string that can be embedded directly in CSS or HTML โ€” no separate file request needed. This technique eliminates an HTTP request for each embedded image, which can improve performance for small icons, logos, and UI elements.

๐Ÿ–ผ๏ธ
Try the Image to Base64
Free, no signup
โ†’
DG
Derek Giordano
Designer & Developer
In this guide
01Data URI Syntax02When to Use Base64 Images03When to Avoid Base6404Converting with the Tool
โšก Key Takeaways
  • Convert images to Base64 data URIs for inline embedding in CSS backgrounds, HTML img tags, and email templates.
  • Covers data uri syntax.
  • When to Use Base64 Images.
  • When to Avoid Base64.
  • Covers converting with the tool.

Data URI Syntax

A Base64-encoded image uses the data URI format:

background-image: url(data:image/png;base64,iVBORw0KGgo...);

or in HTML:

Example Base64-encoded image output

The format is: data:[media-type];base64,[encoded-data]. The Image to Base64 tool generates the complete data URI โ€” upload any image and copy the output directly into your CSS or HTML.

When to Use Base64 Images

Small icons and UI elements under 2-4KB โ€” the overhead of a separate HTTP request exceeds the Base64 size increase. Email templates where external image hosting is unreliable. Single-file HTML documents that need to be self-contained. CSS sprites for tiny repeated patterns (dots, noise textures). Inline SVGs can be Base64-encoded but are often better included as raw SVG markup.

๐Ÿ’ก Tip
Use 3+ color stops instead of 2 to avoid the muddy gray band that appears in the center of complementary-color gradients.

When to Avoid Base64

Images larger than 4-5KB โ€” the 33% size increase outweighs the saved HTTP request. The Base64 string can't be cached independently from the stylesheet. Hero images, photographs, and any large raster image should always be served as separate files. With HTTP/2 and HTTP/3 multiplexing, the HTTP request overhead for separate files is minimal anyway.

โš  Warning
CSS gradients used as backgrounds cannot be animated with standard transitions. Use background-size animation or @property registered custom properties instead.

Converting with the Tool

Upload any image (PNG, JPG, WebP, SVG, GIF) to the Image to Base64 tool. It returns the Base64 string, the complete data URI for CSS, and the complete tag for HTML. File size before and after encoding is shown so you can decide if Base64 is worth it for that particular image.

Frequently Asked Questions

How do I convert an image to Base64?+
Upload the image to the Image to Base64 tool. It returns the encoded string and ready-to-use CSS and HTML snippets. You can also use JavaScript: reader.readAsDataURL(file).
Does Base64 increase image size?+
Yes, by approximately 33%. A 3KB image becomes ~4KB in Base64. This is acceptable for small icons but problematic for large images.
Can I Base64 encode SVG files?+
Yes, but raw SVG markup is often better โ€” you can include SVG directly in HTML or CSS without Base64 encoding, and it's more readable and editable.
Try it yourself

Use the Image to Base64 โ€” free, no signup required.

โšก Open Image to Base64
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.