A Data URL is a URI scheme that provides a way to inline data in a document, and it’s commonly used to embed images in HTML and CSS.
Introduction
A Data URL is a URI scheme that provides a way to inline data in an HTML document.
Say you want to embed a small image. You could go the usual way, upload it to a folder and use the img
tag to make the browser reference it from the network:
<img src="image.png" />
or you can encode it in a special format, called Data URL, which makes it possible to embed the image directly in the HTML document, so the browser does not have to make a separate request to get it.
Data URLs might save some time for small files, but for bigger files there are downsides in the increased HTML file size, and they are especially a problem if the image reloads on all your pages, as you can’t take advantage of the browser caching capabilities.
Also, an image encoded as Data URL is generally a bit bigger than the same image in binary format.
They aren’t much practical if your images are frequently edited, since every time the image is changed, it must be encoded again.
That said, they have their place on the Web Platform.
How does a Data URL look
A Data URL is a string that starts with data:
followed by the MIME type format. For example, a PNG image has MIME type image/png
.
This is followed by a comma and then by the actual data.
Text is usually transferred in plain text, while binary data is usually base64 encoded.
Here is an example of how such a Data URL will look like:
<img src="data:image/png,%89PNG%0D%0A..." />
And [here is a small version of the banner image of this article encoded in a link](data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%96%00%00%00c%08%03%00%00%00N%80%09z%00%00%03%00PLTE%FF%FF%FF%FE%FE%FE%FD%FD%FC%FE%FE%FF%FF%FF%FE%FF%FE%FE%F0%EE%ED%E7%E7%E8%E6%E5%E6%E4%E4%E3%E4%E4%E4%F6%F8%F9%E9%E8%E8%E3%E2%E1%F8%FA%FD%E9%E8%E7%E0%E0%E0%F5%F5%F7%FA%F9%F8%E1%E2%E3%F1%EF%EE%E2%E1%E0%F2%F4%F5%95%92%8B%00%00%00%21%1F%1D%E0%E4%EAywo%2F%2B%263AL%E9%EB%ED%FD%FB%FAlg%5B%CC%D1%D7%E9%E7%E4%DB%DF%E3%FC%FC%FB%B4%B1%A8%C0%C6%CE%E1%DF%D930.%00%02%16%D9%DD%E1875%08%05%06%03%01%01%1A%19%19%1E%1B%1B%0C%03%00%EE%ED%EC%FE%FD%FE%F6%F5%F5%E9%EA%EA%EB%EB%E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