A virtual teacher who reveals to you the great secrets of Base64

<img>

The <img> tag is designed to display images on web pages. Each <img> element must have the src attribute and should contains the URL-address or the data URI of the image.

For example, you can display one-pixel red dot GIF as follows:

<img src="//static.base64.guru/uploads/images/1x1.gif" />

The same can be achieved by encoding image to Base64 and embedding it using data URI:

<img src="data:image/gif;base64,R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs" />

Both behaviors are identical — when you open the page, your browser displays the same image specified within <img> tag. The difference is that in the first case the browser sends one HTTP request to fetch the external image, when in the second case the image is already loaded in the browser’s memory and it does not need to send any HTTP requests.

It is important to note, that src attribute will be ignored if it is a child of <picture> tag and at least one of its <source> tags match a requirement for the device screen or page layout.

Please note that Base64 Data URIs have some limitations. In addition, like almost any unconventional solutions, it has both advantages and disadvantages. Therefore, if you plan to use it, first of all, read about Base64 Data URI.

Add new comment

If you have any questions, remarks, need help, or just like this page, please feel free to let me know by leaving a comment using the form bellow.
I will be happy to read every comment and, if necessary, I will do my best to respond as quickly as possible. Of course, spammers are welcome only as readers.

Loading...