Styling stretches inline images instead of scaling them
See original GitHub issueThis 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: 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:
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:
- Created 3 years ago
- Comments:11 (6 by maintainers)
Top 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 >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
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?
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: It’s hard to tell but thanks to theheight
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
andheight
and uses the Cheerio instance to set it based on the binary image size (there’s a node lib for that).