Kevin Powell - CommunityKP-C
Kevin Powell - Community10mo ago
48 replies
MC23

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
}
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
image.png
Was this page helpful?