Blog Upload Shorten Tools Gallery FAQ Contact Sign in
📢 永久無廣告,自動清除EXIF,保護隱私內容
Tools

🔗 Image to Base64

Data URI, CSS, and HTML snippets

Base64 inflates data by about 33%, so compressing first is almost always worth it. Turn this off to encode the original file as-is.

⚠️ Data URIs suit very small icons. Inlining anything past 5–10KB usually backfires: it cannot be cached separately, so it is re-sent with every page load.

🔒 Everything runs inside your browser — your files are never uploaded to our servers and we never see them. Close the tab and nothing is left behind.

Image to Base64: before and after example
Original on the left, the actual output of this tool on the right — not a mock-up. Any file sizes or dimensions shown are real measured values.

Step-by-step

  1. Pick an image Small icons suit this best — logos, icons, simple background patterns. A photograph produces a string so long it is barely usable.
  2. Leave "re-encode smaller first" on Base64 inflates data by about 33% (every 3 bytes become 4 characters). Shrinking to the size you actually display and compressing first is therefore almost always worth it. The defaults — 256px wide, WebP — are ample for an icon.
  3. Choose the output form Data URI gives you the raw string to use however you like. HTML img hands you a complete tag with width and height. CSS background gives you a rule ready for a stylesheet.
  4. Press start The full string appears below along with its length. That length is the point — it is exactly how much bigger your HTML or CSS file becomes.
  5. Copy it, or download the .txt The Copy button puts it on the clipboard. For long strings the downloaded .txt is far easier to work with than selecting it all in a browser.

When you would use this

Inlining tiny icons to save a request

For an icon of a few hundred bytes, the HTTP request can cost more than the image itself. A data URI removes that round trip. Since HTTP/2 the benefit is much smaller than it used to be, so do not overdo it.

Writing HTML email

Many mail clients block external images until the recipient clicks "show images". Inlined images are not blocked — though Gmail's support for data URIs is limited, so always test before sending.

Single-file pages and reports

When you have to hand over an HTML file that opens and works with no other files alongside it, every image has to be inlined. Offline documents, generated reports, and pages sent as email attachments all fall into this category.

Quick tests and demos

Need an image in a CodePen, JSFiddle, or a throwaway test page without dealing with hosting? Pasting a data URI is the fastest route.

What Base64 is

Images are binary data, while HTML, CSS, and JSON can only hold text. Base64 is an encoding that turns arbitrary binary into plain text, representing the original bytes with 64 safe printable characters (A–Z, a–z, 0–9, +, /).

The price is a size increase of about 33%: every 3 bytes must be written as 4 characters. That is not implementation waste — it is the mathematical floor of the encoding.

When inlining is worth it

A simple test: inline only when the image is small enough that the HTTP request costs more than the image does. In practice that means icons under about 5KB.

Past that, the downsides start to outweigh the benefit, and the main one is caching. A separate image file is downloaded once and served from cache on every subsequent page. An inlined image is part of the HTML or CSS file and is re-sent in full every time that file loads. A site inlining 500KB of images ships an extra 665KB of text on every single page view.

What changed with HTTP/2

Data URIs became popular when browsers strictly limited simultaneous connections per domain, usually to six. Cutting the number of requests was a genuine optimisation. HTTP/2 multiplexes many requests over one connection, which shrinks that benefit dramatically.

The sensible modern use is narrow: inline only the tiny icons that appear on every page and rarely change. Leave everything else as separate files so caching can do its job.


Frequently asked questions

Why is the string larger than the original file?
Base64 represents arbitrary binary data using 64 printable characters, turning every 3 bytes into 4 characters — an unavoidable ~33% increase, plus the data:image/webp;base64, prefix. That is exactly why shrinking first is recommended: a 100KB image becomes 133KB of text.
When should I not use a data URI?
Once the image passes 5–10KB. The real problem is caching: a separate image file is cached by the browser and reused on every page, whereas an inlined image is part of the HTML or CSS and is re-sent in full every time that file loads. Inlining a 200KB image means 266KB of extra text on every single page view.
Does it affect page performance?
Yes, in both directions. You save a request; but a larger CSS file delays parsing of the entire stylesheet, and CSS blocks rendering. A stylesheet stuffed with data URIs can slow down first paint for the whole page. Since HTTP/2, the value of "one fewer request" has dropped considerably.
Can I use the string directly in <img src>?
Yes — data URIs work in src, in CSS url(), and on canvas. The "HTML img" form also gives you width and height attributes, which are worth keeping: they let the browser reserve the space and stop the page jumping around as things load.

What you might need next

Image work rarely ends in one step. These pair up with Image to Base64 most often:

Want to share the result? Upload turns images, photos, or videos into a short link with optional password, expiry, and view limits. Or head back to all 12 tools.


More tools