Next.js Discord

Discord Forum

How to Keep Images the Same Size in Each Card

Unanswered
Jersey Wooly posted this in #help-forum
Open in Discord
Jersey WoolyOP
This is my ProductCard component:
export default function ProductCard({
  props,
}: {
  props: ProductCardProps;
}) {
  return (
    <div data-product-id={props.productUrl}>
      <Card className="block relative">
        <CardContent className="px-2">
          <a href={props.productUrl}>
            <figure>
              <Image
                decoding="async"
                src={getImageSrcPath(props.imageSrc)}
                alt={props.heading}
                height={300}
                width={300}
              />
            </figure>
            <h2 className="min-h-16 text-lg leading-none font-semibold mx-0 my-3">{props.heading}</h2>
            <div className="mb-1.5 pr-0.5 font-medium text-base text-black">
              {i18n.localCurrency.symbol} {props.localPrice}
            </div>
          </a>
        </CardContent>
      </Card>
    </div>
  );
}

These I show in my ProductGrid component:
export default async function ProductGrid({ gridKey, products }: { gridKey: number, products: ProductCardProps[] }) {
  return (
    <div className="container m-auto grid gap-1 lg:gap-2 xl:gap-5 grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 2xl:grid-cols-6" >
      {products.map((item, index) => (
        <ProductCard key={`${gridKey}-${index}`} props={item} />
      ))}
    </div >
  );
}

My problem here, and I'm a frontend noob, is that despite the cards in the grid all being the same width, and me putting height and width of 300 on the Image in the card, many cards in the grid have different heights due to them having different size image files.

6 Replies

American black bear
the main problem here is images being of different sizes as you mentioned
use css to lock the aspect ratio which will make all cards the same size
className="aspect-square w-full object-cover"
this will cut some images a bit but there is no cleaner way to do it other than to take all product images in the same format
Dutch
i stopped frontend dev cuz of that yes
@American black bear this will cut some images a bit but there is no cleaner way to do it other than to take all product images in the same format
Jersey WoolyOP
Perfect, thank you. The only one of those classes I've seen before is w-full, but it works great. The images provided by the client are crap anyway and he really needs to work on them, so he can get someone to size them or pay me extra to do so.