Inject image width/height to avoid cumulative layout shift
See original GitHub issueCLS (cumulative layout shift) is when what you’re looking at sudden moves down right in front of your eyes as stuff like images finally come in, somewhere above.
Example: on http://localhost:5000/en-us/docs/Web/CSS/CSS_Box_Alignment
Since we control the <img src>
tags in Yari we can use things like image-size
which is pure Node JavaScript and doesn’t read in the whole file so it’s fast.
Now we can automatically convert the:
<img src="screenshot.png" alt="Bla">
…into…
<img src="/en-US/docs/Current/Doc/screenshot.png" alt="Bla" width="430" height="260">
Now you get this result:
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (5 by maintainers)
Top Results From Across the Web
Optimize Cumulative Layout Shift - web.dev
Summary: Always include width and height size attributes on your images and video elements. Alternatively, reserve the required space with CSS ...
Read more >Set height of image with 100% width for CLS (Cumulative ...
In the below fiddle I load a large image (you may still want to add throttling to see there is no layout shift)....
Read more >Set a Width and Height on Images to Reduce Layout Shifts
Solution: Set a Width and Height on Images. To prevent these kinds of layout shifts, you should give your images explicit dimensions using...
Read more >How To Fix Cumulative Layout Shift (CLS) Issues
Set Width And Heights On Images And iFrames #. I've written about this before, but one of the easiest things you can do...
Read more >How to reduce Cumulative Layout Shift (CLS) - Blog - Marfeel
To prevent cumulative layout shift from images, always include width and height size attributes on your images and video elements.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m increasingly thinking that this should be handled by editors but kindly asked …
Responding to your “devil’s advocating”… If we allow people to upload a 2000x2000 PNG but you explicitly set the width like
<img src="pic.png" width="200">
then it’s likely that people end up downloading a LOT more data to be able to see a smaller image and it’s not any crisper. Resized down to 200px it’s very possible that no human eye can tell it apart from one that natively 200px wide. But the amount of data transferred might be 1MB vs 50KB which hurts every single reader indefinitely. And implementing complicated override’able rules and exceptions might lead to complexity that is undesirable.(I feel like you knew ALL of that perfectly well but I think it’s fair to log the complete argument here)