如何将百分比值分配给div以将整个页面分为3部分,其中body高度=窗口的高度 [英] How to assign percentage values to div to divide entire page into 3 parts with body height = window's height

查看:773
本文介绍了如何将百分比值分配给div以将整个页面分为3部分,其中body高度=窗口的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建3个div,以便整个页面都是窗口的大小(即页面不应该滚动),每个div应该具有整个高度的一些百分比的高度。
eg

I want to create 3 div such that the entire page is of the size of the window( i.e the page should not scroll) and each div should have height as of some percent of the entire height. e.g

|--------------------------------------|
|      Body height=window's height     |
| |----------------------------------| |
| |        Header height:10%         | |
| |----------------------------------| |
| |                                  | |
| |        Content height:85%        | |
| |                                  | |
| |                                  | |
| |----------------------------------| |
| |        Footer height:5 %         | |
| |----------------------------------| |
|--------------------------------------|

这里是我没有成功使用的代码

Here is the code which I used without any success

<body style="width: 100%; height:100%">
<div style="width: 100%; height:100%;">
    <div id="headerDiv" style="width: 100%; height:10%; background-color: #ff0000">
        <!-- some content -->
    </div>
    <div style="width: 100%; height:85%;"   >
        <!-- some content -->
    </div>
    <div style="width: 100%; height:5%"  >
        <!-- some content -->
    </div>
</div>
</body>


推荐答案

你需要这样的东西吗?

演示

html, body {
    height: 100%;
}

div:nth-of-type(1) {
    background: #f00;
    height: 20%;
}

div:nth-of-type(2) {
    background: #00f;
    height: 70%;
}

div:nth-of-type(3) {
    background: #0f0;
    height: 10%;
}

我想你的解决方案也会工作,但你必须错过了重置默认浏览器样式,在CSS中使用这个,你错过了设置 height:100%;

I guess your solution will also work, but you must have missed out resetting default browser styles, use this in your CSS and you've missed out setting height: 100%; for html element too

* {
   margin: 0;
   padding: 0;
}

这篇关于如何将百分比值分配给div以将整个页面分为3部分,其中body高度=窗口的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆