Halloween Sale 15% off Ends in See the plans
HTML

Fade In Images as They Load

Makes content images fade in smoothly when they finish loading instead of popping in, for a more polished feel.

Don’t have WPIDE yet? Install it on your site

Code

<style>
@media (prefers-reduced-motion: no-preference) {
    .wpide-fade{opacity:0;transition:opacity .5s ease;}
    .wpide-fade.is-loaded{opacity:1;}
}
</style>
<script>
(function () {
    document.querySelectorAll('.entry-content img, .wp-block-post-content img, .wp-block-image img').forEach(function (img) {
        if (img.complete && img.naturalWidth) { return; }
        img.classList.add('wpide-fade');
        var done = function () { img.classList.add('is-loaded'); };
        img.addEventListener('load', done, { once: true });
        img.addEventListener('error', done, { once: true });
    });
})();
</script>

Notes

Respects the reduced-motion accessibility setting. Images that are already loaded are shown immediately.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.