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

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

问题描述

我想创建 3 个 div,使整个页面与窗口的大小相同(即页面不应滚动),并且每个 div 的高度应为整个高度的某个百分比.例如

|--------------------------------------||体高=窗口的高度|||-----------------------------------||||标题高度:10% ||||-----------------------------------||||||||内容高度:85% ||||||||||||-----------------------------------||||页脚高度:5 % ||||-----------------------------------|||--------------------------------------|

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

<div style="width: 100%; height:100%;"><div id="headerDiv" style="width: 100%; height:10%; background-color: #ff0000"><!-- 一些内容-->

<div style="宽度:100%;高度:85%;"><!-- 一些内容-->

<div style="width: 100%; height:5%" ><!-- 一些内容-->

解决方案

你需要这样的东西吗?

演示

html, body {高度:100%;}div:nth-of-type(1) {背景:#f00;高度:20%;}div:nth-of-type(2) {背景:#00f;高度:70%;}div:nth-of-type(3) {背景:#0f0;高度:10%;}

我猜您的解决方案也可以使用,但您一定错过了重置默认浏览器样式的机会,在你的 CSS 中使用它,你也错过了为 html 元素设置 height: 100%;

* {边距:0;填充:0;}

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>

解决方案

Do you need something like this?

Demo

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%;
}

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 个部分,主体高度 = 窗口高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆