I just found this, my only question is, Who surfs the web with CSS turned off?
Also, if it’s display none, how would you even see it anyway?
It is prudent to specify a width and height of “1” in case a browser is surfing with CSS turned off. One-pixel square images would be little if any distraction.
As has been explained on numerous occasions, there is no “best” or “one-size-fits-all” solution to coding. The choice of approach depends on the actual situation. Rather than posting random versions of code from various places and asking “which is best”, you would get much more useful responses if you explained what problem you are seeking to address, and asked how best to approach it.
These days I think the most intuitive method is what is referred to as ‘lazy loading’.
Something similar to this -
Lazy Load delays loading of images in long web pages. Images outside of viewport will not be loaded before user scrolls to them. This is opposite of image preloading.
This is a modern vanilla JavaScript version of the original Lazy Load plugin. It uses Intersection Observer API to observe when the image enters the browsers viewport.
What I like about it is that it uses the vanilla js framework that weighs in at an astonishing 0 bytes.
Believe it or not, there are still people that do not have high speed broadband connections, I’m one of them. I can tell when a site takes forever to load due to images that are not optimized or “Preloading Images” that I may not even be interested in viewing. Especially common with image galleries, hence the reason they need real thumbnail images and not just scaled down versions of the large image.