Flex-box: Align last row to grid

后端 未结 29 2475
不知归路
不知归路 2020-11-22 02:54

I have a simple flex-box layout with a container like:

.grid {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-between;
}

相关标签:
29条回答
  • 2020-11-22 03:18

    If the individual child items have an explicit width (eg. 32%), you can solve this by adding an :after element to the parent and giving this the same explicit width.

    0 讨论(0)
  • 2020-11-22 03:21

    One technique would be inserting a number of extra elements (as many as the max number of elements you ever expect to have in a row) that are given zero height. Space is still divided, but superfluous rows collapse to nothing:

    http://codepen.io/dalgard/pen/Dbnus

    body {
      padding: 5%;
    }
    
    div {
      overflow: hidden;
      background-color: yellow;
    }
    
    ul {
      display: flex;
      flex-wrap: wrap;
      margin: 0 -4px -4px 0;
      list-style: none;
      padding: 0;
    }
    
    li {
      flex: 1 0 200px;
      height: 200px;
      border-right: 4px solid black;
      border-bottom: 4px solid black;
      background-color: deeppink;
    }
    li:empty {
      height: 0;
      border: none;
    }
    
    *,
    :before,
    :after {
      box-sizing: border-box;
    }
    <div>
      <ul>
        <li>a</li>
        <li>b</li>
        <li>c</li>
        <li>d</li>
        <li>e</li>
        <li>f</li>
        <li>g</li>
        <li>h</li>
        <li>i</li>
        <li>j</li>
        <li>k</li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </div>

    In the future, this may become achievable through using multiple ::after(n).

    0 讨论(0)
  • 2020-11-22 03:22

    This is a combination of a lot of the answers but it does exactly what I was needing -- which is, aligning the last child in a flex container to the left while maintaining the space-between behavior (in this case it's a three-column layout).

    Here's the markup:

    .flex-container {
      display: flex;
      justify-content: space-between;
      flex-direction: row;
    }
    
    .flex-container:after {
      content: "";
      flex-basis: 30%;
    }
    
    0 讨论(0)
  • 2020-11-22 03:23

    If you know the width of spaces between elements in the row and the amount of elements in a row, this would work:

    Example: 3 elements in a row, 10px gap between elements

    div:last-child:nth-child(3n+2) {
      flex-grow: 1
      margin-left: 10px
    }
    
    0 讨论(0)
  • 2020-11-22 03:24

    I know there are many answers here but.. The simplest way to do this is with a grid instead of flex and grid template columns with repeat and auto fills, where you have to set the number of pixels that you have given to each element, 100px from your snippet code.

    .exposegrid {
         display: grid;
         grid-template-columns: repeat(auto-fill, 100px);
         justify-content: space-between;
    }
     .exposetab {
         width: 100px;
         height: 66px;
         background-color: rgba(255, 255, 255, 0.2);
         border: 1px solid rgba(0, 0, 0, 0.4);
         border-radius: 5px;
         box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
         margin-bottom: 10px;
    }
    <div class="exposegrid">
       <div class="exposetab"></div>
       <div class="exposetab"></div>
       <div class="exposetab"></div>
       <div class="exposetab"></div>
       <div class="exposetab"></div>
       <div class="exposetab"></div>
       <div class="exposetab"></div>
       <div class="exposetab"></div>
       <div class="exposetab"></div>
       <div class="exposetab"></div>
       <div class="exposetab"></div>
       <div class="exposetab"></div>
       <div class="exposetab"></div>
       <div class="exposetab"></div>
    </div>

    0 讨论(0)
  • 2020-11-22 03:24

    It is possible to use "flex-start" and to add the margins manually. It requires some math-hacking but is definitely easy to do and make it easy to use with a CSS preprocessor like LESS.

    See for example this LESS mixin:

    .flexboxGridMixin(@columnNumber,@spacingPercent) {
      @contentPercent: 100% - @spacingPercent;
      @sideMargin: @spacingPercent/(@columnNumber*2);
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      justify-content: flex-start;
      > * {
        box-sizing: border-box;
        width: @contentPercent/@columnNumber;
        margin-left: @sideMargin;
        margin-right: @sideMargin;
      }
    }
    

    And then it can easily be used to display a responsive grid layout:

    ul {
      list-style: none;
      padding: 0;
      @spacing: 10%;
      @media only screen and (max-width: 499px) { .flexboxGridMixin(1,@spacing); }
      @media only screen and (min-width: 500px) { .flexboxGridMixin(2,@spacing); }
      @media only screen and (min-width: 700px) { .flexboxGridMixin(3,@spacing); }
      @media only screen and (min-width: 900px) { .flexboxGridMixin(4,@spacing); }
      @media only screen and (min-width: 1100px) { .flexboxGridMixin(5,@spacing); }
    }
    
    li {
      background: pink;
      height: 100px;
      margin-top: 20px;
    }
    

    Here is an example of

    http://codepen.io/anon/pen/YyLqVB?editors=110

    0 讨论(0)
提交回复
热议问题