I am using CSS Grid to layout a thumbnail gallery, no Javascript or any other responsive techniques. I would like to set the height of thumbnails to be as its width (1:1) square ratio.
Why? when the page loads I need the thumbnail div's to take space, because now if there is no Image inside the thumbnails div it doesn't take any space.
Here is a Live example: https://www.w3schools.com/code/tryit.asp?filename=FMCULIDQUOBX
Here is the Code:
.container {max-width: 900px;}
.gallery {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-gap: 15px;
}
.thumbnail {box-shadow: 0 4px 4px rgba(0,0,0,0.50);}
img {width: 100%; display: block;}
...............................
<div class="container">
<div class="gallery">
<div class="thumbnail">
<a href="large-image.jpg">
<img src="thumbnail-image.jpg">
</a>
</div>
<div class="thumbnail">
...... same as above ......
</div>
<div class="thumbnail">
...... same as above ......
</div>
<div class="thumbnail">
...... same as above ......
</div>
</div>
</div>
The Code above divides the width (900px) into four fractions, and the 4 thumbnails are placed in 1 row. That's why I don't define any width.
If Images don't load, the thumbnail div wont even be visible.
In other words, if you disable images in browser, thumbnail div should take space in a square shape.
How to fix this? Thanks
fr
units`. – Behavewidth
andheight
attributes in the HTML, so that the browser will know what the aspect ratio is upfront ... then it can calculate the correct height according to the width the image is display with before it has loaded the actual image. – Icalheight:auto
for the images in your CSS. – Icalheight:auto
doesn't make the div a square. Try adding that and disable Images in browser. – Haymesalt
attribute. I have tried with padding-bottom with no luck. Will try again with some modification to make it work. – Haymes