在两列页面上,如何使用CSS或者Javascript将左div增长到右div的相同高度?
我正在尝试使用基于div的布局制作两列的页面(请不要使用表格!)。问题是,我不能将左div的长度与右div的高度匹配。我右边的div通常有很多内容。
这是我的模板的配对示例,以说明问题。
<div style="float:left; width: 150px; border: 1px solid;"> <ul> <li>nav1</li> <li>nav2</li> <li>nav3</li> <li>nav4</li> </ul> </div> <div style="float:left; width: 250px"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna .... </div>
替代文字http://involution.com/images/divlayout.png
解决方案
回答
最简单的答案在于下一个版本的css(3),目前尚不支持浏览器。
目前,我们只能使用javascript计算高度并将其设置在左侧。
如果导航是如此重要,以至于不能以这种方式定位,请沿着顶部运行。
我们也可以通过将边框移到容器和较大的内部容器上并使其看起来相同来做视觉上的欺骗。
这使它看起来相同,但事实并非如此。
<div style="border-left:solid 1px black;border-bottom:solid 1px black;"> <div style="float:left; width: 150px; border-top: 1px solid;"> <ul> <li>nav1</li> <li>nav2</li> <li>nav3</li> <li>nav4</li> </ul> </div> <div style="float:left; width: 250px; border:solid 1px black;border-bottom:0;"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna Lorem ipsum dolor sit amet, consectetur adipisicing elit, ... </div> <div style="clear:both;" ></div> </div>
回答
@hoyhoy
如果设计人员可以使用html进行这项工作,那么他可以进行此设计。如果他是一位真正的网页设计大师,他将意识到这是媒体的局限性,因为杂志广告中无法播放视频。
如果他想通过赋予两列相等的重要性来模拟粗细,则可以更改边框,以使它们看起来具有相同的粗细,并使边框的颜色与各列的字体颜色形成对比。
但是,要使物理元素具有相同的高度,此时只能使用表结构或者设置高度。为了模拟它们看起来相同的大小,它们不必具有相同的大小。
回答
我们可以在jQuery中非常简单地完成此操作,但是我不确定应该将JS用于此类情况。最好的方法是使用纯CSS做到这一点。
- 看一看人造柱甚至流体人造柱
- 另外一种技术(在漂亮的IE6上不起作用)是定位:相对于父容器。子容器(在情况下为导航列表)应放置在绝对位置,并被迫以'top:0;占据整个空间;底部:0;'
回答
这是CSS不能做到的那些完全合理,简单的事情之一。正如Silviu所建议的那样,Faux Columns是一个骇人但实用的解决方法。
如果有一天可以说的话,那会很可爱
div.foo { height: $(div.blah.height); }
回答
可以用CSS完成!别让别人告诉你。
最简单,最轻松的方法是使用"人造色谱柱"方法。
但是,如果该解决方案对我们不起作用,我们将需要继续阅读此技术。但请注意,这是一种CSS骇客,会让我们半夜醒来。
要点是,我们为该列的底部分配了大量填充,并为该列分配了负的页边距。然后,将列放置在设置了"溢出:隐藏"的容器中。或者多或者少的padding / margin值允许框继续扩展,直到到达包装器的末尾(由内容最多的列确定)为止,并且由于溢出而将padding产生的任何多余空间都切除了。我知道这没有多大意义。
<div id="wrapper"> <div id="col1">Content</div> <div id="col2">Longer Content</div> </div> #wrapper { overflow: hidden; } #col1, #col2 { padding-bottom: 9999px; margin-bottom: -9999px; }
请务必阅读我所链接的整篇文章,这里有许多警告和其他实现问题。这不是一个很漂亮的技术,但是效果很好。
回答
想一想,我从未在列的底部边框上做到这一点。它可能只是溢出而被切断。我们可能希望底部边框来自列内容一部分的单独元素。
无论如何,我知道这不是一个完美的魔术子弹解决方案。我们可能只需要使用它,或者解决它的缺点。
回答
这可以通过使用背景色的CSS来完成
<!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>Untitled Document</title> <style type="text/css"> * { margin: 0; padding: 0; } body { font-family: Arial, Helvetica, sans-serif; background: #87ceeb; font-size: 1.2em; } #container { width:100%; /* any width including 100% will work */ color: inherit; margin:0 auto; /* remove if 100% width */ background:#FFF; } #header { width: 100%; height: 160px; background: #1e90ff; } #content {/* use for left sidebar, menu etc. */ background: #99C; color: #000; float: right;/* float left for right sidebar */ margin: 0 0 0 -200px; /* adjust margin if borders added */ width: 100%; } #content .wrapper { background: #FFF; margin: 0 0 0 200px; overflow: hidden; padding: 10px; /* optional, feel free to remove */ } #sidebar { background: #99C; color: inherit; float: left; width: 180px; padding: 10px; } .clearer { height: 1px; font-size: -1px; clear: both; } /* content styles */ #header h1 { padding: 0 0 0 5px; } #menu p { font-size: 1em; font-weight: bold; padding: 5px 0 5px 5px; } #footer { clear: both; border-top: 1px solid #1e90ff; border-bottom: 10px solid #1e90ff; text-align: center; font-size: 50%; font-weight: bold; } #footer p { padding: 10px 0; } </style> </head> <body> <div id="container"> <!--header and menu content goes here --> <div id="header"> <h1>Header Goes Here</h1> </div> <div id="content"> <div class="wrapper"> <!--main page content goes here --> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis ligula lorem, consequat eget, tristique nec, auctor quis, purus. Vivamus ut sem. Fusce aliquam nunc vitae purus. Aenean viverra malesuada libero. </p> </div> </div> <div id="sidebar"> <!--sidebar content, menu goes here --> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis ligula lorem, consequat eget, tristique nec, auctor quis, purus.</p> </div> <div class="clearer"></div><!--clears footer from content--> <!--footer content goes here --> <div id="footer"> <p>Footer Info Here</p> </div> </div> </body> </html>
回答
还有一个基于Javascript的解决方案。如果我们使用jQuery,则可以使用以下插件。
<script type="text/javascript"> // plugin jQuery.fn.equalHeights=function() { var maxHeight=0; this.each(function(){ if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;} }); this.each(function(){ $(this).height(maxHeight + "px"); if (this.offsetHeight>maxHeight) { $(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px"); } }); }; // usage $(function() { $('.column1, .column2, .column3').equalHeights(); }); </script>
回答
我用它来对齐ID为" center"和" right"的2列:
var c = $("#center"); var cp = parseInt(c.css("padding-top"), 10) + parseInt(c.css("padding-bottom"), 10) + parseInt(c.css("borderTopWidth"), 10) + parseInt(c.css("borderBottomWidth"), 10); var r = $("#right"); var rp = parseInt(r.css("padding-top"), 10) + parseInt(r.css("padding-bottom"), 10) + parseInt(r.css("borderTopWidth"), 10) + parseInt(r.css("borderBottomWidth"), 10); if (c.outerHeight() < r.outerHeight()) { c.height(r.height () + rp - cp); } else { r.height(c.height () + cp - rp); }
希望能帮助到你。
回答
使用jQuery解决这个问题;只需在ready函数中调用此函数即可:
function setHeight(){ var height = $(document).height(); //optionally, subtract some from the height $("#leftDiv").css("height", height + "px"); }