I'm working on an image gallery site, which shows a bunch of images of different sizes and aspect ratios. The images should be shown many on a line if they fit.
Each image should be say no wider than 20vw, and no taller than 300px.
Critically, I don't want any "jank" as the page loads - each picture should take up it's space before it has loaded so it all loads smoothly without jank / layout shift, even when internet is slow.
Now, I've managed to achieve this for images that are just sitting straight in the document.
But I can't get it to work when they are wrapped inside a <figure> element. (Eventually I want to add <figcations> to them, but this is not relevant for my question). The figure elements could just as well be <div>s or something else.
An example will illustrate what I'm trying to do:
* {
margin: 0;
}
body {
background-color: lightgray;
}
figure {
display: inline-block;
background-color: lightblue;
}
img {
max-width: 20vw;
max-height: 300px;
height: auto;
width: auto;
background-color: black;
display: inline-block;
}
figure img {
background-color: gray;
}
<h3>These 2 images take up space correctly while loading:</h3>
<img src="https://deelay.me/1500/https://dummyimage.com/300x600/f0f/000.png" width="300" height="600">
<img src="https://deelay.me/1500/https://dummyimage.com/400x200/0f0/000.png" width="400" height="200">
<h3>These 2 images just jank into existence when they load:</h3>
<figure>
<img src="https://deelay.me/1500/https://dummyimage.com/500x1000/f00/000.png" width="500" height="1000">
</figure>
<figure>
<img src="https://deelay.me/1500/https://dummyimage.com/300x150/0f0/000.png" width="400" height="200">
</figure>
<p>
some text that shouldn't move as the page loads
</p>
How can I make the second two images inside <figure>s take up space before they've loaded?