首页 文章

行flexbox里面的嵌套列flexbox包装

提问于
浏览
6

我有一个带有嵌套弹性框的页面:http://jsfiddle.net/fr0/6dqLh30d/

<div class="flex-group">
  <ul class="flex-container red">
    <li class="flex-item">1</li>
    <li class="flex-item">2</li>
    <li class="flex-item">3</li>
  </ul>

  <ul class="flex-container gold">
    <li class="flex-item">1</li>
    <li class="flex-item">2</li>
    <li class="flex-item">3</li>
    <li class="flex-item">4</li>
    <li class="flex-item">5</li>
  </ul>

  <ul class="flex-container blue">
    <li class="flex-item">1</li>
    <li class="flex-item">2</li>
    <li class="flex-item">3</li>
    <li class="flex-item">4</li>
    <li class="flex-item">5</li>
    <li class="flex-item">6</li>
    <li class="flex-item">7</li>
    <li class="flex-item">8</li>
  </ul>
<div>

和(相关)CSS:

.flex-group {
   display: flex;
   flex-direction: row;
   height: 500px;
 }

 .flex-container {
   padding: 0;
   margin: 0;
   list-style: none;
   border: 1px solid silver;
   display: flex;
   flex-direction: column;
   flex-wrap: wrap;
   flex: 0 0 auto;
 }

 .flex-item {
   padding: 5px;
   width: 100px;
   height: 100px;
   margin: 10px;

   line-height: 100px;
   color: white;
   font-weight: bold;
   font-size: 2em;
   text-align: center;
 }

外部flexbox( .flex-group )用于从左到右布局 . 内部flexboxes( .flex-container )意味着从上到下布局,如果我的jsfiddle中没有't enough space (which there isn' t则应该换行 . 我想要发生的是,当包裹发生时, .flex-container 将在X方向上增长 . 但那并没有发生 . 相反,容器溢出 .

我希望它看起来像(在Chrome中):https://dl.dropboxusercontent.com/u/57880242/flex-good.png

有没有办法让 .flex-container 在X方向上适当调整大小?我不想对它们的宽度进行硬编码,因为这些列表中显示的项目将是动态的 .

2 回答

  • 2

    我现在已经玩了几分钟了,我想我已经找到了你想要的东西 .

    这是CSS

    .flex-group {
        margin: auto;
        display: flex;
        flex-direction: row;
        justify-content: space-around;
    }
    
    .flex-container {
        flex: 0 1 auto;
    
        display: flex;
        flex-flow: row wrap;
        align-items: space-around;
        align-content: flex-start;
    
        padding: 0;
        margin: 0;
        list-style: none;
        border: 1px solid silver;
    }
    
    .red li {
        background: red;
    }
    
    .gold li {
        background: gold;
    }
    
    .blue li {
        background: deepskyblue;
    }
    
    .flex-item {
        flex: 0 1 auto;
    
        padding: 5px;
        width: 100px;
        height: 100px;
        margin: 10px;
    
        line-height: 100px;
        color: white;
        font-weight: bold;
        font-size: 2em;
        text-align: center;
    }
    

    这是一个updated fiddle看到它在行动 .

  • 1

    有趣的是,我也玩弄它 . 我给了flex-container一个flex:1 100%;这使容器均匀分布到100%;块将在它们自己的容器空间中流动,并且无论您如何调整窗口大小,容器都保持相同的高度和重量 .

    .flex-container {
      flex: 0 100%;
      display: flex;
      flex-flow: row wrap;
      align-items: space-around;
      align-content: flex-start;
      padding: 0;
      margin: 0;
      list-style: none;
      border: 1px solid silver;
    }
    

    See fiddle here

相关问题