Implement Inline Images


Part One Chapter 13

Image

When a web page has to display an image, a http request must happen to fetch the image file. The typical HTML code to load the image file into the web page is:

<body>
  <img src="https://www.mobilewebsitebook.com/images/logo.png">
</body>

Base64

It is possible to load an image file onto your web page without using http request. You can inline an image in a web page's HTML code by using Base64 encoding. This is how you should include the image in Base64 format:

<body>
  <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwQAADsEBuJFr7QAAABp0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjUuMTFH80I3AAAKPElEQVR4Xu2b+VNWZRTHnZyaFqspsz3NzEyzzbIms1EzM9MmS62srLHNsmUqK0tNUUDZRMEFUVAWUVndAFHAFURckEUBERH8R059jvM83ve+bw6LzPuS/PCd995z7n3ufb73Oec55zzP2+PSpUsCzjSekUNnDl1TNDQ1aNuBDEvAoj2L5IGIB2RswthrgociH5L08nSvBwYaPAiYtnmavJ/6vkzfMl1mZc2Soqoirxtai0+2ftL1CBi+drhEF0dLyN4Q/e0IXln3StcjYGjsUPlj9x/XBM+ufrZrmkD2iWxZkLfAAmd2sOagZB3PahPGJY6ToD1BPnX7q/d7vYi/4EXA7ordElYYZnG64bQMWjlInlj5hP52FP2W95O+UX29XsRf8CIgsihSRqwbYbHz1E598c1lm3VEcN3MjJleDbUWeRV5gU1A1fkqj7n8QvMFJeDH7T/Kzzt+lu9zvpfvsr+Tv3L/ahdmps8MbAKWFCyRIbFDLHad2qUEfLTlI53anBi/cbzcHnq7l/xqeGvTW4FNgFNpAAHnms7JvNx5Gh+A4L3BSk7/6P6ScyLHyn1hz+k9tq2ANwGn0gACGi42qFfHFEB4YbglAD9h5L5QWFVo2+qyBNReqJWR8SPl8RWPK8ZtHGcJiCqOsnJfiD8cb9vqsgQ0NjdqYBNzIEZRUFlgCThef9zKfaGmsca2FdAEvJvyrr6sGwNXDJTK85XyZMyTcufSOxVMkYaAlftXWrkvRB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XblWlDaWbtRzvxOAHZtzTKDv8r42EMIh8j8m59DH77y87mVrKjjFb7O/tV+djhNJ4os4dxKAuUGWmbZBQBEAXlv/mq32UPhwFkANCI8ptR2rO6YLJW7fgHNjbQFfYAiAILJKE0MY+J0AQlwCKBB/JF6ein1KvxTAJFhic983Y9sMWVywWIexc8YxYHQwkrB1QwARKubhdpp+J+CmJTfpXA9uXHKj2jG2T/pL2OvrPhZX+PcKRVX3CDIg+CLAggBKcOx3ptLsrCcAvxNgojqAx6YoYpIeCHBPaYB8n9Uj7mdYu/UMdwI0ag4QQBzBaPg7/291ls5ZIuB8AEEMoTTHZIaExE49oMrMv1TwAVSV3cMaInsv661ypxOETFJ2slhzbcARgJPCq3O8cM9C9RHOUcDL8wdPIxuTMEZHg9Fj/yRMJmJ0EgCIFzA3Ksmc+50AvqD5wzVTHl79yJnL1WS+ILnFsDXDdIWJjjCHO/+DREzArDAldYrm+RDC9aZu4CYAMNOYv/75lQDWDJ3/LCGFJjp0XoO9MjtQIyDSY3g79YACTMi+EHWOtOOMFvED5CPO69mtyhpEU0uT/APpWzS9XrEGKAAAAABJRU5ErkJggg==" />
</body>

Base64 encoding should be implemented on mobile web pages where large number of small images (e.g. over 20) are used. This will significantly reduce the number of http requests.

Base64 Encoder

Use this tool to encode your image into Base64:

  • base64encode.net/base64-image-encoder

Disadvantages of Base64

  • One downside when implementing inline images through Base64 is that web browsers will not cache the images.
  • The Base64 encoded image normally will end up larger than the original image file. For example, a 2 kilobyte image file may result in over 2.5 kilobytes. One advice is not to use base64 encoding when your image file size exceeds 5 kilobytes.


Gordon Choi's Mobile Website Book has been available since November 2016.







Content on Gordon Choi's Mobile Website Book is licensed under the CC Attribution-Noncommercial 4.0 International license.

Gordon Choi's Mobile Website Book

Gordon Choi’s Other Books:
The China Mobile SEO Book
Analytics Book