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

子元素设max-height:100%仍超出父容器的原因排查

CSS布局溢出问题排查与解决

问题描述

父容器#main-container继承了body的100vh明确高度,子元素#main-body设置了height:100%和max-height:100%,但仍超出父容器。已排查父元素未定义高度、额外元素导致溢出这两种常见诱因,均不成立。

相关代码

html {
    box-sizing: border-box;
  }
  *, *:before, *:after {
    box-sizing: inherit;
}

html,body{
    height: 100vh;
    max-height: 100vh;
    width: 100vw;
    margin:0px;
    padding:0px;
    font-family: "Roboto";
    color:white;
    font-weight: bold;
}

body{
    display:flex;
    justify-content: center;
    background-color: black;
}

body>div{
    height:100%;
    max-height:100%;
}

div{
    border: none;
}

h2{
    padding:0;
    margin:0;
}

#main-container{
    width:80%;
    height:100%;
    margin:0px;
    padding:0px;
    display: grid;
    grid-template-columns: 1fr 4fr;
}

#sidebar{
    background-color:#3b82f6;
}

#sidebar-header{
    display: grid;
    grid-template-columns: 1fr 4fr;
    gap:50px;
    align-items: center;
    border:none;
    margin-bottom: 125px;
    padding-left:50px;
}

#sidebar-header-image{
    height: 170px;
}

#sidebar-heading{
    font-size:80px;
}

#first-sidebar-container{
    margin-bottom: 150px;
}

li{
    display:grid;
    grid-template-columns: 0.9fr 5fr;
    grid-template-rows: 150px;
    gap:80px;
}

.icon{
    height: 70px;
    filter:invert(100%) sepia(0%) saturate(0%) hue-rotate(338deg) brightness(102%) contrast(105%);
    justify-self: right; 
}

.icon-text{
    font-size: 60px;
    border: none;
}


.sidebar-container{
    display:grid;
    list-style:none;
}

.sidebar-container.first{
    margin-bottom: 100px;
}

#main-body{
    max-height: 100%;
    height:100%;
    background-color: white;
    display:grid;
    grid-template-rows: auto 1fr;
    color:black;
}

#header{
    box-shadow: 1px 9px #E5E7EB;
    padding-bottom: 12px;
}

#header-top-section{
    margin-top:40px;
    display:flex;
    gap: 60px;
    align-items:center;
}

.header-icon:nth-child(1){
    margin-left:100px;
}

.header-icon:nth-child(3){
    margin-left:300px;
}

.header-icon{
    height:75px;
}

#search{
    flex-grow:1;
    height:100px;
    border-radius: 100px;
}

#top-profile-picture{
    height:150px;
    border-radius: 50%;
}

#top-username{
    margin-right: 250px;;
    font-size: 60px;
    font-weight: 800;
}

#main-header-section{
    height: 240px;
    display: grid;
    grid-template-columns: 1.8fr 1fr;
    margin: 80px 120px 50px;
    padding-right:30px;
}

#profile-section{
    display:grid;
    grid-template-rows: 1fr 1fr;
    grid-template-columns: min-content auto;
    column-gap: 60px;
    row-gap:30px;
}

#main-profile-picture{
    height: 240px;
    border-radius: 50%;
    grid-row: 1/3;
}

#side-text{
    font-size:40px;
    align-self: end;
}

#profile-name{
    font-size:70px;
    align-self: start;
}

#options-section{
    display:flex;
    justify-content: space-between;
    align-items: center;
}

.option{
    height:100px;
    background-color:#3b82f6;
    border-radius: 100px;
    width:300px;
    height:115px;
    display:flex;
    justify-content: center;
    align-items: center;
    font-size:45px;
    color:white;
}

#main-content{
    display:grid;
    grid-template-columns: 3fr 1.2fr;
    gap:60px;
    padding: 95px 100px 130px;
}

#main-content>div{
    border: 3px black solid;
}

#cards-container{
    display: grid;
    grid-template-rows: auto 1fr;
}

#cards-header{
    margin: 0;
    margin-bottom: 10px;
    padding: 0;
    font-size:60px;
    font-weight: bold;
    color: black;
}

#cards{
    max-height: 100%;
    display:grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 30% 30% 30%;
    gap:75px;
    padding-top: 20px;;
}

页面截图

页面布局截图


问题原因与修复方案

核心原因

  1. Grid容器行高未约束:#main-container是Grid布局,但未设置grid-template-rows: 1fr,默认行高为auto(由内容撑开)。此时#main-body的height:100%是基于Grid项的内容高度,而非父容器的100vh高度,导致子元素不受父容器高度限制。
  2. 外边距/内边距叠加溢出:#header和#main-content中的大数值margin(如#header-top-section的margin-top:40px、#main-header-section的margin:80px 120px 50px)不会被box-sizing: border-box包含,直接增加元素总高度,叠加后超出父容器。
  3. 卡片行高计算错误:#cards用grid-template-rows: 30% 30% 30%加gap:75px,三个30%的行高+间隙总高度超过容器100%高度,进一步加剧溢出。

修复步骤

  1. 约束Grid行高:给#main-container添加grid-template-rows: 1fr,让Grid行强制占满父容器高度,#main-body无需再设置height和max-height:
#main-container{
    width:80%;
    height:100%;
    margin:0px;
    padding:0px;
    display: grid;
    grid-template-columns: 1fr 4fr;
    grid-template-rows: 1fr; /* 添加此行 */
}
  1. 替换外边距为内边距:把#header中的margin改为padding(内边距会被box-sizing包含,不会额外增加高度):
#header-top-section{
    padding-top:40px; /* 替换原margin-top:40px */
    display:flex;
    gap: 60px;
    align-items:center;
}
#main-header-section{
    height: 240px;
    display: grid;
    grid-template-columns: 1.8fr 1fr;
    padding: 80px 120px 50px; /* 替换原margin:80px 120px 50px */
    padding-right:30px;
}
  1. 调整卡片行高:将#cards的行高改为repeat(3, 1fr),让卡片自动均分剩余高度:
#cards{
    max-height: 100%;
    display:grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: repeat(3, 1fr); /* 修改此行 */
    gap:75px;
    padding-top: 20px;;
}

内容的提问来源于stack exchange,提问作者20centuryboy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 13:27:02