Bootstrap 100% height with navbar

后端 未结 3 1863
不思量自难忘°
不思量自难忘° 2020-12-09 02:06

This is basically what I want to achieve:

\"Example\"

I want the total page to be 100% height, but when

相关标签:
3条回答
  • 2020-12-09 02:46

    Probably because it is adding the default margin from the navbar. Try this:

    .navbar {
        height: 40px;
        position: relative;
        margin: 0;
        padding: 0;
    }
    

    Also, try changing the min to max height:

    max-height: 100% !important;
    
    0 讨论(0)
  • 2020-12-09 02:57

    You'll have to subtract the height of the navbar from the 100%. There are several solutions, many of which will include JavaScript but you won't need that.

    1: box-sizing

    Give the navbar an absolute position. Then you have the issue of the content of the other elements disappearing below it. Then comes the next trick: add a top-padding the size of the navbar. And the last trick: add box-sizing: border-box; to the .sidebar and .body. For better browser support you also need -moz- and -webkit- prefixes like so: -moz-box-sizing:

    Example: Fiddle to box-sizing

    2: Actually subtract.

    use .body, .sidebar{ height: calc(100% - 40px); Browser support is very minimal for this from what I know less than for box-sizing, so I would recommend the first solution. Calc explained on css-tricks

    3: Flexbox(added anno 2015)

    You should now probably go with using calc when you know the height but an awesome replacement for using tables is flexbox. This is really my saviour for complex designs in responsive websites. By using one of the best features - flex-shrink: 0 - on the header you can force other elements into adjusting themselves to fill the rest of the container.

    An old answer is probably not the best place to write down an extensive guide on flexbox so, again, a great link to css-tricks

    0 讨论(0)
  • 2020-12-09 03:07

    I change a litle your code by adding a container around your side bar and body class:

    Here is the RESULT

    Css:

    body, html, .container-fluid, .sidebar, .body {
        height: 100%;
        min-height: 100%;
    }
    
    #container{
        width:100%;
        height:100%;
    }
    
    
    .sidebar{
        background-color: green;
        width:10%;
        float:left;
        height:100%;
    
    }
    
    .body{
        background-color: orange;
        float:left;
        width:90%;
        height:100%;
    
    }
    
    .navbar {
        height: 40px;
        position: relative;
        background-color: yellow;
        width:100%;
    }
    

    HTML

    <div class="container-fluid">
        <div class="navbar">
            navbar
        </div>
        <div id="container">
            <div class="sidebar">
                sidebar
            </div>
            <div class="body">
                body
            </div>
          </div>
    </div>
    
    </body>
    
    0 讨论(0)
提交回复
热议问题