HTML
Base64 Images
Web Development
Coding Tips
Image Display

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:

  1. Choose an Image: Any image file (e.g., .png, .jpg, .gif) can be converted to Base64.
  2. 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:
python
1     import base64
2     with open('image.png', 'rb') as image_file:
3         encoded_string = base64.b64encode(image_file.read())
4         print(encoded_string)
  • JavaScript Example:
javascript
1     let reader = new FileReader();
2     reader.readAsDataURL(file);  // file is from an input element or drag and drop
3     reader.onload = function () {
4       console.log(reader.result);  // reader.result contains the base64 string
5     };

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:

html
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...more base64 data..." alt="Description of image">

Details to note:

  • The src attribute starts with data:, followed by the MIME type (image/jpeg, image/png, etc.), then ;base64,, and finally, the encoded Base64 data.
  • Always include an alt attribute 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:

AdvantageDisadvantage
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.


Course illustration
Course illustration

All Rights Reserved.