I am trying to create a simple thumbnail gallery with each image has a text below it. However, since the images I am dealing with have various sizes, I decided to crop and center each image in the gallery. I actually did this following the instructions given here:
https://jonathannicol.com/blog/2014/06/16/centre-crop-thumbnails-with-css/
However, texts of the images appear on the image. I could not find a way to move the text to the below. I tried adding padding to bottom but padding-bottom
is not working. I suspect it is due to height: auto
line. Somehow this setting preventing me to adjust padding in the vertical direction.
My html:
<div class="gallery">
<img src="https://placehold.it/900x500" alt="">
<div class="desc">My Text</div>
</div>
<div class="gallery">
<img src="https://placehold.it/500x900" class="portrait" alt="">
<div class="desc">My long long text</div>
</div>
Styling:
.gallery {
position: relative;
width: 200px;
height: 200px;
overflow: hidden;
margin-top: 5vh;
margin-right: 0.5vh;
border: 1px solid #ccc;
float: left;
}
.gallery img {
position: absolute;
left: 50%;
top: 50%;
height: 100%;
width: auto;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.gallery img.portrait {
width: 100%;
height: auto;
}
.desc {
display: block;
position: absolute;
top: 85%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
jssfiddle:
https://jsfiddle.net/8ue603on/
Thank you!