You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

网格项内的Div无法继承网格项高度问题求助

问题描述

使用浏览器检查工具时,发现类名为items的div尺寸为36.90x0,无法占满accounts-grid-item的高度。尝试给.items设置height:100%但无效,已知html标签设置了100vh,希望找到让items高度相对于accounts-grid-item控制的方法。

现有代码

CSS代码

.container {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.wrapper {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
  font-size: 32px;
}

.account-intro {
  height: 10%;
  width: 60%;
  display: flex;
  flex-direction: column;
  font-weight: bold;
  font-size: 20px;
}

.accounts {
  height: 50%;
  width: 60%;
  display: grid;
  grid-template-columns: auto auto auto;
  gap: 10px;
}

button {
  border-radius: 8px;
  color: #fff;
  background-color: #1976d2;
  font-size: 1.1em;
  border: 0;
  cursor: pointer;
  margin: 1em;
}

.accounts-grid-item {
  background-color: #16330014;
  padding: 80px;
  font-size: 30px;
  text-align: center;
  border-radius: 5%;
  height: 60%;
  border: none;
  display: flex;
}

.items {
  height: 100%;
  background-color: red;
  width: 100%;
}

HTML代码

<div class="container">
  <div class="wrapper">
    <section class="account-intro">
      <p>Account</p>
    </section>
    <section class="accounts">
      <ng-container *ngFor="let account of accountDetails">
        <div class="accounts-grid-item">
          <div class="items"></div>
        </div>
      </ng-container>
    </section>
  </div>
</div>

解决方案

修改要点

  1. 给.accounts-grid-item添加box-sizing: border-box,让设置的height包含padding值,避免内部可用空间被padding挤压;
  2. 将.items的height:100%替换为flex:1,利用flex布局特性让子元素自动占满父容器的所有可用高度,这种方式比百分比高度更适配flex容器的逻辑。

修改后的关键CSS代码

.accounts-grid-item {
  background-color: #16330014;
  padding: 80px;
  font-size: 30px;
  text-align: center;
  border-radius: 5%;
  height: 60%;
  border: none;
  display: flex;
  box-sizing: border-box; /* 新增:padding计入高度计算 */
}

.items {
  flex: 1; /* 替换height:100%,自动填充父容器高度 */
  background-color: red;
  width: 100%;
}

额外验证

如果仍存在高度异常,可确认.accounts的高度是否正常传递:.wrapper为flex column布局且设置了height:100%,.accounts的height:50%会正确继承该高度,确保.accounts-grid-item的height:60%有明确的参照基准。


内容的提问来源于stack exchange,提问作者Kwaku Biney

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 12:15:30