What is the attribute for loading
in the images?
To mismatch the standard image loading behavior in an HTML document. The attribute loading
is an enumeration that you may have two values:
eager
, that causes the image to load immediately (along with the loading of the page itself), without considering its position in the document. It is the standard behavior.
lazy
(novelty), which causes the image to be loaded only if it is in the imminence to enter the viewport user (ie what it sees).
The immediate advantage that is perceived is the postponement (and eventually even waiver) of the loading of images that the user will not immediately view. In environments (such as mobile phones) where internet access is a little less powerful, failing to load a heavier resource (like image) is something of extreme value in terms of user experience.
How the option works lazy
in the attribute loading
specifically?
Operation may vary slightly between browsers, but generally when defining loading="lazy"
in some element that supports it, the browser follows these steps.
Assuming I lower the term "images" refers to elements that support the configuration loading="lazy"
and with the attribute loading
defined as lazy
, one has:
As images who are in the viewport user are immediately loaded. Behavior is analogous to loading="eager"
, which is the standard.
The images that are relatively below the viewport user are not loaded.
- Once the user, when performing scroll by the page, approaching the image, the browser starts the loading process. The distance (referred to as threshhold may vary[1]) for the load to start is variable.
- Most of the time the image will have already been loaded when the viewport user to target. In this sense, most of the time, the image is loaded from the imminence of becoming visible.
It is important to note that in order for the browser to calculate the distance it needs wait around to load the image more accurately, it is ideal and recommended that whenever an image is defined with loading="lazy"
also specify width and height (through attributes such as width
and height
, respectively). For example:
<img src="image.png" loading="lazy" width="200" height="200" alt="..." />
This attribute is tag specific <img />
or works in others tags?
As far as I could tell, loading="lazy"
is functional in elements of tag img
and iframe
.
Endnotes
This behaviour is liable to polyfill. An example.
Like the support is still relatively low, you can check whether the browser supports it as follows:
// Para <img>:
HTMLImageElement.prototype.hasOwnProperty('loading'); //=> true ou false
// Para <iframe>:
HTMLIFrameElement.prototype.hasOwnProperty('loading'); //=> true ou false
For further deepening, I suggest reading of this article.
I don’t understand what it has to do with marketing...
– Luiz Felipe
The galley uses it for content engagement. So much so that the content is read and then the images are loaded.
– Wemerson Nino