Base64 Encoding: When and Why to Use It
Learn what Base64 encoding is, how it works, and when to use it. Understand the difference between encoding and encryption, common use cases, and best practices for web development.
At Šikulovi s.r.o., we base64-encoded every icon on a client's landing page. Page load time doubled. Here's what that disaster taught me about when embedding actually makes sense.
Back in 2019, I was optimizing a landing page and read somewhere that inline images eliminate HTTP requests. Brilliant, I thought. So I converted every icon, logo, and decorative image to base64. The result? My HTML file went from 50KB to 400KB, and the page actually loaded slower than before.
That was the day I learned that base64 encoding increases file size by roughly 33%. A 10KB image becomes about 13.3KB when base64-encoded. And since it is embedded in HTML or CSS, it cannot be cached separately. Every page load re-downloads those images as part of the document.
Despite my disaster, there are legitimate cases where base64 embedding makes sense. The key is understanding the trade-off: you eliminate one HTTP request but increase the file size and lose caching benefits.
Base64 encoding converts binary data to ASCII characters. Every 3 bytes of binary becomes 4 characters of base64. That is where the 33% comes from. But it gets worse with gzip compression.
Binary images like PNG and JPEG are already compressed. When you base64-encode them, the resulting text compresses poorly compared to the original binary. A 10KB PNG might gzip to 9KB. The same image base64-encoded might only gzip to 12KB. You lose twice.
I remember when CSS sprites were the gold standard for icon optimization. You combined all icons into one image and used background-position to show each one. It felt hacky but it worked.
Today, I rarely use either technique. SVG icons with proper caching beat both approaches. But if you are stuck with raster icons, here is my rule: use sprites if you have more than 5 icons of similar size, use base64 if you have 1-3 tiny critical icons, and use individual files with HTTP/2 for everything else.
Email is the one place where I still use base64 liberally. Most email clients block external images by default. When a user opens your email, they see broken image placeholders until they click "display images."
Base64 images embedded directly in the email body often render without that extra click. The trade-off is larger email size, but for critical images like your logo, it is usually worth it. Just keep it to a few small images - embedding large photos will get your emails flagged as spam.
After years of experimenting, here is what I do now. First, I optimize the image itself - compress it, resize it, choose the right format. Then I ask: is this image critical for the initial render? Is it under 2KB? Will it be used in email?
If yes to any of those, I consider base64. Otherwise, I serve it as a regular file and let the browser cache it. Modern HTTP/2 makes multiple small requests nearly free, so the old "reduce HTTP requests" advice is less relevant than it used to be.
When you do need to convert an image to base64, the process is simple. Drop your image into the converter, and you get the base64 string plus the complete data URI ready to paste into your code.
I added a file size comparison feature because I kept forgetting to check the original size. Now I can see immediately if the base64 version is worth it. If your 50KB hero image is becoming 67KB of base64 text, that is a sign to use a regular image tag instead.
I stick to under 10KB, but honestly, under 2KB is the sweet spot. Beyond that, the 33% overhead and loss of caching usually hurt more than the saved request helps.
Yes, data URIs work in all modern browsers and have for years. IE8 had a 32KB limit, but that is ancient history now. The only place I see issues is some email clients with very aggressive image blocking.
Usually no. SVGs are already text, so you can inline them directly in HTML without base64 encoding. If you need them in CSS background-image, you can URL-encode the SVG instead of base64 - it's smaller.
For batch conversion, I use a build tool like webpack or a Node script. The browser-based converter is great for one-off conversions, but for a whole project, you want automation.
Search engines can't see base64-embedded images as easily as regular img tags with src attributes. For decorative images, that's fine. For content images you want indexed, use regular image tags with descriptive alt text.
Founder of CodeUtil. Web developer building tools I actually use. When I'm not coding, I experiment with productivity techniques (with mixed success).
Learn what Base64 encoding is, how it works, and when to use it. Understand the difference between encoding and encryption, common use cases, and best practices for web development.
I have slashed bundle sizes by 60% on client projects at Šikulovi s.r.o.. Here is everything I have learned about minifying JavaScript, CSS, and HTML for production.
After years of optimizing builds at Šikulovi s.r.o., I have developed a battle-tested approach to minification. Here is my complete guide to build tool integration, source maps, and avoiding common pitfalls.