Home  >  Q&A  >  body text

I'm having trouble with a CSS grid where the height isn't what I expected, what mistakes might I be making?

I'm currently learning CSS Grid and was asked to make this card by breaking it down into a grid. Attached is a picture of the grid I want to make.

There is actually a larger grid (called profile-grid in CSS) that all these cards are arranged in, and the height of this larger grid is 255px. I want both the card and the grid inside the card to follow this 255px height, I've managed to get the height of the card itself correct, but the height of the inner grid does not. The inner grid itself consists of 2 rows, the first row is the picture and has a height of 150px, the second row is set to 1fr, but I can't get the inner grid to have a height of 255px. Did something go wrong?

https://i.stack.imgur.com/g9Eh6.png

The following is the jsFiddle link: https://jsfiddle.net/40tnwd1o/

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap');
body {
  font-family: roboto;
}

p {
  display: block;
  margin: auto;
}

.profile-card {
  margin-top: 0px;
  width: 150px;
  height: 255px;
  border: none;
  box-shadow: 0px 0px 5px 3px rgba(73, 73, 73, 0.301);
}

.profile-grid {
  display: grid;
  grid-template-columns: 100%;
  grid-template-rows: 150px 1fr;
}

.social-ava {
  width: 100%;
  height: 100%;
  background-color: gray;
  transition: opacity 0.15s;
}

.social-ava:hover {
  opacity: 0.8;
}

.social-text {
  height: 100%;
  padding: 8px;
}

.social-name {
  margin-top: 0px;
  cursor: pointer;
  transition: color 0.15s;
}

.social-name:hover {
  color: rgb(52, 98, 167);
}

.mutual-grid {
  display: grid;
  grid-template-columns: 20px 1fr;
  margin-top: 6px;
  align-items: center;
}

.mutual-pic {
  width: 20px;
  height: 20px;
  background-color: gray;
  border-radius: 10px;
  cursor: pointer;
}

.mutual-friend {
  font-size: 12px;
  color: rgb(80, 80, 80);
  cursor: pointer;
}

.mutual-friend:hover {
  text-decoration: underline;
}

.social-add {
  margin-top: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  color: white;
  background-color: rgb(25, 118, 240);
  font-size: 12px;
  cursor: pointer;
  transition: opacity 0.1s;
}

.social-add:hover {
  opacity: 0.8;
}
<!-- profile card start -->
<div class="profile-card">

  <!-- profile grid start -->
  <div class="profile-grid">

    <!-- row 1: picture start -->
    <div class="image-container">
      <div class="social-ava"></div>
      <!-- placeholder for profile picture -->
    </div>
    <!-- row 1: picture end -->

    <!-- row 2: info start -->
    <div class="social-text">
      <p class="social-name"><strong>Name</strong></p>

      <div class="mutual-grid">
        <!-- grid for mutual friends info start -->
        <div class="mutual-pic"></div>
        <!-- placeholder for mutual's profile picture -->
        <p class="mutual-friend">2 mutual friends</p>
      </div>
      <!-- grid for mutual friends info end -->

      <button class="social-add">Add Friend</button>
    </div>
    <!-- row 2: info end -->

  </div>
  <!-- profile grid end -->

</div>
<!-- profile card end -->

P粉875565683P粉875565683176 days ago347

reply all(1)I'll reply

  • P粉710478990

    P粉7104789902024-04-07 11:53:54

    i.stack.imgur.com/g9Eh6.png This is the height of profile-grid, not profile-card. A height of 255px is set on profile-card.

    You need to add height:100% to profile-grid.

    @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap');
    body {
      font-family: roboto;
    }
    
    p {
      display: block;
      margin: auto;
    }
    
    .profile-card {
      margin-top: 0px;
      width: 150px;
      height: 255px;
      border: none;
      box-shadow: 0px 0px 5px 3px rgba(73, 73, 73, 0.301);
    }
    
    .profile-grid {
      display: grid;
      height: 100%;
      grid-template-columns: 100%;
      grid-template-rows: 150px 1fr;
    }
    
    .social-ava {
      width: 100%;
      height: 100%;
      background-color: gray;
      transition: opacity 0.15s;
    }
    
    .social-ava:hover {
      opacity: 0.8;
    }
    
    .social-text {
      height: 100%;
      padding: 8px;
    }
    
    .social-name {
      margin-top: 0px;
      cursor: pointer;
      transition: color 0.15s;
    }
    
    .social-name:hover {
      color: rgb(52, 98, 167);
    }
    
    .mutual-grid {
      display: grid;
      grid-template-columns: 20px 1fr;
      margin-top: 6px;
      align-items: center;
    }
    
    .mutual-pic {
      width: 20px;
      height: 20px;
      background-color: gray;
      border-radius: 10px;
      cursor: pointer;
    }
    
    .mutual-friend {
      font-size: 12px;
      color: rgb(80, 80, 80);
      cursor: pointer;
    }
    
    .mutual-friend:hover {
      text-decoration: underline;
    }
    
    .social-add {
      margin-top: 6px;
      padding: 8px 16px;
      border: none;
      border-radius: 4px;
      color: white;
      background-color: rgb(25, 118, 240);
      font-size: 12px;
      cursor: pointer;
      transition: opacity 0.1s;
    }
    
    .social-add:hover {
      opacity: 0.8;
    }
    <!-- profile card start -->
    <div class="profile-card">
    
      <!-- profile grid start -->
      <div class="profile-grid">
    
        <!-- row 1: picture start -->
        <div class="image-container">
          <div class="social-ava"></div>
          <!-- placeholder for profile picture -->
        </div>
        <!-- row 1: picture end -->
    
        <!-- row 2: info start -->
        <div class="social-text">
          <p class="social-name"><strong>Name</strong></p>
    
          <div class="mutual-grid">
            <!-- grid for mutual friends info start -->
            <div class="mutual-pic"></div>
            <!-- placeholder for mutual's profile picture -->
            <p class="mutual-friend">2 mutual friends</p>
          </div>
          <!-- grid for mutual friends info end -->
    
          <button class="social-add">Add Friend</button>
        </div>
        <!-- row 2: info end -->
    
      </div>
      <!-- profile grid end -->
    
    </div>
    <!-- profile card end -->

    reply
    0
  • Cancelreply