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.

Styling stretches inline images instead of scaling them

See original GitHub issue

This comes from https://github.com/mdn/yari/issues/1734#issuecomment-731337332

If you have

<p>
<img alt="The default styles used by a browser" src="https://mdn.mozillademos.org/files/16493/html-example.png" style="border: 1px solid #cccccc">
</p>

It looks good: Screen Shot 2020-11-20 at 1 29 54 PM on http://localhost:3000/en-US/docs/Learn/CSS/First_steps/What_is_CSS

But if you correctly set the image width and height attributes:

<p>
<img height="594" width="1385"
alt="The default styles used by a browser" src="https://mdn.mozillademos.org/files/16493/html-example.png" style="border: 1px solid #cccccc"
>
</p>

then this happens:

Screen Shot 2020-11-20 at 1 31 14 PM

Having the height set is good because it means the browser doesn’t need to shift all the stuff below it as the image loads. This causes CLS (Cumulative Layout Shift) which is bad for our Web Perf metrics which could affect our SEO. And just generally make people less happy consuming the content.

Is there not a way to do the CSS so that images are allowed to have their correct size set but “scale” it so it displays as well as it can?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisdavidmillscommented, Nov 20, 2020

Looks like the dimensions are not being set correctly, so that the aspect ratio is incorrect.

If you only set the width, the height is usually adjusted proportionately so the aspect ratio is maintained, and I think it still gives you a box to help avoid CLS. Would that work?

0reactions
peterbecommented, Nov 30, 2020

I love it! Thanks to everyone involved! Now we have the ideal solution. If you set height="594" width="1385" on the <img tag there, you get this: Screen Shot 2020-11-30 at 10 38 26 AM It’s hard to tell but thanks to the height tag on the <img the browser doesn’t do any layout shift whilst waiting for the image.

Now I need to write that piece of code that opens the image, gets its width and height and uses the Cheerio instance to set it based on the binary image size (there’s a node lib for that).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stretch and scale a CSS image in the background
This scales the image so that the background area is completely covered by the background image while maintaining the aspect ratio.
Read more >
How to Resize Images Using CSS for Responsive Web Design
You can solve this by configuring the height and width properties. Stretch: Use the “100% 100%” background-size property to accomplish the following: Stretch ......
Read more >
image-rendering - CSS: Cascading Style Sheets | MDN
The image-rendering CSS property sets an image scaling algorithm. The property applies to an element itself, to any images set in its other ......
Read more >
How To Scale and Crop Images with CSS object-fit
Learn how to use the CSS object-fit property to scale and crop images and maintain aspect ratio.
Read more >
image-rendering - CSS-Tricks
This value only applies to images that are scaled up. This property can be applied to background images, canvas elements as well inline...
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