2列布局,第一列固定宽度,100%高度,第二列100%宽度和高度

时间:2013-12-19 11:48:12

标签: html css jquery-masonry

我已设法用display:table-cell完成此操作,但我遇到了一个问题,将 masonry.js 实施到table-cell,它只是不起作用

所以我必须使用普通的div进行此操作。

我需要左列为40px宽,以及随内容增长的高度。主列需要为100%宽度或动态占用剩余空间。

现在我有了这个,它在结构上有效,但与 masonry.js 无关。
CSS

div#wrapper{
display:table; 
height:100%; 
width:100%;
}
div#sidebarWrapper{
display:table-cell; 
min-width:40px; 
height:100%; 
background-color:#444;
}
div#contentWrapper{
display:table-cell; 
height:100%; 
position:relative;
}
div#content{
border-left:1px solid #ddd; 
border-top:1px solid #ddd; 
overflow:hidden; 
padding-bottom:100px; 
margin-top:195px;
}
div.clear{clear:both;} 

HTML

  <div id="wrapper">
        <div id="sidebarWrapper">
          </div> <!-- sidebar wrapper -->

          <div id="contentWrapper">
            <div id="content">
            </div><!-- content-->
            <div class="clear"></div>
          </div><!-- wrapper wrapper -->
      </div><!-- site wrapper -->

基本上我需要无表格的版本。非常感谢。

1 个答案:

答案 0 :(得分:1)

我认为你需要第二列是流动的......

Demo(做了另一个演示,子元素不需要overflow: hidden;

我想你不需要任何解释,因为一切都是自我解释的,从你孩子不需要的子元素中移除overflow: hidden;

<div class="wrapper">
    <div class="left_container">Hello</div>
     <div class="right_container">World this is just a random junk text</div>
</div>

html, body {
    height: 100%;
}

.wrapper {
   height: 100%;
}

.left_container {
    float: left;
    width: 200px;
    background: #f00;
    min-height: 100%;
}

.right_container {
    min-height: 100%;
    background: #000;
    margin-left: 200px;
    color: #f00;
}

注意:如果右列超出了视口height,则不会展开左列,在这种情况下,我们通常使用display: table;作为纯CSS解决方案,但您已经在这样你可以选择jQuery ..或者你可以在你认为可能超出视口overflow-y: auto;的容器上使用height

$(function(){
    $(window).resize(function(){
        var documentHeight = $(document).height();
        $('.left_container').css('height',documentHeight + 'px');
    }).resize();    
});

Demo 2(含内容)

编辑:Mr.Alien有正确的想法我只是做了一些调整:)

jQuery(function($){
    $(window).resize(function(){
        var containerHeight = $('.right_container').height();
        $('.left_container').css('height',containerHeight + 'px');
    }).resize();    
});