【问题标题】:Make flex container with columns shrink-to-fit使具有列缩小以适应的 flex 容器
【发布时间】:2014-10-14 18:32:34
【问题描述】:

假设我们有一个容器,它的子元素必须在列中填充它。容器的高度是有限的,并且必须与所有后代的需要一样宽/窄才能适合。它应该是这样的:

为了做到这一点,我尝试flexbox。问题是:是否有可能使其缩小以适应?

  • float:left:在 Chrome 中根本无法解决问题,在 Firefox 中,容器只有一列的宽度 - example 1
  • position:absolute:对正常流量没有贡献,所以丢弃它。

现在我将浏览器范围缩小到 Chrome 和 FF。

HTML:

<div class="flexcontainer wrap column">
    <div></div>
    <div></div>
    ...
</div>

CSS:

.flexcontainer{
    display: flex;              /* make it a flexbox */
    flex-flow: column wrap;     /* make it a column container, and fill them one after another */

    align-content: stretch;
    align-items: center;
    justify-content: center;

    float: left;                /* shrink-to-fit */

    max-height: 450px;          /* limit the height */
    min-height: 300px;
}
.flexcontainer > div {
    height: 100px;            
    margin: 3px;
    width: 100px;               /* set the width of descendants */
}

谢谢!

【问题讨论】:

标签: css flexbox shrink


【解决方案1】:

Javascript 解决方案:

$(document).ready(function() {
 $('.flexcontainer').each(function( index ) {
     var parentHeight = $(this).outerHeight();
     var childHeight = $(this).children().outerHeight();
     var childCount = $(this).children().length;
     var cols = Math.ceil(childHeight*childCount/parentHeight);

     var childWidth = $(this).children().outerWidth();
     var padding = 15;
     $(this).width(childWidth*cols+padding);
    })
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 2016-02-07
    • 2020-06-26
    • 1970-01-01
    • 2021-11-18
    相关资源
    最近更新 更多