Reputation: 13908
I've learned that it's possible to embed an image in an HTML page like so, instead of linking to a separate image file:
<a href="data:image/png;base64,...(blah blah base64-encoded png goes here)..."
width="70" height="38" alt="image embedded using base64 encoding!"></a>
Is this "safe", as in will all modern browsers be able to view the image, as long as I stick to common formats like PNG/JPG? Are there any downsides, other than base64-encoding the image increasing the image size by a bit?
Thanks.
Upvotes: 17
Views: 60617
Reputation: 87773
All modern browsers should be able to view these types of images. I have not verified, but this capability has been around for a good while and is probably widely supported.
But you also asked for downsides. One downside is that it is common that HTML markup is dynamically generated and therefore cannot be cached.
More downsides that probably don't relate
Upvotes: 5
Reputation: 499002
Yes, this is safe - all major browsers support the data URI scheme.
One downside is that if you use the same image a number of times in the page, it will be encoded several times vs downloaded once.
Another is the size limit imposed by some browsers (IE 8 only allows up to 32k).
You can also use this in CSS to mitigate the download issue.
Upvotes: 22