If you want to embed an image directly into an HTML file’s code, instead of linking to an image file, and you don’t have access to JavaScript, then encoding the image as Base64 is the only solution.
In other words, instead of linking an image like this:
This article will explain how to convert an image to Base64 and how to add it to your page correctly. But please read to the end of the article to see alternatives, because Base64 is inefficient and very rarely necessary.
The Solution
First, you need to convert your image to Base64, and then add it to the HTML.
To manually convert an image, use a site like
http://base64online.org/encode
. Upload your image to the site and copy the text it returns.
Your
src
attribute must begin with
data:image/png;base64,
. After that, you need to insert your image text, which can be surrounded on both sides by as much whitespace as you like.
The
png
can be replaced with the MIME type of your file:
jpeg
,
gif
, or
svg+xml
.
Some browsers might fail to display your image if you use an unexpected text encoding. To be safe, always specify your MIME
Content-Type
and
Content-Encoding
:
A common error reported by programmers is mistakenly double-encoding their images. If you encounter an error you can’t debug, try to decode your Base64 text to check that you didn’t encode it twice accidentally.
Encode An Image Programmatically
Instead of using a tool to encode your image, you can do it with JavaScript (and of course on the server in PHP or Python or other languages):
However, if you have access to both the file and JavaScript, there is no reason to use embedded Base64. Rather use
createObjectURL
, as discussed later.
The
btoa()
function (Binary To ASCII Base64) in JavaScript is used more for strings than image embedding:
Click to Copy
const encodedData = btoa("Hello, world"); // encode a string
const decodedData = atob(encodedData); // decode the string
If you’re working with strings instead of images, note that
btoa()
won’t work with Unicode, because the function defines binary data as “strings in which the code point of each character occupies only one byte”.
Base64 is a string filled with groups of four 6-bit characters (A-Z, a-z, 0-9). An image in Base64 is always at least a third larger than a binary image file. Your users’ CPUs need to do extra work to decode the image, making your HTML code longer and more difficult to maintain. For these reasons, you should avoid using Base64 whenever there is another solution.
File Uploads
For instance, don’t use Base64 to display an image on the page after a user has uploaded or selected an image from their computer. If your page has access to JavaScript and the internet, you should rather use
createObjectURL
.
Here’s an example that creates a blob from a file, creates a temporary URL pointing to the blob (automatically handled by the browser), and adds it as an image to the document. When handling a user’s uploaded file you would create a blob from that, instead of a file on the web.