Deal with images of different aspect ratio inside grids

What I get is the thing you can see in the pic attached. This is the code behind it:
.posters {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, auto)); // fluid grid
justify-content: center;
gap: 3px;
max-width: 1300px;
margin: 0 auto;
}

.poster {
position: relative;
background-color: red;
}

.poster__image {
width: 100%;
height: 100%; // solves the problem, but Lighthouse gives me a warning about the image not having the original ratio
}
.posters {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, auto)); // fluid grid
justify-content: center;
gap: 3px;
max-width: 1300px;
margin: 0 auto;
}

.poster {
position: relative;
background-color: red;
}

.poster__image {
width: 100%;
height: 100%; // solves the problem, but Lighthouse gives me a warning about the image not having the original ratio
}
I'd like some ideas on how to deal with a fluid image inside a grid while keeping the original aspect ratio and also having it look nice
No description
29 Replies
ἔρως
ἔρως6d ago
you might want this: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit also, // is an invalid comment in css also, try removing the height
MC23
MC23OP6d ago
I'm using SCSS Removing the height is what causes the problem, if I add height: 100% it works I tried this but it doesn't do anything, ecept for none that displays everything worse https://byteshaman.github.io/my-tv-list/ not really a CodePen link, but the code can be edited directly from the browser easily enough (you have to delete height: 100% to notice the problem, and there won't be a red bg color)
ἔρως
ἔρως6d ago
this is what i got
No description
ἔρως
ἔρως6d ago
on .poster, i've added this:
display: flex;
align-content: center;
justify-content: center;
align-items: center;
aspect-ratio: 2 / 3;
display: flex;
align-content: center;
justify-content: center;
align-items: center;
aspect-ratio: 2 / 3;
and on .poster__image i've replaced everything with this:
max-height: 100%;
object-fit: fill;
max-height: 100%;
object-fit: fill;
that random image is 600x400
MC23
MC23OP6d ago
Ohh sweet, I didn't think about using flex
ἔρως
ἔρως6d ago
im using flex just to center the image and not for the flex things
MC23
MC23OP6d ago
Ok I'm doing some tests
ἔρως
ἔρως6d ago
alright
MC23
MC23OP6d ago
I wasn't aware of the aspect-ratio thing, it's really cool
ἔρως
ἔρως6d ago
it's pretty useful oh, wait i fixed a bug on that
ἔρως
ἔρως6d ago
this is the bug: taller images aren't being cropped
No description
ἔρως
ἔρως6d ago
the fix is simple - use this for the .poster__image:
width: 100%;
height: 100%;
object-fit: cover;
width: 100%;
height: 100%;
object-fit: cover;
and images of any size will fit in here
No description
MC23
MC23OP6d ago
Oh neat Thank you
ἔρως
ἔρως6d ago
is this what you wanted?
MC23
MC23OP6d ago
I'll need to check if the Lighthouse error is gone
ἔρως
ἔρως6d ago
it won't be gone specially since you didnt even say anything about it before
MC23
MC23OP6d ago
I said it here when talking about the aspect ratio The biggest problem is performance, and I'm working on it
ἔρως
ἔρως6d ago
that's probably solved as an accident
MC23
MC23OP6d ago
It's this one I referred to
No description
MC23
MC23OP6d ago
I set a new way to get my images, so that I only choose the width (300px) so it might work better now I'm doing some tests
ἔρως
ἔρως6d ago
you have a height of 150px minimum maybe start from there, instead of the width, if you can i mean, width sorry 150px minimum of width 300px is good
MC23
MC23OP6d ago
Yeah that's because on smallest screens I want to display 2 posters
ἔρως
ἔρως6d ago
i know i just misread the grid code
MC23
MC23OP6d ago
Oh no worries
ἔρως
ἔρως6d ago
but honestly, i wouldn't worry about that message on lighthouse you can, however, set a fixed width and height using properties
MC23
MC23OP6d ago
I'd just like to achieve a perfect score as a challenge :>
ἔρως
ἔρως6d ago
you'll be better off finding an unicorn on a pink moon sunset while perfectly sober
ἔρως
ἔρως6d ago
100% is possible, but it doesn't always show how good or bad the site is

Did you find this page helpful?