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


The <input> tag is one of the most common form elements. The main reason for its high popularity is that it allows you to create various field types, such as button, checkbox, password, file, time, email, hidden, and many others. One of these types is image, which transforms the <input> into an <img> and acts as a submit form button. When a form is submitted via such a button, the value of the <input> becomes the XY coordinates of the image that the user clicked on.

As well as <img>, the <input type="image" /> requires the src attribute. Typically, it contains the path to the image, but like the <img> tag, it supports the data URI.

For example, you can create a one-pixel red dot button as follows:

<input type="image" src="//static.base64.guru/uploads/images/1x1.gif" />

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

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

Both behaviors are identical — when you open the page, your browser displays the same image specified within <input> 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.

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.

Comments (3)

I hope you enjoy this discussion. In any case, I ask you to join it.

  • Sam,
    Does anyone know how to add extent values to PNGBase64?
    • Administrator,
      Hey Sam! You cannot do that directly on Base64 string. You should decode Base64 to image, apply extent values, and encode the new image to Base64.
      • Sam,
        Thanks for the advice! How do I apply extent values to the Base64 image?
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.