简体   繁体   English

在2列布局中匹配列高

[英]Matching column height in 2column layout

My website uses 2 columns, but the only way I've been able to get the columns height to match is to use a fixed height, but this presents a "scrollbar in a scrollbar" issue where the content column has to have overflow: auto; 我的网站使用2列,但是我能够使列的高度与之匹配的唯一方法是使用固定的高度,但这出现了“滚动条中的滚动条”问题,其中内容列必须overflow: auto; for all the content to be seen, but if the user's browser doesn't make the entire page visible at once, both the page and the content column have scrollbars. 可以看到所有内容,但是如果用户的浏览器无法一次显示整个页面,则页面和内容列都具有滚动条。

What I would like to do is match the sidebar columns height to that of the content column. 我想做的是将侧边栏列的高度与内容列的高度匹配。 I was thinking of setting some javascript on page load to do it, but I can't help thinking theres a better way. 我当时想在页面加载时设置一些javascript来做到这一点,但我不禁想到还有更好的方法。

The site in question is http://www.pcbuddies.co.za (for reference). 有问题的网站是http://www.pcbuddies.co.za (供参考)。

Any suggestions would be greatly appreciated. 任何建议将不胜感激。 Thanks in advance! 提前致谢!

EDIT 1 编辑1
After applying the JQuery solution below, I'm happy with the result (mostly). 在应用以下JQuery解决方案后,我对结果感到满意(大部分情况)。 Where I do have a problem is when the first section (sidebar) of every page (navbar) is smaller than another section see http://www.pcbuddies.co.za/Services/Default.aspx . 我确实遇到的问题是,每页(导航栏)的第一部分(边栏)小于另一部分时, see http://www.pcbuddies.co.za/Services/Default.aspx

In this situation, the content is overflowing past the site's footer. 在这种情况下,内容溢出网站的页脚。

I wrote out a solution but I was paraphrasing a better example at this site here , which I find works very well. 我写了一个解决方案,但是我在这里解释这个更好的例子,我发现它很好用。 It uses a trick to create the equal height columns but works very well - without any javascript. 它使用技巧来创建等高的列,但效果很好-无需任何JavaScript。

Here's an example of it in action: example 下面是它在行动的例子: 例如

I always catch grief for suggesting this, but I've found the best, most dependable way of doing this is to utilize Javascript (in this case, jQuery) to make all of the columns the same height as the tallest column. 我总是为提出这个建议而感到悲伤,但是我发现最好,最可靠的方法是利用Javascript(在本例中为jQuery)使所有列的高度与最高列的高度相同。 See my live example. 请参阅我的现场示例。

Live Demo http://jsfiddle.net/T9VUc/1/ 现场演示 http://jsfiddle.net/T9VUc/1/

If you want to do this on the page load, try this. 如果要在页面加载时执行此操作,请尝试此操作。 Keep in mind, this procedure uses jQuery , so you will need to include that in your page 请记住,此过程使用jQuery ,因此您需要将其包括在页面中

var tallest=0;

$(document).ready(function() {
    $('.col').each(function(){
        if($(this).height() > tallest)
           tallest = $(this).height();
    });
    $('.col').css('height', tallest + 'px');
});

Live Demo on Page Load http://jsfiddle.net/T9VUc/2/ 页面加载实时演示 http://jsfiddle.net/T9VUc/2/

UPDATE 更新

Based on the URL you gave me, I suggest adding <div style='clear:both'></div> to the end of your 2nd div like this ... 根据您提供给我的网址,我建议像这样将<div style='clear:both'></div>到第二个div的末尾...

<div id="Side" class="col">
    ...
</div>
<div class="content col">
    ...
    <div id="network" style="display: none;">
        ...
    </div>

    <div style='clear:both'></div>
</div>

The other solutions look a bit too complicated to me. 其他解决方案对我来说似乎太复杂了。 How about this: 这个怎么样:

Set both of your columns to transparent background and make a container for both of them with the desired background as alpha-transparent png. 将两个列都设置为透明背景,并为两个列创建一个容器,并以所需的背景作为alpha透明png。

Maybe not the "cleanest" solution, but definitely a simple one. 也许不是“最干净”的解决方案,但绝对是一个简单的解决方案。 Looking at the website you linked, that's what I'd go with. 查看您链接的网站,这就是我想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM