question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inject image width/height to avoid cumulative layout shift

See original GitHub issue

CLS (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 Screen-Recording-2020-10-20-at-9 56 23-AM

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: Screen-Recording-2020-10-20-at-10 01 13-AM

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Jan 7, 2021

I’m increasingly thinking that this should be handled by editors but kindly asked …

0reactions
peterbecommented, Jan 7, 2021

I tend to agree with @peterbe in #1498 (comment) - generally you should try upload an image to correct size, and this should be the minimum size that conveys the relevant information. If only because big files are heavy for recipients and unlike server-based wiki systems, static sites can’t dynamically resize images on sending (not to mention when we get the source locally).

Devils advocate:

* you can't regrow an image you've shrunk. In another site I recently added the feature to allow images to  expand when clicked. This is really useful in some cases, but diligent pruning of image sizes over the years means that much of the benefit was lost.

* When using images within a table or other layout structure you don't always know the ideal layout size when you're starting. I find it useful to be able to set widths, but I almost never set heights.

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)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found