How to display Base64 images in HTML
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Displaying images on a webpage typically involves linking to an image file stored on a server. However, another approach is to use Base64 encoding, which allows you to embed the image data directly within your HTML code. This method can be particularly useful in reducing the number of HTTP requests for small images, leading to faster page loads in some scenarios.
What is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format by translating it into a radix-64 representation. Each Base64 digit represents exactly 6 bits of data. This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.
Base64-encoded images can be included directly in HTML (or CSS) without any additional file requests, which can simplify deployment and improve performance by reducing the number of HTTP requests.
How to Convert Images to Base64
Before an image can be displayed using Base64 in HTML, it must be encoded. Here are the common steps to convert an image to Base64:
- Choose an Image: Any image file (e.g., .png, .jpg, .gif) can be converted to Base64.
- Use a Conversion Tool: There are many tools available online to convert images to Base64, or you can use programming languages like Python or JavaScript.
- Python Example:
- JavaScript Example:
How to Embed Base64 Images in HTML
Once you have the Base64 encoded string, embedding it into HTML is straightforward. Use the img tag's src attribute as you normally would, but instead of providing a URL, include the Base64 encoded string.
Example:
Details to note:
- The
srcattribute starts withdata:, followed by the MIME type (image/jpeg,image/png, etc.), then;base64,, and finally, the encoded Base64 data. - Always include an
altattribute to provide alternative text if the image cannot be displayed.
Advantages and Disadvantages of Using Base64 Images
Here are some of the pros and cons of using Base64 images:
| Advantage | Disadvantage |
| Reduces the number of HTTP requests. | Increases HTML document size. |
| Useful for very small images. | Can increase load time for larger files. |
| Simplifies deployment by embedding images directly in HTML or CSS. | Harder to cache than binary images. |
Considerations When Using Base64 Images
- Performance: Base64 encoding increases file size by approximately 33%. It’s generally a good practice to use Base64 for only small images or elements on a page.
- Caching: Unlike external images that are fetched from URLs, inlined Base64 images cannot be cached by the browser independently of the HTML or CSS files they reside in. This can result in higher bandwidth usage if the HTML or CSS file containing the Base64 encoded image is requested frequently.
- Maintenance: Updating an image encoded in Base64 requires re-encoding and replacing the string in HTML or CSS, which can be cumbersome compared to simply replacing the image file in a server directory.
Conclusion
Using Base64 encoded images can be a powerful tool for web developers looking to optimize their application's performance by reducing HTTP requests. It is most beneficial when used for small images, such as icons and UI elements. However, the increased size and caching limitations require careful consideration to avoid potential pitfalls that could affect your site’s performance.

