How to make div occupy remaining height?

前端 未结 11 991
遇见更好的自我
遇见更好的自我 2020-11-27 12:49

I have this problem, I have two divs:

<
相关标签:
11条回答
  • 2020-11-27 13:18

    Since you know how many pixels are occupied by the previous content, you can use the calc() function:

    height: calc(100% - 50px);
    
    0 讨论(0)
  • 2020-11-27 13:22
    <div>
      <div id="header">header</div>
      <div id="content">content</div>
      <div id="footer">footer</div>
    </div>
    
    #header {
      height: 200px;
    }
    
    #content {
      height: 100%;
      margin-bottom: -200px;
      padding-bottom: 200px;
      margin-top: -200px;
      padding-top: 200px;
    }
    
    #footer {
      height: 200px;
    }
    
    0 讨论(0)
  • 2020-11-27 13:28

    With CSS tables, you could wrap a div around the two you have there and use this css/html structure:

    <style type="text/css">
    .container { display:table; width:100%; height:100%;  }
    #div1 { display:table-row; height:50px; background-color:red; }
    #div2 { display:table-row; background-color:blue; }
    </style>
    
    <div class="container">
        <div id="div1"></div>
        <div id="div2"></div>
    </div>
    

    Depends on what browsers support these display types, however. I don't think IE8 and below do. EDIT: Scratch that-- IE8 does support CSS tables.

    0 讨论(0)
  • 2020-11-27 13:32

    I faced the same challenge myself and found these 2 answers using flex properties.

    CSS

    .container {
      display: flex;
      flex-direction: column;
    }
    
    .dynamic-element{
      flex: 1;
    }
    
    • https://stackoverflow.com/a/35348188/1084619
    • https://stackoverflow.com/a/35348188/1084619
    0 讨论(0)
  • 2020-11-27 13:34

    You can use this http://jsfiddle.net/Victornpb/S8g4E/783/

    #container {
        display: table;
        width: 400px;
        height: 400px;
    }
    #container > div{
        display: table-row;
        height: 0;
    }
    #container > div.fill{
        height: auto;
    }
    

    Just apply the class .fill to any of the children to make then occupy the remaining height.

    <div id="container">
        <div>
            Lorem ipsum
        </div>
        <div>
            Lorem ipsum
        </div>
        <div class="fill">   <!-- this will fill the remaining height-->
            Lorem ipsum
        </div>
    </div>
    

    It works with how many children you want, no additional markup is required.

    0 讨论(0)
提交回复
热议问题