代码之家  ›  专栏  ›  技术社区  ›  klewis

理解速记flex和flex基本属性之间的差异

  •  0
  • klewis  · 技术社区  · 5 年前

    flex flex-basis flex-basis & flex ,与第一个示例不同,仅使用 弯曲 ?

    .row {
      display: flex;
      flex-flow: row wrap;
      flex: 1 1 100%; //Three values for flex: flex-grow | flex-shrink | flex-basis
    }
    

    VS

    .col {
     display: flex;
     flex-direction: column;
     flex-basis: 100%;
     flex: 1;
    }
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   dippas    5 年前

    因为在你的最后一个例子中 flex 将覆盖 flex-basis 将被重新定义

    const row = document.querySelector('.row'),
      col = document.querySelector('.col')
    
    
    console.log(`row: ${window.getComputedStyle(row).getPropertyValue('flex-basis')}`)
    console.log(`col: ${window.getComputedStyle(col).getPropertyValue('flex-basis')}`)
    section:first-child {
      display: flex;
      flex-flow: row wrap;
    }
    
    section:last-child {
      display: flex;
      flex-direction: column;
    }
    
    .row {
      flex: 1 1 100%;
    }
    
    .col {
      flex-basis: 100%;
      /*this will be flex-grow:1 flex-shrink: 0 flex-basis: 0% */
      flex: 1;
    }
    <section>
      <div class="row">row</div>
    </section>
    
    <section>
      <div class="col">col</div>
    </section>