I'm building a CSS grid layout in PHP/HTML5 for displaying mainly images with text and sometimes in combination with videos (not used in this example). One thing still annoys me is the page jumping while images are loading.
To avoid that the img tag should know either threw CSS or otherwise what the ratio is beforehand, most of the images used have a 1:1,414 ratio but there are exceptions so using just one ratio wouldn't work. Adding width and height on each img tag didn't work either.
I'm using a base64 transparent gif so it has something to load before the real image is downloaded. In the CSS each image has a placeholder color (grey) before so you can see that something is loading. Image width is set to 100% (what is good) and height is set to auto in CSS. Show here a small part of the HTMl+CSS coding for the grid and placing images:
.grid-container img {
display: block;
width: 100%;
height: auto;
}
.grid-1,
.grid-2,
.grid-2-left,
.grid-3,
.grid-3-left,
.grid-3-thumb,
.grid-4,
.grid-4-left,
.grid-4-thumb {
align-self: center;
/* start | end | center | stretch */
position: relative;
/* for title-project placement */
background-color: #ccc;
/* placeholder */
}
.grid-2,
.grid-2-txt {
grid-column: span 6;
}
<div class="grid-2">
<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://i.imgur.com/eys1d9h.jpg" class="lazyload" alt="<?php echo $title; ?>">
</div>