Skip to content
Advertisement

Lazy loading images with accessibility and printer support

I am looking for a proper way to implement lazy loading of images without harming printability and accessibility, and without introducing layout shift (content jump), preferrably using native loading=lazy and a fallback for older browsers. Answers to the question How lazy loading images using JavaScript works? included various solutions none of which completely satisfy all of these requirements.

An elegant solution should be based on valid and complete html markup, i.e. using <img src, srcset, sizes, width, height, and loading attributes instead of putting the data into data- attributes, like the popular javascript libraries lazysizes and vanilla-lazyload do. There should be no need to use <noscript> elements either.

Due to a bug in chrome, the first browser to support native lazyloading, images that have not yet been loaded will be missing in the printed page.

Both javascript libraries mentioned above, require either invalid markup without any src attribute at all, or an empty or low quality placeholder (LQIP), while the src data is put into data-src instead, and srcset data put into data-srcset, all of which only works with javascript. Is this considered an acceptable or even best practice in 2020, and does this neither harm the site accessibility, cross-device compatibility, nor search engine optimization?

Update:

I tried a workaround for the printing bug using only HTML and CSS @media print background images in this codepen . Even if this worked as intended, there would be a necessary css directive for each and every image, which is neither elegant nor generic. Unfortunately there is no way to use media queries inside the <picture> element either.

There is another workaround by Houssein Djirdeh at at lazy-load-with-print-ctl1l4wu1.now.sh using javascript to change loading=lazy to loading=eager when a “print” button is clicked. The same function could also be used onbeforeprint.

I made a codepen using lazysizes.

I made another codepen using vanilla-lazyload .

I thought about forking a javascript solution to make it work using src and srcset, but this must probably have been tried before, the tradeoff would be that once the lazyloading script starts to act on the image elements, the browser might have already started downloading the source files.

Advertisement

Answer

The proper solution for printable lazy loading in 2022 is using the native loading attribute.

<img loading=lazy>

The recommendation to use a custom print button has been obsoleted as chromium issue 875403 got fixed.

Prior recommendations included adding a custom print button (which did not fix the problem when using the native browser print functionality) or using JavaScript to load images onBeforePrint the latter not being considered a good solution, as loading=lazy, as a “DOM-only” solution, must not rely on JavaScript.

Beware that, even after the bug fix, some of your users might still visit your site with a buggy browser version.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement