纯CSS解决方案,具有相同的列高度和堆叠的div

时间:2012-03-16 12:46:16

标签: css height dynamic-columns

我重新创建了我的问题here

问题是'left'div应该匹配'top'和bottom'right divs的组合高度。我一直在阅读有关如何使用不同内容的列实现相同高度的文章和教程,但它们似乎都不适用于“堆叠”列。

我目前的非工作解决方案建立在this文章

的概念之上
  

使div的高度等于最高列的唯一方法   如果该div包含所有列。所以解释另一个   通过将列放在容器中,我们将导致容器   成为最高柱的高度。这是非常有用的   结构体。为此结构在所有浏览器中正常工作   容器div必须浮动(左或右)加上每列   内容div也必须浮动,无论哪种方式都无关紧要。

我知道equalHeights jQuery插件并调整大小,但我更喜欢CSS解决方案。

HTML:

<!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>
    </head>
    <body>
      <div id='container'>  
        <div id='height-container'>
            <div id='left'>
                  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque faucibus, nunc non semper fringilla, nibh mauris aliquam diam, sit amet suscipit purus ligula eu odio. Nulla tortor neque, sodales nec placerat vitae, laoreet a mi. In ac ullamcorper dui. Sed enim tellus, volutpat at tristique eu, condimentum volutpat tellus. Integer massa quam, egestas id vulputate id, ultrices ut felis. 
            </div>
            <div id='right'>
              <div id='top'>
                  Proin condimentum purus tortor, eu laoreet velit. Praesent ornare, mauris eu laoreet suscipit, nibh mauris imperdiet dolor, ut molestie erat tellus ac odio. Etiam tempor eros at neque tincidunt a feugiat massa imperdiet. Vestibulum aliquet nibh sit amet urna facilisis condimentum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In ornare dolor quis neque aliquet lacinia et id tortor. Fusce eu sapien non mi aliquet condimentum id vitae ante. Phasellus sit amet eros vitae velit fringilla facilisis non at eros. In non venenatis lectus. Nam tortor tellus, vehicula non vulputate vitae, mollis a velit. Pellentesque gravida pretium ante, eu volutpat risus viverra non. Duis purus quam, venenatis nec consectetur interdum, aliquam rutrum diam. Donec interdum odio eget ante consectetur in sodales mi molestie. Nunc bibendum, turpis vel vulputate accumsan, diam tortor placerat sem, sed posuere risus purus eu massa. Nullam eleifend pulvinar massa, nec cursus velit dictum quis. 
              </div>
              <div id='bottom'>
                   Praesent vel porttitor dolor. Nulla vel nisl quis nibh bibendum rutrum. In gravida lacus a tellus tempus at pretium quam iaculis. Quisque fringilla feugiat urna, ac tincidunt tellus ornare et. Aenean sit amet turpis at ante molestie accumsan. Duis vestibulum, tortor nec aliquam tincidunt, leo urna tincidunt nunc, et laoreet metus risus quis erat. Proin at sem leo, sit amet bibendum quam. Vivamus quam urna, pulvinar vel tempor eget, facilisis tristique ipsum. Aenean sed ipsum et odio convallis congue a sit amet leo. Morbi luctus odio a felis pellentesque sit amet cursus ante consectetur. <br><br>
                  Praesent vel porttitor dolor. Nulla vel nisl quis nibh bibendum rutrum. In gravida lacus a tellus tempus at pretium quam iaculis. Quisque fringilla feugiat urna, ac tincidunt tellus ornare et. Aenean sit amet turpis at ante molestie accumsan. Duis vestibulum, tortor nec aliquam tincidunt, leo urna tincidunt nunc, et laoreet metus risus quis erat. Proin at sem leo, sit amet bibendum quam. Vivamus quam urna, pulvinar vel tempor eget, facilisis tristique ipsum. Aenean sed ipsum et odio convallis congue a sit amet leo. Morbi luctus odio a felis pellentesque sit amet cursus ante consectetur.
              </div>
            </div>
        </div> 
    </div>        
    </body>
</html>    

CSS:

body{
    text-align:justify;
}

#container{
    width:1000px;
    margin:0 auto;
}

#height-container{
    background:orange;
    float:left;
}

#left{
    float:left;
    width:200px;
    background:#CD5555;
    padding:20px;
}

#right{
    width:600px;
    float:right;
}

#top{
    background:yellow;
    padding:20px;
}

#bottom{
    background:#00FF66;    
    padding:20px;
}

2 个答案:

答案 0 :(得分:2)

您可以使用display:table属性:

#height-container{
    background:orange;
    display: table;
    vertical-align:top;
}

#left{
    display: table-cell;
    width:200px;
    background:#CD5555;
    padding:20px;
}

#right{
    width:600px;
    display: table-cell;
}

选中此http://jsfiddle.net/6rBAw/2/

但它的工作直到IE8&amp;上述

<强>已更新

<强> CSS

#left{
    width:200px;
    background:#CD5555;
}

#right{
    float:right;
    width:600px;
}
.clr{clear:both;}

<强> HTML

<div id='right'></div>
<div id='left'>
  <div class="clr"></div>
</div>

检查http://jsfiddle.net/6rBAw/4/

答案 1 :(得分:2)

这是一个在IE 7中工作的解决方案http://jsfiddle.net/6rBAw/6/此解决方案利用负边距底部和填充底部,唯一的是你必须给边距底部和填充底部值相同而且大值并给列溢出隐藏属性的容器。

#container{
    width:1000px;
    margin:0 auto;
    **overflow: hidden;**
}

#left{
    float:left;
    width:200px;
    background:#CD5555;
    **padding:20px 20px 99999px 20px;
    margin-bottom: -99999px;**
}

#right{
    width:600px;
    float:right;
    **padding-bottom: 99999px;
    margin-bottom: -99999px;**
    background-color: black;
    height: 1000px;
}