DIV中的两个DIV.如何通过第二个DIV自动填充父DIV的空间?

Nir*_*man 11 html css height autofill

请访问这个小提琴,看看我的意思 -

我有一个父DIV,其中有两个DIV以垂直顺序放置.顶部DIV应仅具有其内容的高度,而底部DIV应占据父DIV的所有剩余空间,而不考虑内容高度,并且也不应与父DIV重叠.

HTML:

<div class="outer">
    <div  class="inner-title">
        THIS IS MY TITLE
    </div>
    <div class="inner-content">
        CONTENT AREA
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

html,body
{
height: 100%;
}

.outer
{
    background-color:blue;
    height: 80%;
}

.inner-title
{
    background-color:red;
}

.inner-content
{
background-color:yellow;
    height: auto;
}
Run Code Online (Sandbox Code Playgroud)

简而言之,"内部内容"应该占据"外部"DIV的所有剩余空间,而不会重叠.

知道怎么做到这一点?

任何帮助都非常感谢.

Sow*_*mya 15

添加 display:table;到父div和 display:table-row;子div

height:0为第一个孩子div.这需要自动高度

    html,body{
    height: 100%;
}
.outer{
    background-color:blue;
    height: 80%; border:red solid 2px;
    display: table;
     width:100%
}
.inner-title{
    background-color:red;
    display:table-row; 
     width:100%
}
.inner-content{
    background-color:grey;
    display:table-row;
    width:100%;
    height:100%
}
Run Code Online (Sandbox Code Playgroud)

演示更新


Rok*_*jan 8

较新的CSS方式将使用flexbox

/*QuickReset*/ *{box-sizing:border-box;margin:0;} html,body{height:100%;}

.flex-col {
  display: flex;
  flex-direction: column; /* or:   flex-flow: column wrap; */
  height: 100%;
}

.flex-fill { /* Add this class to the element you want to expand */
  flex: 1;  
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex-col">
    <div style="background:red;"> HEADER </div>
    <div class="flex-fill" style="background:yellow;"> CONTENT </div>
    <div style="background:red;"> FOOTER</div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • imho,比前一个更好的解决方案 (2认同)