【问题标题】:In a flexbox grid, how do I prevent the first row from pushing down the second? [duplicate]在 flexbox 网格中,如何防止第一行向下推第二行? [复制]
【发布时间】:2021-02-10 14:19:06
【问题描述】:

我有一个看起来有点像这样的 flex 设计:

.columns {
  flex-wrap: wrap;
  display: flex;
}

.column {
  display: block;
}

.column.is-8 {
   flex: none;
   width: 66.66667%;
}

.column.is-4 {
    flex: none;
    width: 33.33333%;
}

.box-1 {
  background-color: red;
  height: 200px;
}
.box-2 {
  background-color: yellow;
    height: 400px;
}
.box-3 {
  background-color: blue;
    height: 800px;
}

*, *::before, *::after {
    box-sizing: inherit;
}
<div class="columns">
  <div class="column is-8 box-1">
      Box 1
  </div>
  <div class="column is-4 box-2">
      Box 2
  </div>
  <div class="column is-8 box-3">
      Box 3
  </div>
</div>
<!-- on desktop: box 1, 2 and 3 -->
<!-- on mobile: box 1, 2 and 3 -->

如何防止框 2 压下框 3?所以它看起来像这样:

更新

我想在移动设备上添加,盒子的顺序应该相同(例如 1、2 然后 3)

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    您需要稍微不同地设置模板,因为框 1 和 3 位于同一列中。您可以通过将框 1 和 3 包装在一个 div 中来实现所需的布局。

    .columns {
      display: flex;
      width: 100%;
    }
    
    .column {
      width: 100%;
    }
    
    .column.is-8 {
      width: 66.66667%;
    }
    
    .column.is-4 {
      width: 33.33333%;
    }
    
    .box-1 {
      background-color: red;
      height: 200px;
    }
    
    .box-2 {
      background-color: yellow;
      height: 400px;
    }
    
    
    .box-3 {
      background-color: blue;
      height: 800px;
    }
    
    *,
    *::before,
    *::after {
      box-sizing: inherit;
    }
      <div class="columns">
      <div class="column is-8">
        <div class="box-1">Box 1</div>
        <div class="box-3">Box 3</div>
      </div>
      <div class="column is-4">
        <div class="box-2">Box 2</div>
      </div>
    </div>

    【讨论】:

    • 嘿@Abdallah Ajjawi,感谢您的回答。这种方法的唯一问题是我希望移动设备上的顺序(框 1、2、3)相同。使用您的方法,移动设备上将是 1,3, 2
    【解决方案2】:

    这是gridauto-fit 的另一种可能性(有关设置,请参阅 CSS 中的注释)

    片段在整页中运行并调整大小以查看行为。

    .columns {
      display: grid;
      /*Below : 400px for minmax  is more than a third  of 1000px width of the container
      to draw at most 2 columns */
      grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
      width: 800px;
      margin: auto;
      max-width: 100%;
    }
    
    .column {
      border: solid;
    }
    
    .box-1 {
      background-color: red;
      height: 100px;
    }
    
    .box-2 {
      background-color: yellow;
      min-height: 100px;
      grid-row: span 2;
    }
    
    .box-3 {
      background-color: blue;
      height: 300px;
    }
    <div class="columns">
      <div class="column is-8 box-1">
        Box 1
      </div>
      <div class="column is-4 box-2">
        Box 2
      </div>
      <div class="column is-8 box-3">
        Box 3
      </div>
    </div>

    【讨论】:

      【解决方案3】:

      您可以尝试使用它,使用 grid 布局。它可以让您制作网格(惊喜、惊喜),并且可以让您更好地控制其中的所有内容。所以是这样的:

      .columns {
        display: grid;
        width: 100%;
        grid-gap: 0;
        grid-template-columns: 2fr 1fr;
      }
      
      .column {
        display: block;
      }
      
      .box-1 {
        background-color: red;
        height: 200px;
        grid-column: 1;
        grid-row: 1;
      }
      .box-2 {
        background-color: yellow;
        height: 400px;
        grid-column: 2;
        grid-row: 1 / 3;
      }
      .box-3 {
        background-color: blue;
        height: 200px;
        grid-column: 1;
        grid-row: 2;
      }
      
      *, *::before, *::after {
          box-sizing: inherit;
      }
      <div class="columns">
        <div class="column is-8 box-1">
            Box 1
        </div>
        <div class="column is-4 box-2">
            Box 2
        </div>
        <div class="column is-8 box-3">
            Box 3
        </div>
      </div>
      <!-- on desktop: box 1, 2 and 3 -->
      <!-- on mobile: box 1, 2 and 3 -->

      编辑:为了让它响应,你可以很容易地在你的 css 末尾弹出这个:

      @media (max-width: 700px) {
        .columns {
              display: block;
        }
      }
      

      .columns {
        display: grid;
        width: 100%;
        grid-gap: 0;
        grid-template-columns: 2fr 1fr;
      }
      
      .column {
        display: block;
      }
      
      .box-1 {
        background-color: red;
        height: 200px;
        grid-column: 1;
        grid-row: 1;
      }
      .box-2 {
        background-color: yellow;
        height: 400px;
        grid-column: 2;
        grid-row: 1 / 3;
      }
      .box-3 {
        background-color: blue;
        height: 200px;
        grid-column: 1;
        grid-row: 2;
      }
      
      *, *::before, *::after {
          box-sizing: inherit;
      }
      
      @media (max-width: 700px) {
        .columns {
          display: block;
        }
      }
      <div class="columns">
        <div class="column is-8 box-1">
            Box 1
        </div>
        <div class="column is-4 box-2">
            Box 2
        </div>
        <div class="column is-8 box-3">
            Box 3
        </div>
      </div>
      <!-- on desktop: box 1, 2 and 3 -->
      <!-- on mobile: box 1, 2 and 3 -->

      【讨论】:

      • 我明白了,很有趣。我也考虑过网格,但希望通过 flex 可以实现。无论如何,你如何让盒子 1、2 和 3 100% 使用移动设备?
      • 刚刚编辑的帖子。
      • 对于第二个问题,我看到了 flex 的一个大问题,因为它不支持行 :( 可能有很长很长的解决方法,但我不确定 :(
      • 我正在尝试实施您的解决方案,但是,我收到 grid-template-columns: 2fr / 1fr; 的“无效属性值”
      • 啊,一定是grid-template-columns: 2fr 1fr;
      猜你喜欢
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多