Css img url data

How to display Base64 images in HTML ?

Base64 is a method for encoding binary data into ASCII text. In the context of web development, Base64 images refer to images that have been encoded as a Base64 string. This string can be embedded directly into the HTML code of a web page and displayed as an image, without the need for separate image files.

There are several advantages to displaying Base64 images in HTML.

  • Faster page loading times: By embedding the image data directly into the HTML code, you can eliminate the need for separate HTTP requests to retrieve the image files. This can result in faster page loading times, especially for smaller images.
  • Increased security: By encoding the image data into a Base64 string, you can hide the actual binary data from users who view the source code of your web page. This can be useful for preventing unauthorized access to sensitive image data.
  • Improved compatibility: Base64 images can be displayed on all modern browsers, regardless of the image format. This can be especially useful for cross-browser compatibility, as different browsers may have different support for different image formats.
  • Simplified development: In some cases, it can be easier to encode images as Base64 strings during development and then embed the data directly into your HTML code, rather than having to manage separate image files.
Читайте также:  Css box sizing min heights

Displaying Base64 images in HTML involves converting binary image data to a Base64 string and then embedding it into the HTML as a data URL. A data URL is a type of Uniform Resource Identifier (URI) that embeds the image data directly into the source code of a web page. Here’s how you can display a Base64 image in HTML.

  • Convert the image to Base64 format: You can use an online Base64 encoder to convert the binary image data to a Base64 string. The result will be a string of characters that can be easily embedded into your HTML code.
  • Create a data URL: The Base64 string must be wrapped in a data URL format. The data URL format consists of three parts: the data type, the Base64 encoded data, and the ending of the URL. For an image, the data type would be “data:image/[format];base64,” where [format] is the format of the image file (e.g. PNG, JPEG, GIF).
  • Embed the data URL into your HTML: To display the image, you can use an HTML image tag ( ) and set the “src” attribute to the data URL.

For example:

Here’s an example HTML program that demonstrates how to display Base64 images in HTML.

In this example, Let’s say we have the following image.

For Base64, we will consider the Data URL of the image, which is placed in the src attribute. The Data URL has two parts

  • The first part is the Base64 encoded image.
  • The second part is the Base64 encoded string of the image.

Therefore, the Base64 of the above image −

Читайте также:  Уроки для создания html

HTML

Example: Let us see the complete example.

HTML

Example 2: There is another example HTML program that demonstrates how to display Base64 images in HTML. In this example, Let’s say we have the following image.

For Base64, we will consider the Data URL of the image, which is placed in the src attribute. The Data URL has two parts.

  • The first part is the Base64 encoded image.
  • The second part is the Base64 encoded string of the image.

Therefore, the Base64 of the above image.

HTML

Let us see the complete example.

HTML

< img src line number9 index8 alt2">iVBORw0KGgoAAAANSUhEUgAAANMAAABRCAYAAABIf5MKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAqrSURBVHhe7Z3fbxXHFcePr39e/yT4JzWqg4MaopQ4oi0UpY0iOYHKUmV4qclLSFQpVZ5S1S/2X2C/OGqkSkiRqqRRpZaHCvxiFRIk1FY1oW2KSyUoooDbGOOfGBv7+rc7Z3bWd3bu3r17745Njb4fabQ769nd2bvnO3PO7Ow6L77vpQ0CAEQmppYAgIhATABYAmICwBIQEwCWgJgAsATEBIAlICYALAExAWAJiAkAS0BMAFgCYgLAEhATAJaAmACwBMQEgCUgJgAsATEBYAmICQBLQEwAWAJiAsASEBMAloCYALBE5K8TFVblU3F1IRXXFFBJQyHFG4po9fEaJR6s0NK4SFMiTa7S+kr2p4kV5snjyuPX8bELqaA8Xxx7mRb5+OK4fPyVR2tqDwCeHDmLKVaUR3Xfr6TKF+JqS3pYXA+vLYg0r7Zk5pmXy0QqleLJxOyNBI3/cZbWl/HVMvDkyElMZV8vprrXKqmwMrOh63Bv8tX5aVpfTX/KWEEe7T2xW/Zy2bAyu0bjl2dp/j9LagsA2bGxtqKWqzLRxrrMU16M8vILZJLZfH/bzFpM1YfLZYrCv385TmsJVVGN/HiMnvtxncrlxtTVxzIBkC0spo3VZZGEkHzIKxCCKiiyJybGFBTHLPPDSzR7KyFcunXp1sUbi6hExDmVB+Ii5nEU7bI0uULDv51SuSRNp6pFjOSt6NLUKs3eTNCiiL8SI8tqK1H5cyVU3lxMlc8n3UwICURhfTlBGytJG/Mjr7BIhDj+oU3OMROLqeqbpfTonwsZDXj3t8qo5miFyjmM/2GWZv6xoHJEu14qpbpXK1XOYXJwjqb/FhxncczGdeG4aWcKqY1+80UPtdeqLN2kD5s7qFvlotDTP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T2wbjjeqa7ToFxXFk+Ta+qxzhhy6XSRz/3NJABI6dqUMW9t/IVi0LR8MeENLgd5yTWeRv/LYisZ41X7C+hPT/YpXK5sZXvM/Gk17nb3hgMgDC4Lwdmwur7TNwz7HmjKtQr5Tpb+aYtv0M1+tkjzztPAGTDExGTS+33KuS3GjKx1d+A4ONO/MkbzwGw3UQSE5NfGkt+naje+YIQz4tLjK7ICavyC0ITK7l/najWOTZ/9Si+Rxxb+LD85SMe+na/TrS2sDWzMwDIhshiAgA4ZDWaBwBID8QEgCUgJgAsATEBYAmICQBLQEwAWAJiAsASEBMAloCYALAExASAJSAmACwBMQFgCYgJAEtATABYAmICwBIQEwCWgJgAsATEBIAlICYALAExAWAJiAkAC8R3NUFMANhg75GfQEwARIV7pZpvtEFMAESlev8bcgkxARCRir3flkuICYCIlO3eL5cQEwARKYg7/+USYgLAEhATABFZTUzLJcQEQETmp2/LJcQEQETmvvqrXEJMAERk6vZncgkxARCRxMwwTd4awH8OBMAGmOgKgCUSM8P0PwaQ7KGLxOfAAAAAAElFTkSuQmCC» alt = «GeekforGeeks» >

Note: With these steps, you should now be able to display Base64 images on your HTML pages. However, it’s important to note that while Base64 images can offer some benefits, they also have some drawbacks. For example, encoding images as Base64 strings can result in larger file sizes and longer data transfer times. Additionally, Base64 images are not cached by the browser, which can result in slower page loading times for repeated visits to the same page. For these reasons, it’s usually best to only use Base64 images for small, simple images, and to use traditional image file hosting and referencing for larger images.

Conclusion: Displaying Base64 images in HTML can offer several benefits, including faster page loading times, increased security, improved compatibility, and simplified development. However, it’s important to consider the drawbacks, such as larger file sizes and longer data transfer times, when deciding whether to use Base64 images in your projects.

Читайте также:  Язык html все теги формы

Источник

How to Display Base64 Images in HTML

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 Tag

The 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 tag presented above within a element.

Example of embedding a Base64 encoded image into HTML:

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>

Convert your image to a Base64 string

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>

Источник

Оцените статью