2009-1-27 0:3:12
CSS高度自适应,CSS背景自动延伸(2)
这是转自网络的文章,原文地址如下:upload/2009/1/200901310056030657.gif) repeat-y 300px;}
#column1{ float:left; width:300px;} #column2{ float:right; width:476px;} .clear{ clear:both;} 复制代码 2 JS脚本法:
代码的原理基本就是:读取高度,判断高度,高度相等。
代码如下
- <!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=gb2312" />
- <title>练习文档01</title>
- <style type="text/css">
- <!--
- body{
- margin:0px;padding:0px;
- }
- .Div_top{
- width:100%;
- background:#93beff;
- }
- .Div_bottom{
- width:100%;
- background:#ff00fc;
- }
- .Div_Left{
- position:absolute;
- width:200px;
- background:#ff6000;
- left:0px;
- }
- .Div_Right{
- margin-left: 200px;
- background:#00c17b;
- }
- -->
- </style>
- <script>
- function SameH(leftDiv,rightDiv)
- {
- var a=document.getElementById(leftDiv);
- var b=document.getElementById(rightDiv);
- document.write(a.clientHeight+'--');
- document.write(b.clientHeight+'--');
- document.write(a.scrollHeight+'--');
- document.write(b.scrollHeight+'--');
- if (a.scrollHeight < b.scrollHeight)
- {
- a.style.height= b.scrollHeight+"px";
- }
- else
- {
- b.style.height= a.scrollHeight+"px";
- }
- }
- </script>
- </head>
- <body>
- <div class="Div_top">这是顶部的div</div>
- <div id="Div1" class="Div_Left">11111<br>222222<br>33333333<br>444444444<br>555555555</div>
- <div id="Div2" class="Div_Right">22222<br>dsaf</div>
- <div class="Div_bottom">---------------------------------这是底部的DIV,在左中右中DIV 不等高的情况下会跑上去重叠了<br>----------------------------------------我现在想用一个 JavaScript代码来令左右两半的Div等高<br><br>(注意:中左中右两个DIV的高度是无法事先确定 的)</div>
- <script>
- SameH("Div1","Div2");
- </script>
- </body>
复制代码3 容器溢出部分隐藏和列的负底边界和正的内补丁相结合的方法
原作者:Alex Robinson
原文标题:Equal Height Columns
这是一个比较典型的三行二列布局,每列高度(事先并不能确定哪列的高度)的相同,是每个设计师追求的目标,按一般的做法,大多采用背景图填充、加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通过。
最终效果:
以上文章还可参考:CSS高度自适应,CSS背景自动延伸