【问题标题】:Two DIVs inside DIV. How to auto-fill the space of parent DIV by second DIV?DIV 中的两个 DIV。如何通过第二个 DIV 自动填充父 DIV 的空间?
【发布时间】:2013-07-09 13:15:39
【问题描述】:

请访问this fiddle 了解我的意思-

我有一个父 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>

CSS:

html,body
{
height: 100%;
}

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

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

.inner-content
{
background-color:yellow;
    height: auto;
}

简而言之,“inner-content”应该占据“outer”DIV的所有剩余空间,不重叠。

知道如何实现这一目标吗?

非常感谢任何帮助。

【问题讨论】:

    标签: css html height autofill


    【解决方案1】:

    一种较新的 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;  
    }
    <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>

    【讨论】:

    • 比上一个更好的解决方案,恕我直言
    【解决方案2】:

    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%
    }
    

    DEMO UPDATED

    【讨论】:

    • @sowmya.. 为第二个 div 设置最小高度怎么样??
    • 你需要最小高度吗? Op 期望 div 占据整个可用空间
    • 他的问题不是很好,因为第一个 div 高度没有固定,所以
    • @Nirman 这是因为我在第一个 div 中添加了更多内容以显示示例。立即查看更新的答案
    • @Sowmya:现在有更好的基于 flexbox 的解决方案吗?还是 display:table 仍然是唯一的方法?
    猜你喜欢
    • 2012-12-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    相关资源
    最近更新 更多