CSS How to set div height 100% minus nPx

前端 未结 11 1316
自闭症患者
自闭症患者 2020-12-02 04:17

I have a wrapper div which contans 2 divs next to each other. Above this container I have a div that contains my header. The wrapper div must be 100% minus the height of the

相关标签:
11条回答
  • 2020-12-02 05:05

    Here is a working css, tested under Firefox / IE7 / Safari / Chrome / Opera.

    * {margin:0px;padding:0px;overflow:hidden}
    div {position:absolute}
    div#header {top:0px;left:0px;right:0px;height:60px}
    div#wrapper {top:60px;left:0px;right:0px;bottom:0px;}
    div#left {top:0px;bottom:0px;left:0px;width:50%;overflow-y:auto}
    div#right {top:0px;bottom:0px;right:0px;width:50%;overflow-y:auto}
    

    "overflow-y" is not w3c-approved, but every major browser supports it. Your two divs #left and #right will display a vertical scrollbar if their content is too high.

    For this to work under IE7, you have to trigger the standards-compliant mode by adding a DOCTYPE :

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <style type="text/css">
    	*{margin:0px;padding:0px;overflow:hidden}
    	div{position:absolute}
    	div#header{top:0px;left:0px;right:0px;height:60px}
    	div#wrapper{top:60px;left:0px;right:0px;bottom:0px;}
    	div#left{top:0px;bottom:0px;left:0px;width:50%;overflow-y:auto}
    	div#right{top:0px;bottom:0px;right:0px;width:50%;overflow-y:auto}
    </style>
    </head>
    <body>
    <div id="header"></div>
    <div id="wrapper">
      <div id="left"><div style="height:1000px">high content</div></div>
      <div id="right"></div>
    </div>
    </body>

    0 讨论(0)
  • 2020-12-02 05:10

    In this example you can identify different areas:

    <html>
    <style>
    #divContainer {
        width: 100%;
        height: 100%;
    }
    #divHeader {
        position: absolute;
        left: 0px;
        top: 0px;
        right: 0px;
        height: 28px;
        background-color:blue;
    }
    #divContentArea {
        position: absolute;
        left: 0px;
        top: 30px;
        right: 0px;
        bottom: 30px;
    }
    #divContentLeft {
        position: absolute;
        top: 0px;
        left: 0px;
        width: 200px;
        bottom: 0px;
        background-color:red;
    }
    #divContentCenter {
        position: absolute;
        top: 0px;
        left: 200px;
        bottom: 0px;
        right:200px;
        background-color:yellow;
    }
    #divContentRight {
        position: absolute;
        top: 0px;
        right: 0px;
        bottom: 0px;
        width:200px;
        background-color:red;
    }
    #divFooter {
        position: absolute;
        height: 28px;
        left: 0px;
        bottom: 0px;
        right: 0px;
        background-color:blue;
    }
    </style>
    <body >
    
    <div id="divContainer">
        <div id="divHeader"> top
        </div>
        <div id="divContentArea">
            <div id="divContentLeft">left
            </div>
            <div id="divContentCenter">center
            </div>
            <div id="divContentRight">right
            </div>
        </div>
        <div id="divFooter">bottom
        </div>
    </div>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-02 05:13

    Use an outer wrapper div set to 100% and then your inner wrapper div 100% should be now relative to that.

    I thought for sure this used to work for me, but apparently not:

    <html>
    <body>
    <div id="outerwrapper" style="border : 1px solid red ; height : 100%">
    <div id="header" style="border : 1px solid blue ; height : 60px"></div>
    <div id="wrapper"  style="border : 1px solid green ; height : 100% ; overflow : scroll ;">
      <div id="left" style="height : 100% ; width : 50% ; overflow : scroll; float : left ; clear : left ;">Some text 
    
    on the left</div>
      <div id="right" style="height : 100% ; width 50% ; overflow : scroll; float : left ;">Some Text on the 
    
    right</div>
    </div>
    </div>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-02 05:14

    In CSS3 you could use

    height: calc(100% - 60px);
    
    0 讨论(0)
  • 2020-12-02 05:18

    I haven't seen anything like this posted yet, but I thought I'd put it out there.

    <div class="main">
    <header>Header</header>
    <div class="content">Content</div>
    

    Then CSS:

    body, html {
        height: 100%;
        margin: 0;
        padding: 0;
    }
    
    .main {
        height: 100%;
        padding-top: 50px;
        box-sizing: border-box;
    }
    
    header {
        height: 50px;
        margin-top: -50px;
        width: 100%;
        background-color: #5078a5;
    }
    
    .content {
        height: 100%;
        background-color: #999999;
    }
    

    Here is a working jsfiddle

    Note: I have no idea what the browser compatability is for this. I was just playing around with alternate solutions and this seemed to work well.

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