代码之家  ›  专栏  ›  技术社区  ›  Adam C

Flexbox布局图案:5方形(1大,4小)

  •  5
  • Adam C  · 技术社区  · 10 年前

    我正在尝试组合一个flexbox布局-为了这个问题的目的,我将其称为“方形五块”布局(见图)-但我遇到了麻烦,因为我尝试的所有实验都没有正确包装。

    我见过使用float做同样的布局,但我希望将来能稍微验证一下,并使用更新的方法——因此是flexbox。我尝试过寻找这种模式,但似乎没有一个一致的名称,所以找到类似的例子是很困难的。

    我也使用了视口单位,以确保块保持完全正方形,所有这些都基于视口宽度(vw)单位。

    div { width: 25vw; height: 25vw; }
    div:first-of-type { width: 50vw; height: 50vw; }
    

    关键的特点是所有的方块都应该是正方形的,但是第一个方块的大小应该是其余四个方块的总和。以前有人看过这样的布局吗?

    5-block grid of perfect squars using flexbox; first block should be exact size of the remaining four blocks

    谢谢

    2 回复  |  直到 10 年前
        1
  •  5
  •   Paulie_D    10 年前

    嵌套的Flexbox可以与媒体查询结合使用。

    Codepen Demo with media query

    基本上:

    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }
    .parent {
      width: 100vw;
      display: flex;
    }
    .col {
      flex: 0 0 50vw;
      height: 50vw;
      background: blue;
    }
    .wrap {
      display: flex;
      flex-wrap: wrap
    }
    .box {
      flex: 0 0 25vw;
      height: 25vw;
    }
    .red {
      background: red;
    }
    .pink {
      background: pink;
    }
    .orange {
      background: orange;
    }
    .grey {
      background: grey;
    }
    <div class="parent">
      <div class="col"></div>
      <div class="col wrap">
        <div class="box red"></div>
        <div class="box pink"></div>
        <div class="box orange"></div>
        <div class="box grey"></div>
      </div>
    </div>
        2
  •  2
  •   Rahul K    10 年前

    请参阅下面的代码并展开结果。我用过flexbox

    body {
      margin: 0;
      height: 100vh;
      width: 100vw;
    }
    
    .wrapper {
      height: 100%;
    }
    
    .layout.horizontal,
    .layout.vertical {
      display: -ms-flexbox;
      display: -webkit-flex;
      display: flex;
    }
    
    .layout.horizontal {
      -ms-flex-direction: row;
      -webkit-flex-direction: row;
      flex-direction: row;
    }
    
    .layout.vertical {
      -ms-flex-direction: column;
      -webkit-flex-direction: column;
      flex-direction: column;
    }
    
    .flex {
      -ms-flex: 1 1 0.000000001px;
      -webkit-flex: 1;
      flex: 1;
      -webkit-flex-basis: 0.000000001px;
      flex-basis: 0.000000001px;
    }
    
    .box {
      color: #fff;
      text-align: center;
    }
    
    .box.blue {
      background: #312783;
    }
    
    .box.green {
      background: #0B983A;
    }
    
    .box.yellow {
      background: #E1BD48;
    }
    
    .box.pink {
      background: #D2007F;
    }
    
    .box.orange {
      background: #EB6053;
    }
    
    @media all and (max-width: 768px) {
      .change-in-responsive.layout.horizontal {
        -ms-flex-direction: column;
        -webkit-flex-direction: column;
        flex-direction: column;
      }
    }
    <div class="layout horizontal wrapper change-in-responsive">
      <div class="box large flex blue">1</div>
      <div class="flex layout vertical">
        <div class="flex layout horizontal">
          <div class="box green flex">2</div>
          <div class="box yellow flex">3</div>
        </div>
        <div class="flex layout horizontal">
          <div class="box pink flex">4</div>
          <div class="box orange flex">5</div>
        </div>
      </div>
    </div>
    推荐文章