【问题标题】:Flex container with two columns; second column has four rows具有两列的 Flex 容器;第二列有四行
【发布时间】:2016-08-15 13:14:07
【问题描述】:

我很难在 flex 中显示以下布局。我有 5 个盒子,我想将我的容器分成两部分,一个盒子垂直显示,另外 4 个垂直显示。

这是我的 CSS:

.trades, .trade-panel {
    flex: 1;
 }
.layout-4-5 {
    flex-direction: column;
}
.layout-4-5 > div {
    width: 50%;
}

然后我将第四个或最后一个孩子的基数设置为 100%。

.layout-4-5 > div:nth-child(1) {
    flex-basis: 100%;
}

这是我的 HTML

<div class="trades layout-4-5">
  <!--trade-panel are my individual boxes --->
  <div class="trade-panel">
    </div>

</div>

在上面水平打印我的布局。考虑到我的 flex-directioncolumn 并且我的第一个孩子或盒子有 100% 的基础,那不应该打印我想要的吗?请提供任何帮助。

注意:由于盒子大小相同,包含其他四个盒子的列应该更长,只要它们在上面的排列中,就可以了。 tq

【问题讨论】:

  • 在一个容器中添加第一个 div,在另一个容器中添加其他四个。

标签: html css flexbox


【解决方案1】:

flex-container-1 {
    display: flex;                   /* establish flex container */
    flex-direction: row;             /* flex items will align horizontally */
    justify-content: center;         /* center flex items horizontally */
    align-items: center;             /* center flex items vertically */
    
    /* for demo purposes only */
    height: 250px;
    width: 400px;
    border: 1px solid #777;
    background-color: lightgreen;
}

flex-container-1 > flex-item {
    height: 90%;
    flex: 0 0 45%;                   /* <flex-grow> <flex-shrink> <flex-basis> */
    margin-right: 8px;               /* a bit of space between the centered items */
    border: 1px dashed #333;
    background-color: yellow;
}

flex-container-2 {
    height: 90%;
    flex: 0 0 45%;
    display: flex;                   /* flex item is now also flex container */
    flex-direction: column;          /* items will stack vertically */
    justify-content: space-between;  /* align items vertically */
}

flex-container-2 > flex-item {
    flex: 0 0 22%;
    border: 1px dashed #333;
    background-color: yellow;
}
<flex-container-1><!-- main container -->

    <flex-item></flex-item><!-- flex item #1 (first column) -->
    
    <flex-container-2><!-- flex item #2 / nested flex container (second column) -->
    
        <flex-item></flex-item>

        <flex-item></flex-item>

        <flex-item></flex-item>

        <flex-item></flex-item>

    </flex-container-2><!-- close nested container -->
    
</flex-container-1><!-- close main container -->

【讨论】:

【解决方案2】:

我在这个问题上苦苦挣扎,然后偶然发现了解决这个问题的新方法正确,因为我决定放弃并使用浮点数。我终于能够在不使用单独的 DIV 列的情况下让它工作。

更新:我通过在 .items 上指定高度来简化我之前的版本。

  1. 提供非百分比widthheight.items
  2. .items 上使用flex-direction: column

CSS:

.items {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  width: 40em;
  height: 20em;
}

  .item:first-child {
    width: 20em;
    height: 20em;
    background-color: black;
  }

  .item:nth-child(2) {
    width: 20em;
    height: 5em;
    background-color: pink;
  }

  .item:nth-child(3) {
    width: 20em;
    height: 5em;
    background-color: blue;
  }

  .item:nth-child(4) {
    width: 20em;
    height: 5em;
    background-color: yellow;
  }

  .item:last-child {
    width: 20em;
    height: 5em;
    background-color: red;
  }

HTML:

<div class="items">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div><!-- .items -->

代码笔: https://codepen.io/anon/pen/ZXoqJJ

【讨论】:

  • 如果您有动态内容(可变高度内容),此解决方案将不起作用。
  • 没错,我也有同样的想法。谢谢!
【解决方案3】:

我对您的问题或代码并不完全清楚。但这里有一个通用的解决方案:

flex-container-1 {
    display: flex;                   /* establish flex container */
    flex-direction: row;             /* flex items will align horizontally */
    justify-content: center;         /* center flex items horizontally */
    align-items: center;             /* center flex items vertically */
    
    /* for demo purposes only */
    height: 250px;
    width: 400px;
    border: 1px solid #777;
    background-color: lightgreen;
}

flex-container-1 > flex-item {
    height: 90%;
    flex: 0 0 45%;                   /* <flex-grow> <flex-shrink> <flex-basis> */
    margin-right: 8px;               /* a bit of space between the centered items */
    border: 1px dashed #333;
    background-color: yellow;
}

flex-container-2 {
    height: 90%;
    flex: 0 0 45%;
    display: flex;                   /* flex item is now also flex container */
    flex-direction: column;          /* items will stack vertically */
    justify-content: space-between;  /* align items vertically */
}

flex-container-2 > flex-item {
    flex: 0 0 22%;
    border: 1px dashed #333;
    background-color: yellow;
}
<flex-container-1><!-- main container -->

    <flex-item></flex-item><!-- flex item #1 (first column) -->
    
    <flex-container-2><!-- flex item #2 / nested flex container (second column) -->
    
        <flex-item></flex-item>

        <flex-item></flex-item>

        <flex-item></flex-item>

        <flex-item></flex-item>

    </flex-container-2><!-- close nested container -->
    
</flex-container-1><!-- close main container -->

jsFiddle

【讨论】:

    【解决方案4】:

    我已经给出了边距和背景颜色属性,以便您更好地理解。

    <div id="wrapper">
        <div id="left">
           <div class="flex-harshad">
             <div class = "flex-harshad2">  
                Harshad
             </div></div>
        </div>
    
        <div id="right">
           <div class="flex-harshad">
            <div class="flex-item">world</div>
            <div class="flex-item">by</div>
            <div class="flex-item">Alan</div>
            <div class="flex-item">Dong</div>
          </div>
        </div>
      </div>
    

    这里是css。

    body,
    div {
      margin: 0;
      border: 0 none;
      padding: 0;
    }
    
    html,
    body,
    #wrapper,
    #left,
    #right {
      height: 100%;
      min-height: 100%;
    }
    
    #wrapper {
      margin: 0 auto;
      overflow: hidden; 
    }
    
    #left { 
      float: left; 
    }
    
    div.flex-harshad2{    
        margin : 5px;
        margin-top : 25px;   
        min-height: 91%;
        background : white ;
        width : 90px;        
    }
    
    div.flex-harshad{
        background: red;
        height : 100%;   
        width : 100px;
        text-align: center; 
        display : inline-block; 
        background :orange ;
        margin :10px;  
    }
    div.flex-item {
      background: white;
      margin: 5px;
      margin-top : 25%;
      min-height : 20%;; 
      /* remove text-lign will not center text itself */
      text-align: center;   
    }
    

    这是输出:

    【讨论】:

    • 该问题要求弹性解决方案。这是一个浮动解决方案。
    • @Michael_B 有时人们想要的和人们要求的完全不同。
    猜你喜欢
    • 2019-03-31
    • 2021-09-02
    • 2020-06-26
    • 2020-04-21
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2014-10-14
    相关资源
    最近更新 更多