Ordering flex items

前端 未结 3 529
自闭症患者
自闭症患者 2021-01-06 11:31

Suppose I have this:



        
相关标签:
3条回答
  • 2021-01-06 12:04

    Reverse the row order by using row-reverse instead of row for the flex-direction.

    flex-direction: row-reverse;
    

    You can also order the children manually by giving them each an order property that takes an integer.

    .flex-container :nth-child(1) { order: 3; }
    .flex-container :nth-child(2) { order: 2; }
    .flex-container :nth-child(3) { order: 1; }
    
    0 讨论(0)
  • 2021-01-06 12:20

    Its easy if you can switch to CSS Grid layout:

    • create an grid container on flex-cotainer element

    • place the second element to first column (using grid-column: 1) spanning 2 rows (using grid-row: 1 / 3).

    See demo below:

    .flex-container {
      display: inline-grid;
      grid-template-columns: auto auto;
    }
    
    .flex-container div:nth-child(2) {
      grid-column: 1;
      grid-row: 1 / 3;
    }
    
    .flex-container > div {
      background-color: DodgerBlue;
      color: white;
      width: 100px;
      margin: 10px;
      text-align: center;
      line-height: 75px;
      font-size: 30px;
    }
    <div class="flex-container">
      <div><a href="#">1</a></div>
      <div><a href="#">2</a></div>
      <div><a href="#">3</a></div>
    </div>

    A flexbox solution is hacky at best using negative margins and known heights - see below:

    .flex-container {
      display: flex;
      flex-wrap: wrap;
      width: 200px;
    }
    
    .flex-container div:nth-child(1) {
      align-self: flex-start;
    }
    
    .flex-container div:nth-child(2) {
      order: -1;
      line-height: 170px;
    }
    
    .flex-container div:nth-child(3) {
      margin-left: auto;
      margin-top: -85px;
    }
    
    .flex-container > div {
      background-color: DodgerBlue;
      color: white;
      width: calc(100px - 20px);
      margin: 10px;
      text-align: center;
      line-height: 75px;
      font-size: 30px;
    }
    <div class="flex-container">
      <div><a href="#">1</a></div>
      <div><a href="#">2</a></div>
      <div><a href="#">3</a></div>
    </div>

    0 讨论(0)
  • 2021-01-06 12:24

    You shouldn't use Flexbox for this because it's not designed for it. Consider using CSS Grid instead. Using grid-template-areas, assigning the children where we want them in the layout becomes a trivial task. The DOM order of the grid children becomes irrelevant.

    .grid-container {
      display: inline-grid;
      grid-template-areas: "two one"
                           "two three";
    }
    
    .grid-container > div {
      background-color: DodgerBlue;
      color: white;
      width: 100px;
      margin: 10px;
      text-align: center;
      line-height: 75px;
      font-size: 30px;
    }
    
    .one {
      grid-area: one;
    }
    
    .two {
      grid-area: two;
    }
    
    .three {
      grid-area: three;
    }
    <div class="grid-container">
      <div class="one"><a href="#">1</a></div>
      <div class="two"><a href="#">2</a></div>
      <div class="three"><a href="#">3</a></div>
    </div>

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