2016-11-21 104 views
-1

是否可以让一排容器的Flexbox实现的总高度等于特定容器的高度?我现在基本的CSS实现是:将一排CSS Flexbox容器的高度设置为特定容器的高度

.flex-container { 
    display: flex; 
    flex-direction: row; 
    align-items: stretch; 
} 

具有以下HTML:

<div class="flex-container"> 
    <div class="left-container"> 
    stuff 
    </div> 
    <div class="right-container"> 
    some other stuff 
    </div> 
</div> 

的问题是,.right-container可以得到那座高度明智的。所以我想限制.flex-container.left-container的高度,无论它的最终高度如何(我不知道提前知道)。这样,.right-container结束了一个滚动条。

这是仅仅通过CSS这个可能,或者我需要Javascript吗?如果有帮助,我不一定非要在这里使用flexbox,但是除了这个高度问题,我发现它在我的应用程序中使用最方便。

+0

是否可以设置对他们'MAX-height'?只需设置'height:100%;' - >它永远不会超过您设置的'max-height'? – junkfoodjunkie

+0

我想过使用'max-height',但我真的不知道'。left-container'会占用多少空间。实际上它可以有很大的高度。我所知道的是,我从不希望那个容器有一个滚动条。我的'.right-container'可以更高,更高,这是我对约束的需要。如果我没有看到其他解决方案,那么我可能需要设置一些保守的'max-height'值。 – accelerate

+0

[CSS的可能重复:动态限制滚动高度](http://stackoverflow.com/questions/39545915/css-limit-the-height-from-scroll-dynamically) – kukkuz

回答

-1

如果在.left-container上设置了3个元素的高度并将overflow-y设置为scroll,那就应该这样做。

.flex-container { 
 
     display: flex; 
 
     flex-direction: row; 
 
     align-items: stretch; 
 
     border: solid 1px grey; 
 
     padding: 5px; 
 
    height: 150px; 
 
    } 
 

 
    .left-container { 
 
     border: solid 1px red; 
 
     flex: 1; 
 
     height: 150px; 
 
    } 
 

 
    .right-container { 
 
     border: solid 1px blue; 
 
     flex: 3; 
 
     height: 150px; 
 
     overflow-y: scroll; 
 
    }
<div class="flex-container"> 
 
     <div class="left-container"> 
 
     stuff 
 
     </div> 
 
     <div class="right-container"> 
 
     some other stuff some other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuffsome other stuff 
 
     </div> 
 
    </div>

0

.flex-container { 
 
    display: flex; 
 
    flex-direction: row; 
 
    align-items: stretch; 
 
    width:300px; 
 
    height:300px; 
 
    border:1px solid #000; 
 
} 
 

 
.left-container{ 
 
    background-color:red; 
 
    } 
 
.right-container{ 
 
    background-color:green; 
 
    }
<div class="flex-container"> 
 
    <div class="left-container"> 
 
    stuff 
 
    </div> 
 
    <div class="right-container"> 
 
    some other stuff 
 
    </div> 
 
</div>

相关问题