Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

html - Bootstrap 100% height with navbar

This is basically what I want to achieve:

Example

I want the total page to be 100% height, but when I put the sidebar and body at 100%, the page adds the 40px from the navbar, so I get a scrollbar even when there shouldn't be one.

I got it fixed by using tables, but I'm sure there must be an easier way

<body>
    <div class="container-fluid">
        <div class="navbar"></div>
        <div class="sidebar"></div>
        <div class="body"></div>
    </div>
</body>

and css what I've got so far:

body, html, .container-fluid, .sidebar, .body {
    height: 100%;
    min-height: 100%;
}
.navbar {
    height: 40px;
    position: relative;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...