最近蛮多面试题都有相关的等高问题,所以我就试得写了下。希望对大家有用。
1.边距实现
<!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>等高边距实现(www.hemin.cn)</title> <style type="text/css"> *{ margin:0; padding:0;} body{ margin:10px;} .top,.midst,.bottom{width:600px;} .left,.right{float:left; padding-bottom: 32767px;margin-bottom:-32767px;} .top{ height:100px; margin-bottom:10px;background:#808000;} .midst{ overflow:hidden;zoom:1;} .left{ width:200px; display:inline; background:#f00;} .right{width:390px; margin-left:10px; display:inline; background:#008000;} .bottom{ margin-top:10px; height:100px; background:#A65300;} </style> </head> <body> <div class="top">头部</div> <div class="midst"> <div class="left">左边<br /><br /><br /><br /><br /><br /><br /></div> <div class="right">右边</div> </div> <div class="bottom">尾部</div> </body> </html>
提示:你可以先修改部分代码再运行。
2.背景图实现
<!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>等高(背景图)实现(www.hemin.cn)</title> <style type="text/css"> *{ margin:0; padding:0;} body{ margin:10px;} .top,.midst,.bottom{width:600px;} .left,.right{float:left;padding-bottom: 32767px;margin-bottom:-32767px;} .top{ height:100px; margin-bottom:10px;background:#808000;} .midst{ overflow:hidden; background:url(http://www.hemin.cn/blog/wp-content/uploads/2009/06/dg_bg.gif) repeat-y right top;} .left{ width:200px; display:inline; background:#f00;} .right{width:390px; margin-left:10px; display:inline; background:#008000;} .bottom{ margin-top:10px; height:100px; background:#A65300;} </style> </head> <body> <div class="top">头部</div> <div class="midst"> <div class="left">左边<br /><br /><br /><br /><br /><br /><br /></div> <div class="right">右边</div> </div> <div class="bottom">尾部</div> </body> </html>
3.边框实现
<!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>等高(边框)实现(www.hemin.cn)</title> <style type="text/css"> *{ margin:0; padding:0;} body{ margin:10px;} .top,.midst,.bottom{width:600px;} .left,.right{float:left;} .top{ height:100px; margin-bottom:10px;background:#808000;} .midst{ overflow:hidden; border-right:390px solid #008000; width:210px; } .left{ width:200px; display:inline; background:#f00;} .right{width:390px; display:inline; position:absolute; left:220px;} .bottom{ margin-top:10px; height:100px; background:#A65300;} </style> </head> <body> <div class="top">头部</div> <div class="midst"> <div class="left">左边<br /><br /><br /><br /><br /><br /><br /></div> <div class="right">右边</div> </div> <div class="bottom">尾部</div> </body> </html>
4.套标签实现
<!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>等高(套标签)实现(www.hemin.cn)</title> <style type="text/css"> *{ margin:0; padding:0;} body{ margin:10px;} .top,.midst,.bottom{width:600px;} .warp{overflow:hidden;position:relative;width:600px;background:#008000;} .coll_left{float:left;left:50%;padding-bottom:15px;position:relative;width:50%;} .top{ height:100px; margin-bottom:10px;background:#808000;} .midst{ background:#FFFFFF;float:left;margin-left:-40px;position:relative;right:100%;width:141.6%;} .left{ margin:0 10px -32767px 215px;overflow:hidden;background:#f00;padding-bottom:32767px;} .right{float:right;left:390px;position:relative;width:390px;} .bottom{ margin-top:10px; height:100px;background:#A65300;} </style> </head> <body> <div class="top">头部</div> <div class="warp"> <div class="midst"> <div class="coll_left"> <div class="left">左边<br /><br /><br /><br /><br /><br /></div> </div> <div class="right">右边</div> </div> </div> <div class="bottom">尾部</div> </body> </html>
5.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=utf-8" /> <title>等高(JS)实现</title> <style type="text/css"> *{ margin:0; padding:0;} body{ margin:10px;} .top,.midst,.bottom{width:600px;} .left,.right{float:left; } .top{ height:100px; margin-bottom:10px;background:#808000;} .midst{ overflow:hidden;zoom:1;} .left{ width:200px; display:inline; background:#f00;} .right{width:390px; margin-left:10px; display:inline; background:#008000;} .bottom{ margin-top:10px; height:100px; background:#A65300;} </style> <script type="text/javascript"> function alignHeight(eleA,eleB){ if(!document.getElementById(eleA)){return false;} if(!document.getElementById(eleB)){return false;} var heightA = document.getElementById(eleA).clientHeight; var heightB = document.getElementById(eleB).clientHeight; if(heightA > heightB){ document.getElementById(eleB).style.height = heightA + "px"; document.getElementById(eleA).style.height = heightA + "px"; }else{ document.getElementById(eleA).style.height = heightB + "px"; document.getElementById(eleB).style.height = heightB + "px"; } } window.onload = function z_align(){ alignHeight("a","b"); //只需将需要对齐的两个模块的id写在此处即可。 //alignHeight(”AAA”,”BBB”) 可依此连续多组。 } </script> </head> <body> <div class="top">头部</div> <div class="midst"> <div id="a" class="left">左边<br /><br /><br /><br /><br /><br /><br /></div> <div id="b" class="right">右边</div> </div> <div class="bottom">尾部</div> </body> </html>
此题:IE6不兼容。
JS那个.没有考虑到padding
[回复]
hemin 回复:十一月 5th, 2009 at 8:33 下午
@stri, 是否可以再详细点呢?
1 里面的 .midst{ overflow:hidden;zoom:1;} zoom属性是干什么用的啊 我设成0.5没什么效果哦
hemin 回复:十一月 6th, 2009 at 2:42 下午
@soit, 是专门为IE6清楚浮动的,为了自适应高度而已。而且zoom只有IE6才认。 不加zoom:1;只加overflow:hidden;其他更高版本浏览器都可以自适应高度。
soit 回复:十一月 6th, 2009 at 2:49 下午
@hemin, 哦 谢谢你的解答哦
hemin 回复:十一月 6th, 2009 at 3:04 下午
@soit, 不客气,再有问题可以继续提问。
第一种“边距实现”利用padding-bottom:10000px;margin-bottom:-10000px;的思想很巧妙。第二种的的背景图片感觉和第一种一样。第三种“边框实现”,没有考虑到右侧内容多的情况
第3种方法貌似在right里面设置内容的话就出问题哦
hemin 回复:十二月 8th, 2009 at 5:25 下午
@lw, 嘻嘻,这只是个方法。显然是可以解决的。
[...] 另外,以下这篇文章(http://www.hemin.cn/blog/?p=761)记录了五种实现等高的方法,权且作为备份放在这里。 [...]
Your comment
增大 | 减小输入框高度: + | –
名字&Name (required)*
邮箱&Mail (will not be published) (required)*
网址&Website
JS那个.没有考虑到padding
[回复]
hemin 回复:
十一月 5th, 2009 at 8:33 下午
@stri, 是否可以再详细点呢?
[回复]
1 里面的 .midst{ overflow:hidden;zoom:1;} zoom属性是干什么用的啊 我设成0.5没什么效果哦
[回复]
hemin 回复:
十一月 6th, 2009 at 2:42 下午
@soit, 是专门为IE6清楚浮动的,为了自适应高度而已。而且zoom只有IE6才认。
不加zoom:1;只加overflow:hidden;其他更高版本浏览器都可以自适应高度。
[回复]
soit 回复:
十一月 6th, 2009 at 2:49 下午
@hemin,
哦
谢谢你的解答哦
[回复]
hemin 回复:
十一月 6th, 2009 at 3:04 下午
@soit, 不客气,再有问题可以继续提问。
[回复]
第一种“边距实现”利用padding-bottom:10000px;margin-bottom:-10000px;的思想很巧妙。第二种的的背景图片感觉和第一种一样。第三种“边框实现”,没有考虑到右侧内容多的情况
[回复]
第3种方法貌似在right里面设置内容的话就出问题哦
[回复]
hemin 回复:
十二月 8th, 2009 at 5:25 下午
@lw, 嘻嘻,这只是个方法。显然是可以解决的。
[回复]
[...] 另外,以下这篇文章(http://www.hemin.cn/blog/?p=761)记录了五种实现等高的方法,权且作为备份放在这里。 [...]