Native lazy-loading (loading=lazy) not working even with flags enabled - html

I am currently trying to update my Website using the new loading="lazy" attribute as shown here: https://web.dev/native-lazy-loading
As seen in the video, everything works as expected, but compared with my waterfall diagram in chrome, it doesn't.
How it looks:
How it should look:
This is how its implemented:
<img class="has-border" src="https://andreramoncombucket.s3.amazonaws.com/static/assets/img/work/personal-website/pw_full.jpg" style="object-fit: cover;" alt="..." loading="lazy">

I had a similar issue when trying to implement it.
I use Chrome by default and it was not working. When I tested it in Firefox, it did work. That made me think it was a browser problem.
After digging in a bit more, I found out the "problem" for my case. It might be the same probably for many others.
It turns out Chrome is more impatient than Firefox when loading images tagged as lazy. That means it loads the images much earlier, so an image will not be loaded when it appears at the screen but earlier than that.
Firefox, on the other side, is loading the images almost when they are about to be shown at the screen.
The images I was testing were below the fold, but the page was not very long, so Chrome was loading the images anyway.
When I tried it in a much longer article, the images that were deep down the article did load lazily in Chrome as well.
Hope this helps!

I had a similar problem, and after some research, I found the solution:
Just need to add width and height to the IMG tag, this is because the browser needs to know the size of the element before applying lazy loading.
Your code:
<img class="has-border" src="..." style="object-fit: cover;" alt="..." loading="lazy">
Your code after adding width and height:
<img class="has-border" src="..." style="object-fit: cover;" alt="..." loading="lazy" width="200px" height="200px">
Another alternative is using inline style:
<img class="has-border" src="..." style="object-fit:cover; height:200px; width:200px;" alt="..." loading="lazy">
Take into consideration I just randomly set the dimensions of the IMG tag to 200px.
You can find more information on this web.dev article
hope it helps 👍

I have the worst reason it wasn't working - I forgot to add the width and height attributes (they were instead added as styles in my broken code)

optionally if you dont want to change your image size use this
Original code
<img class="has-border" src="..." style="object-fit: cover;" alt="..." loading="lazy">
With working Lazy load with no size restrictions
<img class="has-border" src="..." style="object-fit: cover;" alt="..." loading="lazy" width="auto" height="100%">

I have applied loading="lazy" to all of my images, included width="111px" height="111px" attributes for my <img> tags and even converted them into WebP format to reduce size, but my Firefox browser still keeps loading all images from the entire page. I don't touch or scroll after refresh.
The indicator Dev tools says that it is - lazy-img.
I use Nodejs, vue and tailwind.
What could be wrong?

It could be because of cache.
If you see here lazy-img in the Network tab that means everything is fine.
You can test on a private window if you want just to double check.

The image should be positioned as "relative" in style.

Related

html picture element not honoring small media query

I've got a thing like this:
<picture>
<source media="(min-width:1200)" srcset="big.jpg">
<source media="(min-width:840)" srcset="small.jpg">
<img srcset="big.jpg" alt="Test" />
</picture>
I'm also using picturefill.
My issue is that both firefox and chrome (the 2 I'm currently testing on), will load only big.jpg, even in small screens. Checked also on console's network trace.
My css sets img as basic fluid:
img{
display: block;
max-width: 100%;
height:auto;
}
So what's wrong in my code?
As for I haz kode's comment, the answer is: my code lacked unit declaration in media queries.
As for completeness, I also write here a better code for my use case: having a default image for small screens and different one from 840px up.
<picture>
<source media="(min-width:840px)" srcset="big.jpg">
<img srcset="small.jpg" alt="Foradori">
</picture>
This will work correctly and download only the right image for the current breakpoint.
Also remember we need picturefill to ensure crossbrowser support.

Can I use srcset attribute in css to edit resolution?

I was wondering if I could somehow use the srcset attribute in css to (for example) make all images 2x, it usually works like this
<img src="image.jpg" srcset="image.jpg 1x, higher-resolution-image.jpg 2x" >
I wonder if there's a way to use that in css to make all images displayed in 2x
Thanks in advance :D
You should find this post useful:
Is there a srcset equivalent for css background image
It mentions using image-set:
background: -webkit-image-set( url('path/to/image') 1x, url('path/to/high-res-image') 2x );

Browser hiding images when site online, works as intended offline

Code works fine when offline, however when I upload it to the site the images for the social media buttons don't show, on further inspection the code being overridden by the browser
it looks like:
<img src="images/facebook.png" class="socialbuttons" alt="find on facebook" width="0" height="0" style="display: none !important; visibility: hidden !important; opacity: 0 !important; background-position: 0px 0px;">
instead of:
<img src="images/facebook.png" class="socialbuttons" alt="find on facebook">
for the images. I've tried using different images, changing the class to something else and viewing on other browsers.
I tried to override it using !important but that doesn't work either.
The address of the site is
http://markncarolproperties.co.uk/
stack overflow won't let me post images, imgur links instead
https://imgur.com/a/WZieW
Simple solution is:
customize your adBlock
add your website to the "Show ads on a webpage or domain" list
I use Chrome with adblock and the images come up just fine. If it is still an issue, try changing the names to something simpler like FB.png ?
Without being able to replicate the issue or see it as it is working, I cannot even use Chrome's Inspect Element tool.

HTML img rover effect changes size of image

I have the following code to create a rollover effect in HTML:
<img src="images/facebook.png"
onmouseover='this.src="images/facebookActive.png"'
onmouseout='this.src="image/facebook.png"' height="32px" width="32px"/>
It starts out fine, however upon hovering over it, it goes back to its original size (500x500). I have attempted to add height="32px" width="32px" to each attribute, however it doesn't seem to help. Also, by decreasing the size of the image using GIMP, quality is lost and it becomes more pixelated. Any help on this situation?
I tried to reproduce your scenario but I'm unable to do that. So can you edit this JsFiddle to reproduce your issue. try to use your images.
<img src="http://netdna.copyblogger.com/images/flogo.jpg" onmouseover='this.src="http://t1.gstatic.com/images?q=tbn:ANd9GcQ6y qrcyQQxC6hISHmE37Yk2IUsOQtACy8NvcQgxMA0Uk3er5y"' onmouseout='this.src="http://netdna.copyblogger.com/images/flogo.jpg"' height="32px" width="32px" />
It seems to be working fine. Also Try re sizing the image using PNGGauntlet
Have you tried setting the this.width and this.height in onmouseover and onmouseout?

How to Preload Images without Javascript?

On one of my HTML pages there are some large images that are shown when I mouse hover over some links and it takes time to load those images.
I don't want to use JavaScript to preload images. Are there any good solutions?
HTML5 has a new way to do this, by link prefetching.
<link rel="prefetch" href="http://davidwalsh.name/wp-content/themes/walshbook3/images/sprite.png" />
Just add as many link tags as you need in your HTML and you are good to go. Of course, older browsers will not load the content this way.
Note
Above code will not work for Apple Safari safari does not support prefetch til now version 12 https://caniuse.com/#search=prefetch
UPDATE
If your server is served with HTTP2, you can also add a Link header in your response a made use of HTTP2 Server Push.
Link: <http://davidwalsh.name/wp-content/themes/walshbook3/images/sprite.png>; rel=preload; as=image;
From http://snipplr.com/view/2122/css-image-preloader
A low-tech but useful technique that uses only CSS. After placing the css in your stylesheet, insert this just below the body tag of your page: Whenever the images are referenced throughout your pages they will now be loaded from cache.
#preloadedImages
{
width: 0px;
height: 0px;
display: inline;
background-image: url(path/to/image1.png);
background-image: url(path/to/image2.png);
background-image: url(path/to/image3.png);
background-image: url(path/to/image4.png);
background-image: url();
}
There is no need to preload images. I can't understand why 99% people thinks, that hover effects have to use 2 images. There is no such need, and using 2 images makes it look bad.
The only good solution I know is to use CSS for A elements (or easy JS for all other buttons). When button us hovered set background-position to some offset.
a { display:block; width:160px; height:26px; background:url(b_tagsdesc.png); }
a:hover { background-position:0 26px }
That's all, image used you can see below:
(source: margonem.pl)
Edit: You can also use it other way. Instead of toggling image, you can hide your image. So starting point would be "background-position:0 -100px" and on hover "0 0".
This technique is called CSS sprites - here is good description of it: http://css-tricks.com/css-sprites/
A technique I didn't see mentioned here yet, which works great if you don't need to worry about IE6 & 7, is to use the :after pseudo-selector to add your images as content to an element on the page. Code below lifted from this article:
body:after {
content: url(img01.jpg) url(img02.jpg) url(img03.jpg);
display: none;
}
The only downside I can see to this compared to using JavaScript to preload the images is that there is no easy way to release them from memory.
You could use a hidden div to put images in. Like so
<html>
<body>
<div style="width:1px; height:1px; visibility:hidden; overflow:hidden">
<img src="img1.jpg" /><img src="img2.jpg" />
</div>
<div>Some html</div>
</body>
</html>
This only works for images though, ie. if you're trying to do the same for say .swf files there will be problems. Firefox doesn't run the .swf file if it's not visible.
<link rel="preload" as="image" href="..." />
This works best for me when we want to load image early for CSS
(while rel="prefetch" will cause duplicate loading from CSS)
Reference your images in invisible img tags. while page loading they will downloaded too.
As I'm not sure if hidden images are loaded, you'll probably want to use image sprites. Then the entire image is loaded before anything is displayed. You can even put all of your menu items in one image and therefore save a lot of HTTP requests.
If preloading images is what you seek, then performance is what you want. I doubt blocking up the page while the resources load is what you want. SO, just place some prefetching links at the end of the body and add the bogus media to them to make them appear unimportant (so that they get loaded after everything else). Then, add the onload tag to those links, and in that onload will be the code that sets the source of the actual resource in your page. For example, this could even be used in conjunction with dynamic iframes.
Before:
<a onclick="myFrame.style.opacity=1-myFrame.style.opacity;myFrame.src='http://jquery.com/'">
Click here to toggle it
</a><br />
<iframe id="myFrame" src="" style="opacity: 0"></iframe>
After:
<a onclick="myFrame.style.opacity=1-myFrame.style.opacity">
Click here to toggle it
</a><br />
<iframe id="myFrame" src="" style="opacity: 0"></iframe>
<link rel="prefetch" href="http://jquery.com/"
onload="myFrame.src=this.href" media="bogus" />
Notice how the first one (before) freezes up the page and blinks in a quite ugly manner while the second one (after) with the content preloaded doesn't freeze up the page or blink, rather it appears and disappears seamlessly instantly.
Can't you add them as an <img /> tag to your page and hide them using css?