When the height of the sub-container of the css flex vertical layout sub-container is 100%, the content will exceed

problem description

recently, I found a strange problem when using flex to do the interface. When laying out vertically, there is a fixed height toolbar at the top and a full content container at the bottom (height:100%, and allows scaling). The layout is normal at first.
but there is a problem after putting a height:100% div in the content container. The height of the main container will not be reduced by flex, causing the content to be highly stretched.

related codes

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>(runoob.com)</title>
<style> 
    -sharpmain
    {
        width:220px;
        height:300px;
        padding:1px;
        box-sizing:border-box;
        border:1px solid black;
        display:flex;
        flex-direction:column;
        justify-content:space_between;
        align-items:stretch;
    }

    div
    {
        box-sizing:border-box;
    }
    .d1{
        width:100%;
        height:40px;
        margin-bottom:20px;
        background-color:coral;
        flex-shrink:0;
    }
    .d2{
        height:100%;
        padding:5px;
        background-color:lightblue;
        opacity:0.8
    }
    .dd2{
        height:100%;
        background-color:-sharpe1e1e1;
    }
</style>
</head>
<body>
    <div id="main">
      <div class="d1"></div>
      <div class="d2">
        <div class="dd2"></div>    
      </div>
    </div>
</body>
</html>

because the actual use should be uncertain whether the content with the bottom of the top exists, the height of the content container cannot be set to the top. Ask for advice on how to correct this problem? Tell him to stay high.

Aug.25,2021

what does div.d1 's margin-bottom:20px do?

the height:100% of div.d2 must have exceeded, using height:calc (100%-40px).


D2 do not set height , but set overflow-y:auto
write a demo, head that is optional. The height of the content container is always as high as possible and will not exceed the main container

.
<style>
* {
  padding: 0;
  margin: 0;
}
.wrapper{
  height: 100vh;
  display: flex;
  overflow: hidden;
  flex-direction: column;
  justify-content: space-between;
}
.head{
  height: 88px;
  background: grey;
  flex-shrink: 0;
}
.content{
  border: 10px solid lightblue;
  overflow-y: auto;
}
.word{
  height: 4000px;
}
</style>

<body>
  <div class="wrapper">
    <div class="head"></div>
    <div class="content">
      <div class="word"></div>
    </div>
  </div>
</body>

clipboard.png


the order of DOM rendering is to render the parent node first, then the child node, and then the sibling node in the front, and then the sibling node in the back. So when the child node is rendered, the parent node is already rendered. The height of the child node here is based on the parent node, so the parent node does not know how much it is when rendering, so it naturally overflows.
in this case, the child node adaptation is usually set, such as

in your example.
.d2 {
    flex: auto;
} 

in addition, look at the

written in your code.
div {
    width: 100%;
}
The width of the

div block-level element itself is adaptive (here the flex-direction is vertical, and this feature is maintained), which is redundant.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bdc447-312dd.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bdc447-312dd.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?