How To Display Base64 Images In HTML - W3docs
Maybe your like
Images encoded with Base64 can be embedded in HTML by using the <img> tag. This can help to increase the page load time for smaller images by saving the browser from making additional HTTP requests.
Base64 encoding and Data URL go hand-in-hand, as Data URLs reduce the number of HTTP requests that are needed for the browser to display an HTML document.
In this snippet, we’ll demonstrate how you can display Base64 images in HTML.
How to Use the <img> Tag
The <img> tag has a src attribute and contains the Data URL of the image. A Data URL is composed of two parts, which are separated by a comma. The first part specifies a Base64 encoded image, and the second part specifies the Base64 encoded string of the image. Add also an alt attribute.
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4 //8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />Now, see the full code, where we need to place the <img> tag presented above within a <div> element.
Example of embedding a Base64 encoded image into HTML:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <div> <p>From wikipedia</p> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4 //8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" /> </div> </body> </html> Try it Yourself »Convert your image to a Base64 string
You can use the following code to convert any image on your device into a Base64 string. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Image to Base64</title> </head> <body> <input type="file" id="inputImage" accept="image/*"> <textarea id="outputBase64" rows="10" cols="80" readonly></textarea> <script> document.getElementById('inputImage').addEventListener('change', function(event) { const file = event.target.files[0]; const reader = new FileReader(); reader.onloadend = function() { const base64 = reader.result; document.getElementById('outputBase64').value = base64; }; reader.readAsDataURL(file); }); </script> </body> </html> Try it Yourself »Tag » Code Base64 Image
-
Base64 Image Encoder
-
Image To Base64 | Base64 Encode | Base64 Converter - Base64.Guru
-
Best Online Base64 To Image Decoder / Converter - Code Beautify
-
Convert Image To Base64 - Code Beautify
-
Base64 Image Encoder - Convert Any Image File Or URL Online
-
Image To Base64 Converter - Online - Browserling
-
Base64 Image Encoder / Decoder Online - AppDevTools
-
Base64 Image Decoder - Online Tool
-
Base64 To Image Decode (jpg,png,gif)
-
How To Display Base64 Images In HTML - Stack Overflow
-
Base64 - Wikipedia
-
Converts Base64 String Into An Image - IP Location Lookup
-
Base64 To Image | Decode And Encode Online
-
How To Avoid Problems With Base64 Images - CodeTwo