I have adapted a grid layout tutorial and have it working almost as I want. However, I would like to maintain the aspect ratio of the images as the screen size changes. I can’t see how the usual approach of setting padding-bottom could be used in this instance.
Hi,
That method applies to an <img> in the html. If you remember it uses absolute positioning on the image and a wrapping div (with the padding) to scale the img.
Your codepen is setting the images as background-images and it’s using background-size:cover on the li.
.boxes li {
background-size: cover;
display: flex;
align-items: flex-end;
}
If the images are indeed part of the pages content they should be moved to the html as an <img>
I’m not too familiar with grid yet but you may be able to use object-fit to handle the scaling on the img in that case.
You’re right, of course. The images are part of the content so yes they should not be background images. That should make adding the overlay a bit more tricky.
Hi gandalf,
I went ahead and played around with object-fit on the images, I was curious about it too.
My layout uses flexbox but you may be able to merge the concept into grid.
I found it awkward to work with object-fit while the images have the size attributes on them in the html. Come to find out, that is what the object-position property is for. But when going that route it makes the <img> element a container like a div then positions the rendered image within the remaining space.
Other examples I found would set a fixed height on the image in the css and I didn’t really like that either because it seemed to interfere with aspect scaling.
Regardless, it seemed to cause alignment problems for me when I tried to use the size attributes along with the caption text.
I wound up using absolute positioning for the captions so they laid on top the images.