Relative div height

后端 未结 6 1757
自闭症患者
自闭症患者 2021-02-19 06:07

I want to split up the view in four parts. A header at the top, using full page width and fixed height.

The remaining page is split up in two blocks of the same height,

相关标签:
6条回答
  • 2021-02-19 06:09

    Basically, the problem lies in block12. for the block1/2 to take up the total height of the block12, it must have a defined height. This stack overflow post explains that in really good detail.

    So setting a defined height for block12 will allow you to set a proper height. I have created an example on JSfiddle that will show you the the blocks can be floated next to one another if the block12 div is set to a standard height through out the page.

    Here is an example including a header and block3 div with some content in for examples.

    #header{
       position:absolute;
       top:0;
       left:0;
       width:100%;
       height:20%;
        }
    
        #block12{
        position:absolute;
        top:20%;
        width:100%;
        left:0;
        height:40%;
        }
        #block1,#block2{
        float:left;
        overflow-y: scroll;
        text-align:center;
        color:red;
        width:50%;
        height:100%;
    
        }
     #clear{clear:both;}
     #block3{
        position:absolute;
        bottom:0;
        color:blue;
        height:40%;
    
        }
    
    0 讨论(0)
  • 2021-02-19 06:10

    When you set a percentage height on an element who's parent elements don't have heights set, the parent elements have a default

    height: auto;
    

    You are asking the browser to calculate a height from an undefined value. Since that would equal a null-value, the result is that the browser does nothing with the height of child elements.

    Besides using a JavaScript solution you could use this deadly easy table method:

    #parent3 {
        display: table;
        width: 100%;
    }
    
    #parent3 .between {
        display: table-row;
    }
    
    #parent3 .child {
        display: table-cell;
    }
    

    Preview on http://jsbin.com/IkEqAfi/1

    • Example 1: Not working
    • Example 2: Fix height
    • Example 3: Table method

    But: Bare in mind, that the table method only works properly in all modern Browsers and the Internet Explorer 8 and higher. As Fallback you could use JavaScript.

    0 讨论(0)
  • 2021-02-19 06:22

    The div take the height of its parent, but since it has no content (expecpt for your divs) it will only be as height as its content.

    You need to set the height of the body and html:

    HTML:

    <div class="block12">
        <div class="block1">1</div>
        <div class="block2">2</div>
    </div>
    <div class="block3">3</div>
    

    CSS:

    body, html {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
    }
    .block12 {
        width: 100%;
        height: 50%;
        background: yellow;
        overflow: auto;
    }
    .block1, .block2 {
        width: 50%;
        height: 100%;
        display: inline-block;
        margin-right: -4px;
        background: lightgreen;
    }
    .block2 { background: lightgray }
    .block3 {
        width: 100%;
        height: 50%;
        background: lightblue;
    }
    

    And a JSFiddle

    0 讨论(0)
  • 2021-02-19 06:24

    add this to you CSS:

    html, body
    {
        height: 100%;
    }
    

    working Fiddle

    when you say to wrap to be 100%, 100% of what? of its parent (body), so his parent has to have some height.

    and the same goes for body, his parent his html. html parent his the viewport.. so, by setting them both to 100%, wrap can also have a percentage height.

    also: the elements have some default padding/margin, that causes them to span a little more then the height you applied to them. (causing a scroll bar) you can use

    *
    {
        padding: 0;
        margin: 0;
    }
    

    to disable that.

    Look at That Fiddle

    0 讨论(0)
  • 2021-02-19 06:32

    Percentage in width works but percentage in height will not work unless you specify a specific height for any parent in the dependent loop...

    See this : percentage in height doesn’t work?

    0 讨论(0)
  • 2021-02-19 06:33

    add this to your css:

    html, body{height: 100%}
    

    and change the max-height of #block12 to height

    Explanation:

    Basically #wrap was 100% height (relative measure) but when you use relative measures it looks for its parent element's measure, and it's normally undefined because it's also relative. The only element(s) being able to use a relative heights are body and or html themselves depending on the browser, the rest of the elements need a parent element with absolute height.

    But be careful, it's tricky playing around with relative heights, you have to calculate properly your header's height so you can substract it from the other element's percentages.

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