Explain Codes LogoExplain Codes Logo

Blank image encoded as data-uri

html
data-uri
image-placeholder
responsive-design
Nikita BarsukovbyNikita Barsukov·Dec 28, 2024
TLDR

Add a 1x1 pixel transparent image into your HTML with this base64 encoded GIF:

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="Instant Tiny Ghost">

No fuss, no wait, it's all encoded, ready, and no additional HTTP requests needed.

Work smarter with data-uri placeholders

Data-uri placeholders are quite handy, because they fill up all image containers instantly with proper dimensions before the actual content loads. This trick is especially useful in image sliders or image galleries with lazy-loading; they ensure everything is smooth for users from the get-go.

<!-- No more awkward half-loaded sliders ruining my day --> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="Quick Fix">

Choose the image type wisely

A minimal data-URI such as src="data:image/gif;base64," could work, but clarity is key. So, specifying the image type, like src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" for a 1x1 transparent GIF, is the way to go.

Transparent or white images? Your choice!

Sometimes you need a 1x1 white image not a transparent one. Here's the white image variant that comes to your rescue:

<!-- You thought it only could be transparent, huh? Well, say hello to the rebel white version! --> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAP7//wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="White Flag">

Optimize loading like a boss

If image load performance is a priority for you, a minimal data-URI can mimic blank images and trick the browser to optimize the loading process, giving your users a faster experience.

<!-- Everybody loves speedy things, right? Here's to less time loading and more time browsing! --> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="Speedy Gonzales">

Visualization

Visualize a blank image encoded as data-uri in HTML like an empty stage:

Stage: [🖼] A blank image is like having a stage set for the performance to begin.

In the code, it appears like:

<!--<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="Empty Stage">-->

And its impact? It's like:

🌐 Web Page Stage Before: [🎨:...] After : [🖼:🎨] // A blank canvas (as an image) is now part of the page, ready to house some epic content.

While it's blank, it's a valid space holder in the HTML gallery, silently waiting to be filled.

Try SVG for more flexibility

While GIFs are most commonly used, SVGs are a modern alternative. They offer scalability and usually have smaller sizes:

<!-- SVGs: the superheroes of modern web design. --> <img src="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>" alt="SVG Superhero">

Never forget to test all browsers

Testing for cross-browser compatibility is crucial. Most modern browsers love data-URIs, but older versions might not be so compliant. No one likes surprises, right?

Accessibility matters, now more than ever

Your data-URI placeholders should still be accessible. If an image conveys content, make sure you use relevant alt text. For pure decoration, let assistive technologies ignore the image with alt="".

<!-- A good web developer never forgets about accessibility --> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="">

Avoid image errors with dynamic loading

Using dynamic image loading methods or JavaScript libraries? Ensure you're using properly encoded data-URIs to avoid image errors or as fallbacks in case network issues disrupt loading of external image resources.

<!-- dynamic image loading with zero errors? Yes, please! --> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="Error-proof">