【转】CSS实现div的高度填满剩余空间

转自:http://www.cnblogs.com/zhujl/archive/2012/03/20/2408976.html

高度自适应问题,我很抵触用js去解决,因为不好维护,也不够自然,但是纯用CSS,难度不小,比如下面我要说的例子。

需求:

1. 这个矩形的高度和浏览器窗口的高度相同,不能出现纵向滚动条

2. 绿色部分高度固定,比如50px

3. 紫色部分填充剩余的高度

HTML结构暂且如下:

<div id="main">

    <div id="nav">nav</div>

    <div id="content">content</div>

</div>

先看1.

html, body {

    height: 100%;

    margin: 0px;

    padding: 0px;

}

#main {

    background-color: #999;

    height: 100%;

}

需求2 也很容易:

#nav {

    background-color: #85d989;

    height: 50px;

}

需求3 是最让人头痛的,一般我们都会想到height:100%, 但是100%是以父元素的高度为准的,比如父元素的高度是300px,#nav占去了50px,#content理应是250px,但是写成height: 100%,结果就是#content的高度也变成了300%,出现了需求不允许的纵向滚动条。

当然,用js解决这种问题是相当简单的,但是这次我就是不想用js,下面就来试吧:

这个需求真的让我非常崩溃,看似简单,换了n种方式都觉得不靠谱,最后找到一种最接近理想效果的方法,如下

html, body {

    height: 100%;

    margin: 0px;

    padding: 0px;

}

#main {

    background-color: #999;

    height: 100%;

}

#nav {

    background-color: #85d989;

    width: 100%;

    height: 50px;

    float: left;

}

#content {

    background-color: #cc85d9;

    height:100%;

}

这里利用了浮动,最后的结果仅仅是看着没问题,当然了,如果你只是简单的展示文本和图片,这种方法已经够用了,但是如果你想用js做点交互,比如#content内部有个需要拖拽的元素,它的top最小值肯定不能是0,否则就被#nav挡住了,悲剧的是我就有这种需求,于是继续苦逼的试。

经过一天的尝试,加上高人指点,终于有解了,泪奔啊

#nav {

    background-color: #85d989;

    width: 100%;

    height: 50px;

}

#content {

    background-color: #cc85d9;

    width: 100%;

    position: absolute;

    top: 50px;

    bottom: 0px;

    left: 0px;

}

重点是要top和bottom一起使用,这是很反常规的用法,可以强制定义盒模型的区域,神奇啊

地图窗口常会遇到类似问题

时间: 2024-08-08 09:50:37

【转】CSS实现div的高度填满剩余空间的相关文章

CSS实现div的高度填满剩余空间

需求: 1. 这个矩形的高度和浏览器窗口的高度相同,不能出现纵向滚动条 2. 绿色部分高度固定,比如50px 3. 紫色部分填充剩余的高度 HTML结构: <div id="main"> <div id="nav">nav</div> <div id="content">content</div> </div> 需求1实现: html, body { height: 100%

css background-size与背景图片填满div

background-size与背景图片填满div 在开发中,常有需要将一张图片作为一个div的背景图片充满div的需求 background-size的取值及解释 background-size共有三种属性,分别为 background-size: cover MDN文档解释说明:缩放背景图片以完全覆盖背景区,可能背景图片部分看不见.A keyword that is the inverse of contain. Scales the image as large as possible a

CSS左右固定,中间填满布局

先上个高清无码图: 源码: <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> <link href="reset.css" rel="stylesheet&

css 让div 的高度和屏幕的高度一样

<html><head><title>无标题文档</title><style type="text/css">html,body{height:100%; width:100%; overflow:hidden; margin:0;padding:0;}.main{width:100%; height:100%; overflow:hidden; margin:0;padding:0;}.a1{width:100%; heig

div height 自适应高度 占满剩余高度的方法

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> html, body { margin: 0; padding: 0; height: 100%; } .if-top { display: block; border: none; width: 10

左边DIV固定宽度,右边的DIV如何自适应填满剩下的屏幕宽度

<style> .main{ margin-top: -20px; width: 100%; height: auto; overflow: hidden; } .main .left{ float: left; width: 300px; height: 700px; border-right: 1px solid #DDDDDD; } .main .right{ margin-left: 300px; height: 700px; background-color: red; } <

postfix导致maillog填满磁盘空间的巨坑!

双休日回家pull在公司修改的代码...于是菜鸟的linux探索之路开始了 1.df -f发现磁盘又占满了(之前是node的error) 2.发现maillog整整10个G,无数条(Jul 7 04:08:15 tangxu postfix/error[4318]: B1612103F04: to=[email protected], relay=none, delay=361517, delays=361504/14/0/0, dsn=4.4.3, status=deferred (deliv

自动填满窗口(宽度和高度都要填满)

要求 :A的高度是100px,宽度填满整个窗口,B,C的宽度分别是200px,100px,高度填满剩下的窗口,D的宽度和高度填满剩下的窗口. 我的想法:当时拿到这个题目的时候,宽度填满很简单,高度用自适应和百分之百都不行,就想到用js的方法,获取网页可用区域的宽度和高度  然后在相减就可以了,这个方法确实可以,不过昨晚回来查了下,其实用css方法也是可以的,之前自学的时候也学习过,不过没有用到实际的项目中 就忘记了,现在遇到这样的问题反而不会了.其实就是运用position:absolute的属

弹性布局之定宽剩下填满

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> #container{ display: flex; width: 500px; border: 1px solid red; } #box1{ background-color: mediumspr