- CSS布局自适应高度解决方法
- 发布时间:2007-09-22 08:34:25 浏览数:8853 发布者:sunhw0725 设置字体【大 中 小】
这是一个比较典型的三行二列布局,每列高度(事先并不能确定哪列的高度)的相同,是每个设计师追求的目标,按一般的做法,大多采用背景图填充、加JS脚本的方法使列的高度相同,本文要介绍的是采用容器溢出部分隐藏和列的负底边界和正的内补丁相结合的方法来解决列高度相同的问题。
先看代码:#wrap{
overflow: hidden;
}
#sideleft, #sideright{
padding-bottom: 32767px;
margin-bottom: -32767px;
}实现原理:
块元素必须包含在一个容器里。
应用overflow: hidden 到容器里的元素。
应用 padding-bottom(足够大的值)到列的块元素 。
应用margin-bottom(足够大的值)到列的块元素。
padding-bottom将列拉长变的一样高,而负的margin-bottom又使其回到底部开始的位置,同时,溢出部分隐藏掉了。
兼容各浏览器
IE Mac 5
得到高度正确,所以要过滤掉上面的代码。/*\*/
#sideleft, #sideright{
padding-bottom: 32767px;
margin-bottom: -32767px;
}
/**/ Opera
1. Opera7.0-7.2不能正确清除溢出部分,所以要加:/* easy clearing */
#wrap:after
{
content: '[DO NOT LEAVE IT IS NOT REAL]';
display: block;
height: 0;
clear: both;
visibility: hidden;
}
#wrap
{
display: inline-block;
}
/*\*/
#wrap
{
display: block;
}
/* end easy clearing */
/*\*/2. Opera8处理overflow: hidden有个BUG,还得加上以下代码:/*\*/
#sideleft, #sideright
{
padding-bottom: 32767px !important;
margin-bottom: -32767px !important;
}
@media all and (min-width: 0px) {
#sideleft, #sideright
{
padding-bottom: 0 !important;
margin-bottom: 0 !important;
}
#sideleft:before, #sideright:before
{
content: '[DO NOT LEAVE IT IS NOT REAL]';
display: block;
background: inherit;
padding-top: 32767px !important;
margin-bottom: -32767px !important;
height: 0;
}
}
/**/3.Opera9的B2在修正8的bug.
测试环境:IE5.01、IE5.5、IE6.0、Firefox1.5、Opera8.5、Netscape 7.2通过。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Equal height(DIV+CSS布局中自适应高度的解决方法)</title>
<style type="text/css">
body{
padding: 0;
margin: 0;
font-size: 12px;
font-family: Arial, Helvetica, sans-serif;
line-height: 140%;
text-align: center;
background:#E7DFD3;
}
#wrap{
width: 750px;
margin: 0 auto;
overflow: hidden;
}
#header{
background: #E8E8E8;
}
#sideleft{
width: 580px;
float: left;
background: #FFF;
text-align: left;
}
#sideright{
width: 170px;
float: left;
background: #F0F0F0;
text-align: left;
}
/* easy clearing */
#wrap:after
{
content: '[DO NOT LEAVE IT IS NOT REAL]';
display: block;
height: 0;
clear: both;
visibility: hidden;
}
#wrap
{
display: inline-block;
}
/*\*/
#wrap
{
display: block;
}
/* end easy clearing */
/*\*/
#sideleft, #sideright
{
padding-bottom: 32767px !important;
margin-bottom: -32767px !important;
}
@media all and (min-width: 0px) {
#sideleft, #sideright
{
padding-bottom: 0 !important;
margin-bottom: 0 !important;
}
#sideleft:before, #sideright:before
{
content: '[DO NOT LEAVE IT IS NOT REAL]';
display: block;
background: inherit;
padding-top: 32767px !important;
margin-bottom: -32767px !important;
height: 0;
}
}
/**/
#footer{
background: #E8E8E8;
width: 100%;
float: left;
}
h1,h2,address,p{
margin: 0;
padding: .8em;
}
h1,h2{font-size: 20px;}
</style>
</head>
<body>
<div id="wrap">
<div id="header">
<h1>Head</h1>
</div>
<div id="sideleft">
<h2>sideleft</h2>
<p> 要从固定的、基于像素的设计方法转到弹性的、相对的设计方法并不容易。但是如果恰当利用,就可以成为增强亲和力和易用性的一个自然选择,同时又无须做出设计上的牺牲。 </p>
<p> 像素是计算机屏幕上的不可缩放的点,而一个
h3 就是一个字大小的方块。由于字体大小的变化, h3
代表用户喜欢的文字大小的相对单位。 </p>
<p> 采用印刷式的固定设计方案或许要容易些,因为如果尺寸不变,则考虑的东西就相对较少。可是如果采用弹性的设计方法,就可以充分利用电脑的显示器和浏览器。 </p>
<p> 也许你想你的网站以某种特定的方式显示,但是你的用户想看到的可能不一样。任何强加于用户的东西都不利于易用性,从而对网站的成功造成损害。 </p>
<p> 要从固定的、基于像素的设计方法转到弹性的、相对的设计方法并不容易。但是如果恰当利用,就可以成为增强亲和力和易用性的一个自然选择,同时又无须做出设计上的牺牲。 </p>
<p> 像素是计算机屏幕上的不可缩放的点,而一个
h3 就是一个字大小的方块。由于字体大小的变化, h3
代表用户喜欢的文字大小的相对单位。 </p>
<p> 采用印刷式的固定设计方案或许要容易些,因为如果尺寸不变,则考虑的东西就相对较少。可是如果采用弹性的设计方法,就可以充分利用电脑的显示器和浏览器。 </p>
<p> 也许你想你的网站以某种特定的方式显示,但是你的用户想看到的可能不一样。任何强加于用户的东西都不利于易用性,从而对网站的成功造成损害。 </p>
</div>
<div id="sideright">
<h2>sideright</h2>
<p> 要从固定的、基于像素的设计方法转到弹性的、相对的设计方法并不容易。但是如果恰当利用,就可以成为增强亲和力和易用性的一个自然选择,同时又无须做出设计上的牺牲。 </p>
<p> 像素是计算机屏幕上的不可缩放的点,而一个
h3 就是一个字大小的方块。由于字体大小的变化, h3
代表用户喜欢的文字大小的相对单位。 </p>
<p> 采用印刷式的固定设计方案或许要容易些,因为如果尺寸不变,则考虑的东西就相对较少。可是如果采用弹性的设计方法,就可以充分利用电脑的显示器和浏览器。 </p>
<p> 也许你想你的网站以某种特定的方式显示,但是你的用户想看到的可能不一样。任何强加于用户的东西都不利于易用性,从而对网站的成功造成损害。 </p>
</div>
<div id="footer">
<address>
Footer
</address>
<p>制作:<a href="http://www.forest53.com">forestgan</a></p>
</div>
</div>
</body>
</html>
[ 本帖最后由 sunhw0725 于 2007-9-22 08:36 编辑 ]
#2楼发布者:sunhw0725时间:2007-09-22 09:16:04
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
</head>
<body style="height:100%; margin:0;">
<div style="width:100%; margin:0 auto; text-align:center; background:#FF9400; height:100px; position:absolute; top:0; line-height:100px;">head</div>
<div style="width:100%; margin:0 auto; background:#eee; height:100%;">
<div style="height:101px; background:#fff;"></div>
<div style="height:101px; background:#eee;"></div>
</div>
<div style="width:100%; margin:0 auto; text-align:center; background:#f00; height:100px; position:absolute; bottom:0; line-height:100px;">foot</div>
</body>
</html>
#3楼发布者:sunhw0725时间:2007-09-22 09:16:18
<html>
.<style type=text/css>
html,body{margin:0;height:100%}
.box_1 { position:absolute; left:0; width:100%; background:#f00; z-index:2;height:20%}
.t{top:0;}
.t1{top:20%;height:80%;background:#ffd2e9;}
.t2{top:80%;}
</style>
<body>
<div class="box_1 t">ccc</div>
<div class="box_1 t1">add</div>
<div class="box_1 t2">dd</div>
</body>
</html>
#4楼发布者:sunhw0725时间:2007-09-22 09:16:32
<html>
<head>
<title></title>
<style type="text/css">
body,html{
height: 100%;
padding: 0;
margin: 0;
}
#header,#footer{
height: 10%;
background: #EBF7F9;
}
#container{
min-height: 80%;
background: #FDF5E6;
}
/*\*/
* html #container{
height: 80%;
}
/**/
</style>
</head>
<body>
<div id="header">...</div>
<div id="container">
<div id="mainbg">
<div id="left">...</div>
<div id="right">...
Examples of engineered cells and assays which Dragonfly has experience in include:GFP and lac Z.
</div>
</div>
</div>
<div id="footer">...</div>
</body>
</html>
#5楼发布者:sunhw0725时间:2007-09-22 10:00:51
自适应高度的问题,采用 Div + CSS 进行三列或二列布局时,事先不知道具体高度,只能根据内容的增减自适应高度,要使两列(或三列)的高度相同,用 Table 很容易实现,但采用 Div + CSS 就显得比较麻烦了。按照一般的做法,大都采用背景图填充或 JS 脚本的方法使高度相同,但这些都不是最好的办法,我认为…
下面介绍采用“隐藏容器溢出”和“正内补丁”和“负外补丁”结合的方法
主要代码:
#wrap{overflow:hidden;} /*外容器*/
#sidebar_left,#sidebar_right{padding-bottom:100000px;margin-bottom:-100000px;} /*列*/
完整例子代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Div + CSS Example, Wayhome's Blog</title>
<style type="text/css">
<!--
#wrap{overflow:hidden;}
#sidebar_left,#sidebar_right{padding-bottom:100000px;margin-bottom:-100000px;}
-->
</style></head>
<body>
<div id="wrap" style="width:300px; background:#FFFF00;">
<div id="sidebar_left" style="float:left;width:100px; background:#FF0000;">Left</div>
<div id="sidebar_mid" style="float:left;width:100px; background:#666;">
Middle<br />
Middle<br />
Middle<br />
Middle<br />
Middle<br />
Middle<br />
Middle<br />
Middle<br />
Middle<br />
</div>
<div id="sidebar_right" style="float:right;width:100px; background:#0000FF;">Right</div>
</div>
</body>
</html>