【问题标题】:CSS: Refactor grid to a flex layout with different childsCSS:将网格重构为具有不同子项的 flex 布局
【发布时间】:2019-07-15 09:05:51
【问题描述】:

我目前正在尝试构建一个应该有 3 个可能的子元素的 flex 布局:

  • 如果包装器只有一个按钮,它应该具有包装器的整个宽度。
  • 如果包装器有两个子级,则最后一个的最大宽度应为 75 像素,第一个必须填满可用空间。
  • 如果包装器共有三个子级,则第一行应与前面示例中的一样,但最后一个子级应位于下一行,宽度为 100%。

我已经构建了这个网格布局,但由于某些浏览器的兼容性,我需要将其更改为 flex。我怎样才能做到这一点?我尝试了很多,但结果总是很糟糕。

.wrapper {
    width: 60%;
    display: grid;
    display: -ms-grid;
    grid-template-columns: 1fr 75px;
    text-align: center;
    position: relative;
    margin-bottom: 20px;
}

.wrapper div+div {
    margin-left: 6px;
}

.wrapper div {
    background-color: yellow;
    padding: .6em 1em;
    text-align: center;
}

.wrapper div.show.single-button {
  grid-column: 1/span 2;
}

.wrapper div.cancel {
    grid-column: 1/span 2;
    -ms-grid-row: 2;
    -ms-grid-column-span: 2;
    margin-top: 6px;
    margin-left: 0;
}
<div class="wrapper">
  <div class="show single-button">Show</div>
</div>

<div class="wrapper">
  <div class="show">Show</div>
  <div class="invoice">Invoice</div>
</div>

<div class="wrapper">
  <div class="show">Show</div>
  <div class="invoice">Invoice</div>
  <div class="cancel">Cancel</div>
</div>

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    Flexbox 提供了你需要的一切。无需将 flexbox 与 box-model 或 width 属性混合使用:

    .wrapper {
      height: 100px;
      display: flex;
      flex-wrap: wrap;
      margin-bottom: 10px;
    }
    
    .wrapper > div {
      flex: 1;
      border: 1px dotted #999;
      background-color: aliceblue;
    }
    
    .wrapper > div:nth-child(2) {
      flex-shrink: 0;
      flex-grow: 0;
      flex-basis: 75px;
      -webkit-flex-basis: 75px;
    }
    
    .wrapper > div:nth-child(3) {
      flex-shrink: 0;
      flex-grow: 0;
      flex-basis: 100%;
      -webkit-flex-basis: 100%;
    }
    <div class="wrapper">
      <div>1</div>
    </div>
    
    <div class="wrapper">
      <div>1</div>
      <div>2</div>
    </div>
    
    <div class="wrapper">
      <div>1</div>
      <div>2</div>
      <div>3</div>
    </div>

    【讨论】:

      【解决方案2】:

      你可以使用 flex-basis 来判断元素的初始宽度,使用 flex-grow 来判断它们是否应该占用更多空间,并使用 flex-wrap 这样它就不会强制它们放在一行中,例如:

      .wrapper {
        display: flex;
        flex-wrap: wrap; // make it multiline
        margin-bottom: 1rem;
      }
      
      .wrapper div {
        background: yellow;
        margin: .2rem;
        padding: .2rem;
        text-align: center;
      }
      
      .invoice {
        flex-basis: 75px; // make it have 75px
        flex-grow: 0; // can't grow
        flex-shrink: 0; // can't shrink
      }
      
      .show {
        flex-grow: 1; // no basis width, just grow and take the space left from .invoice
      }
      
      .cancel {
        flex-basis: 100%; // takes the whole width so it must go on a single row
      }
      

      https://codepen.io/anon/pen/KJLqVj

      【讨论】:

        【解决方案3】:

        明智地使用flex:1flex-wrap 似乎有效。

        * {
          box-sizing: border-box;
        }
        
        .wrapper {
          width: 60%;
          margin: auto;
          display: flex;
          flex-wrap: wrap;
          margin-bottom: 20px;
          justify-content: space-between;
          border: 1px solid green;
        }
        
        .wrapper div {
          background-color: lightblue;
          padding: .6em 1em;
          text-align: center;
          margin: 6px;
        }
        
        .wrapper div.show {
          flex: 1;
        }
        
        .wrapper div.invoice {
          flex: 1;
          max-width: 75px;
        }
        
        .wrapper div.cancel {
          flex: 1 1 100%;
        }
        <div class="wrapper">
          <div class="show single-button">Show</div>
        </div>
        
        <div class="wrapper">
          <div class="show">Show</div>
          <div class="invoice">Invoice</div>
        </div>
        
        <div class="wrapper">
          <div class="show">Show</div>
          <div class="invoice">Invoice</div>
          <div class="cancel">Cancel</div>
        </div>

        【讨论】:

        • 这是我一直在寻找的解决方案。谢谢!
        猜你喜欢
        • 2021-03-12
        • 2018-08-05
        • 2021-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-30
        相关资源
        最近更新 更多