首页  >  问答  >  正文

我在使用CSS网格时遇到了高度不符合预期的问题,我可能犯了哪些错误?

我目前正在学习CSS网格,并且被要求通过将其分解为网格来制作这张卡片。附上了我想要制作的网格的图片。

实际上有一个更大的网格(在CSS中称为profile-grid),所有这些卡片都排列在其中,这个更大的网格的高度为255px。我想让卡片和卡片内部的网格都遵循这个255px的高度,我成功地使卡片本身的高度正确,但是内部网格的高度却没有。内部网格本身由2行组成,第一行是图片,高度为150px,第二行设置为1fr,但是我无法使内部网格的高度为255px。有哪个地方出错了吗?

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

以下是jsFiddle链接: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 天前350

全部回复(1)我来回复

  • P粉710478990

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

    i.stack.imgur.com/g9Eh6.png 这是 profile-grid 的高度,而不是 profile-cardprofile-card 上设置了 255px 的高度。

    你需要在 profile-grid 上添加 height:100%

    @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 -->

    回复
    0
  • 取消回复