Base64 Images in HTML, CSS and JSON: Best Practices
Inline images can speed up a page or slow it down. Here are clear rules for when Base64 is the right tool.
By Prestofiles Team · · 2 min read
Base64 turns a picture into plain text so it can live inside HTML, CSS, JSON or an email. Learn what it is, when it helps and when it hurts.
Base64 is a way to write binary data, such as a picture, using only normal letters, numbers and a few symbols. The result is a long piece of text that can be pasted anywhere text is allowed.
An image in HTML can be written like this: <img src="data:image/png;base64,iVBORw0KGgo...">. Everything after the comma is the encoded picture, and image/png tells the browser what type it is.
Base64 makes data about one third larger than the original file. For big photos this is a bad trade: the page gets heavier and the browser cannot cache the picture separately. Use it for small images, usually under a few KB. For large pictures, compress them and link to the file normally.
Need the reverse? Read how to decode Base64 to an image.
Inline images can speed up a page or slow it down. Here are clear rules for when Base64 is the right tool.
Got a long Base64 text from an API or a database and want to see the picture? Paste it and download the image in seconds.
A phone photo can be 5MB while a form accepts 100KB. Here is why photos are so large and the quickest way to shrink them.