Hera Zeus
Hera Zeus

Reputation: 163

Make the images same size in grid card

I made some card using grid and flexbox for the content. The image is not the same size each cards. I use the display grid for the wrapper and flexbox for the card content. I tried to use flex-grow and flex-basis in the trackcard_header class, but nothing works. I attach the sample of my code result below, please take a look to the snippet too.

Here is the screenshot: Result

And this is my snippet:

.createpl_card_wrapper {
  margin-top: 2rem;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-column-gap: 2rem;
  grid-row-gap: 1rem;
}
.trackcard {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  border-radius: 1rem;
  background-color: rgba(0, 0, 0, 0.2);
  width: 100%;
  padding: 1rem 0.5rem;
  position: relative;
}
.trackcard_header {
  width: 80%;
  margin: auto;
}
.trackcard_header img {
  max-width: 100%;
  height: auto;
  border-radius: 1rem;
}
.trackcard_body {
  padding: 1rem;
  flex-grow: 1;
  text-align: center;
}
.trackcard_body h2 {
  font-size: 1.5rem;
}
.trackcard_body h4 {
  font-size: 1rem;
}
.trackcard_footer {
  width: 80%;
  margin: auto;
}
.trackcard_btn {
  width: 100%;
  padding: 0.25rem 0;
  background-color: #1db954;
  border-radius: 0.5rem;
  border: 1px solid #1db954;
  color: #fff;
}
.trackcard_btn:hover {
  background-color: #18a349;
}
<!DOCTYPE html>
<html>
  <head>
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
  </head>
  <link rel="stylesheet" href="./src/styles.css" />

  <body>
    <div class="createpl_card_wrapper">
      <div class="trackcard">
        <div class="trackcard_header">
          <img
            src="https://i.scdn.co/image/ab67616d0000b2738c0defcb336a0296eb7d704a"
            alt="name"
          />
        </div>
        <div class="trackcard_body">
          <h2>GANADARA (Feat. IU)</h2>
          <h4>
            Jay Park - GANADARA
          </h4>
        </div>
        <div class="trackcard_footer">
          <button type="button" class="trackcard_btn">
            Select
          </button>
        </div>
      </div>
      <div class="trackcard">
        <div class="trackcard_header">
          <img
            src="https://i.scdn.co/image/ab67616d0000b273c63be04ae902b1da7a54d247"
            alt="name"
          />
        </div>
        <div class="trackcard_body">
          <h2>eight(Prod.&Feat. SUGA of BTS)</h2>
          <h4>
            IU - eight
          </h4>
        </div>
        <div class="trackcard_footer">
          <button type="button" class="trackcard_btn">
            Select
          </button>
        </div>
      </div>
      <div class="trackcard">
        <div class="trackcard_header">
          <img
            src="https://i.scdn.co/image/ab67616d0000b27315cf3110f19687b1a24943d1"
            alt="name"
          />
        </div>
        <div class="trackcard_body">
          <h2>Good day</h2>
          <h4>
            IU - REAL
          </h4>
        </div>
        <div class="trackcard_footer">
          <button type="button" class="trackcard_btn">
            Select
          </button>
        </div>
      </div>
      <div class="trackcard">
        <div class="trackcard_header">
          <img
            src="https://i.scdn.co/image/ab67616d0000b2734ed058b71650a6ca2c04adff"
            alt="name"
          />
        </div>
        <div class="trackcard_body">
          <h2>Celebrity</h2>
          <h4>
            IU - IU 5th Album 'LILAC'
          </h4>
        </div>
        <div class="trackcard_footer">
          <button type="button" class="trackcard_btn">
            Select
          </button>
        </div>
      </div>
      <div class="trackcard">
        <div class="trackcard_header">
          <img
            src="https://i.scdn.co/image/ab67616d0000b273a1d785640d9421ec17ea8fe6"
            alt="name"
          />
        </div>
        <div class="trackcard_body">
          <h2>BBIBBI</h2>
          <h4>
            IU - BBIBBI
          </h4>
        </div>
        <div class="trackcard_footer">
          <button type="button" class="trackcard_btn">
            Select
          </button>
        </div>
      </div>
    </div>

    <script src="src/index.js"></script>
  </body>
</html>

I want to make the images is in the same size, both vertically and horizontally. Could anyone give me solution? Thank you in advance.

Upvotes: 1

Views: 3289

Answers (2)

Sigurd Mazanti
Sigurd Mazanti

Reputation: 2395

That can be achieved with some JavaScript.

First you select all the elements with querySelectorAll. You then make an array of its height with offsetHeight. Math.max returns the largest sum of the input parameter.

In a for loop, you can set the elements minHeight with Math.max. It keeps looping until it finds the length and then applies the styling. Keep in mind this will apply the height of the largest image as min-height on all of your images.

And while we're at it, I did the same thing for the h2-elements, so they're aligned equally in all the cards. I separated the JS so it is easier to read and understand. The only gotcha is you have to reload the page if you drag the screen size, as the min-height will be a bit off.

As for CSS I used grid-template-columns: repeat(5, minmax(200px, 1fr)); instead. This way there will always be 5 columns that are atleast 200px wide, so they have an equal size. If you want a responsive effect, just edit the 5 out with auto-fit, this way they will wrap on smaller screens.

Like so:

grid-template-columns: repeat(5, minmax(auto-fit, 1fr));

If you want to change the width or height of your images, I suggest using max-width and max-height for a responsive effect. If you don't change the width, they will look a bit stretched.

You can apply object-fit: cover; on the images also, this way the image won't be stretched, but a bit of the image will be cut off instead.

I hope this helps you! Snippet below will not work properly, check Codepen: https://codepen.io/sigurdmazanti/pen/qBpLGXe

let img = document.querySelectorAll(".trackcard_header img");
let text = document.querySelectorAll(".trackcard_body h2");

// Image
let imgLength = Array.from(img).map(e => e.offsetHeight);
let maxLength = Math.max(...imgLength);


// H2
let textLength = Array.from(text).map(e => e.offsetHeight);
let maxTextLength = Math.max(...textLength);

// Image for loop
for (let i = 0; i < img.length; i++) {
  img[i].style.minHeight = maxLength + "px";
}

// H2 for loop
for (let i = 0; i < text.length; i++) {
  text[i].style.minHeight = maxTextLength + "px";
}
.createpl_card_wrapper {
  margin-top: 2rem;
  display: grid;
  /*grid-template-columns: repeat(5, 1fr); */
  grid-template-columns: repeat(5, minmax(200px, 1fr));
  grid-column-gap: 2rem;
  grid-row-gap: 1rem;
}

.trackcard {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  border-radius: 1rem;
  background-color: rgba(0, 0, 0, 0.2);
  width: 100%;
  padding: 1rem 0.5rem;
  position: relative;
}

.trackcard_header {
  width: 80%;
  margin: auto;
}

.trackcard_header img {
  max-width: 100%;
  height: auto;
  border-radius: 1rem;
  /* Consider this */
  object-fit: cover;
}

.trackcard_body {
  padding: 1rem;
  flex-grow: 1;
  text-align: center;
}

.trackcard_body h2 {
  font-size: 1.5rem;
}

.trackcard_body h4 {
  font-size: 1rem;
  margin-top: auto;
}

.trackcard_footer {
  width: 80%;
  margin: auto;
}

.trackcard_btn {
  width: 100%;
  padding: 0.25rem 0;
  background-color: #1db954;
  border-radius: 0.5rem;
  border: 1px solid #1db954;
  color: #fff;
}

.trackcard_btn:hover {
  background-color: #18a349;
}
<!DOCTYPE html>
<html>

<head>
  <title>Parcel Sandbox</title>
  <meta charset="UTF-8" />
</head>
<link rel="stylesheet" href="./src/styles.css" />

<body>
  <div class="createpl_card_wrapper">
    <div class="trackcard">
      <div class="trackcard_header">
        <img src="https://i.scdn.co/image/ab67616d0000b2738c0defcb336a0296eb7d704a" alt="name" />
      </div>
      <div class="trackcard_body">
        <h2>GANADARA (Feat. IU)</h2>
        <h4>
          Jay Park - GANADARA
        </h4>
      </div>
      <div class="trackcard_footer">
        <button type="button" class="trackcard_btn">
            Select
          </button>
      </div>
    </div>
    <div class="trackcard">
      <div class="trackcard_header">
        <img src="https://i.scdn.co/image/ab67616d0000b273c63be04ae902b1da7a54d247" alt="name" />
      </div>
      <div class="trackcard_body">
        <h2>eight(Prod.&Feat. SUGA of BTS)</h2>
        <h4>
          IU - eight
        </h4>
      </div>
      <div class="trackcard_footer">
        <button type="button" class="trackcard_btn">
            Select
          </button>
      </div>
    </div>
    <div class="trackcard">
      <div class="trackcard_header">
        <img src="https://i.scdn.co/image/ab67616d0000b27315cf3110f19687b1a24943d1" alt="name" />
      </div>
      <div class="trackcard_body">
        <h2>Good day</h2>
        <h4>
          IU - REAL
        </h4>
      </div>
      <div class="trackcard_footer">
        <button type="button" class="trackcard_btn">
            Select
          </button>
      </div>
    </div>
    <div class="trackcard">
      <div class="trackcard_header">
        <img src="https://i.scdn.co/image/ab67616d0000b2734ed058b71650a6ca2c04adff" alt="name" />
      </div>
      <div class="trackcard_body">
        <h2>Celebrity</h2>
        <h4>
          IU - IU 5th Album 'LILAC'
        </h4>
      </div>
      <div class="trackcard_footer">
        <button type="button" class="trackcard_btn">
            Select
          </button>
      </div>
    </div>
    <div class="trackcard">
      <div class="trackcard_header">
        <img src="https://i.scdn.co/image/ab67616d0000b273a1d785640d9421ec17ea8fe6" alt="name" />
      </div>
      <div class="trackcard_body">
        <h2>BBIBBI</h2>
        <h4>
          IU - BBIBBI
        </h4>
      </div>
      <div class="trackcard_footer">
        <button type="button" class="trackcard_btn">
            Select
          </button>
      </div>
    </div>
  </div>

  <script src="src/index.js"></script>
</body>

</html>

Upvotes: 1

cSharp
cSharp

Reputation: 3169

You need to give the divs an actual size, in your code, they only have % values and no parent static value to take the %age out of. I've given the .trackcard class a width of 30vw. In order to make the images square, you also need to give it a height that's not dependent on other height values, but have the same value as the width, so I gave it a placeholder of 24vw which is 80% of the .trackcard. You need to use object-fit: contain to not stretch. You can refer to the docs I linked and use the one you prefer.

.createpl_card_wrapper {
  margin-top: 2rem;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-column-gap: 2rem;
  grid-row-gap: 1rem;
}
.trackcard {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  border-radius: 1rem;
  background-color: rgba(0, 0, 0, 0.2);
  width: 30vw;
  padding: 1rem 0.5rem;
  position: relative;
}
.trackcard_header {
  width: 80%;
  margin: auto;
}
.trackcard_header img {
  width: 24vw;
  height: 24vw;
  object-fit: contain;
  border-radius: 1rem;
}
.trackcard_body {
  padding: 1rem;
  flex-grow: 1;
  text-align: center;
}
.trackcard_body h2 {
  font-size: 1.5rem;
}
.trackcard_body h4 {
  font-size: 1rem;
}
.trackcard_footer {
  width: 80%;
  margin: auto;
}
.trackcard_btn {
  width: 100%;
  padding: 0.25rem 0;
  background-color: #1db954;
  border-radius: 0.5rem;
  border: 1px solid #1db954;
  color: #fff;
}
.trackcard_btn:hover {
  background-color: #18a349;
}
<!DOCTYPE html>
<html>
  <head>
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
  </head>
  <link rel="stylesheet" href="./src/styles.css" />

  <body>
    <div class="createpl_card_wrapper">
      <div class="trackcard">
        <div class="trackcard_header">
          <img
            src="https://i.scdn.co/image/ab67616d0000b2738c0defcb336a0296eb7d704a"
            alt="name"
          />
        </div>
        <div class="trackcard_body">
          <h2>GANADARA (Feat. IU)</h2>
          <h4>
            Jay Park - GANADARA
          </h4>
        </div>
        <div class="trackcard_footer">
          <button type="button" class="trackcard_btn">
            Select
          </button>
        </div>
      </div>
      <div class="trackcard">
        <div class="trackcard_header">
          <img
            src="https://i.scdn.co/image/ab67616d0000b273c63be04ae902b1da7a54d247"
            alt="name"
          />
        </div>
        <div class="trackcard_body">
          <h2>eight(Prod.&Feat. SUGA of BTS)</h2>
          <h4>
            IU - eight
          </h4>
        </div>
        <div class="trackcard_footer">
          <button type="button" class="trackcard_btn">
            Select
          </button>
        </div>
      </div>
      <div class="trackcard">
        <div class="trackcard_header">
          <img
            src="https://i.scdn.co/image/ab67616d0000b27315cf3110f19687b1a24943d1"
            alt="name"
          />
        </div>
        <div class="trackcard_body">
          <h2>Good day</h2>
          <h4>
            IU - REAL
          </h4>
        </div>
        <div class="trackcard_footer">
          <button type="button" class="trackcard_btn">
            Select
          </button>
        </div>
      </div>
      <div class="trackcard">
        <div class="trackcard_header">
          <img
            src="https://i.scdn.co/image/ab67616d0000b2734ed058b71650a6ca2c04adff"
            alt="name"
          />
        </div>
        <div class="trackcard_body">
          <h2>Celebrity</h2>
          <h4>
            IU - IU 5th Album 'LILAC'
          </h4>
        </div>
        <div class="trackcard_footer">
          <button type="button" class="trackcard_btn">
            Select
          </button>
        </div>
      </div>
      <div class="trackcard">
        <div class="trackcard_header">
          <img
            src="https://i.scdn.co/image/ab67616d0000b273a1d785640d9421ec17ea8fe6"
            alt="name"
          />
        </div>
        <div class="trackcard_body">
          <h2>BBIBBI</h2>
          <h4>
            IU - BBIBBI
          </h4>
        </div>
        <div class="trackcard_footer">
          <button type="button" class="trackcard_btn">
            Select
          </button>
        </div>
      </div>
    </div>

    <script src="src/index.js"></script>
  </body>
</html>

Upvotes: 3

Related Questions