Reverse order of columns in CSS Grid Layout

后端 未结 6 1919
日久生厌
日久生厌 2020-12-08 19:05

I was hoping to use CSS Grid to reverse the apparent order of two side-by-side divs, where one of the divs grows arbitrarily (I don\'t want to use floats).

I\'ve c

相关标签:
6条回答
  • 2020-12-08 19:49

    I found out: I need to apply grid-auto-flow: dense; on the container:

    0 讨论(0)
  • 2020-12-08 19:56

    I'm not sure how to reverse more grid items. But if you have 2 grid items in your grid, you can simply position 2nd grid item using below code.

    #container > .b {
        grid-column-start: 1;
        grid-row-start: 1;
    }
    
    0 讨论(0)
  • 2020-12-08 19:57

    While the above solutions work for this specific example, it isn't the proper way to solve your question. There is a CSS property that Grid Layout adheres to called direction. direction: rtl will give you what the question calls for. It is supported by all browsers that support Grid Layout.

    #container {
      grid-template-columns: 240px 1fr;
      display: grid;
    }
    
    #container.reverse {
      direction: rtl;
    }
    
    .a {
      background: yellow;
    }
    
    .b {
      background: blue;
      color: white;
    }
    <h3>direction: unset</h3>
    <div id="container">
      <div class="a">A</div>
      <div class="b">B</div>
    </div>
    
    <h3>direction: rtl</h3>
    <div id="container" class="reverse">
      <div class="a">A</div>
      <div class="b">B</div>
    </div>

    0 讨论(0)
  • 2020-12-08 20:04

    As the Grid auto-placement algorithm lays out items in the container, it uses next available empty cells (source).

    In your source code the A element comes before the B element:

    <div id="container" class="reverse" style="width: 800px;">
       <div class="a">A</div>
       <div class="b">B</div>
    </div>
    

    Therefore, the grid container first places A, then uses the next available space to place B.

    By default, the auto-placement algorithm looks linearly through the grid without backtracking; if it has to skip some empty spaces to place a larger item, it will not return to fill those spaces. To change this behavior, specify the dense keyword in grid-auto-flow.

    http://www.w3.org/TR/css3-grid-layout/#common-uses-auto-placement


    grid-auto-flow: dense

    One solution to this problem (as you have noted) is to override the default grid-auto-flow: row with grid-auto-flow: dense.

    With grid-auto-flow: dense, the Grid auto-placement algorithm will look to back-fill unoccupied cells with items that fit.

    #container {
      display: grid;
      grid-template-columns: 240px 1fr;
      grid-auto-flow: dense; /* NEW */
    }
    

    7.7. Automatic Placement: the grid-auto-flow property

    Grid items that aren’t explicitly placed are automatically placed into an unoccupied space in the grid container by the auto-placement algorithm.

    grid-auto-flow controls how the auto-placement algorithm works, specifying exactly how auto-placed items get flowed into the grid.

    dense

    If specified, the auto-placement algorithm uses a “dense” packing algorithm, which attempts to fill in holes earlier in the grid if smaller items come up later. This may cause items to appear out-of-order, when doing so would fill in holes left by larger items.

    #container {
      display: grid;
      grid-template-columns: 240px 1fr;
      grid-auto-flow: dense; /* NEW */
    }
    
    .a {
      background: yellow;
    }
    
    .b {
      background: blue;
      color: white;
    }
    
    #container>.a {
      grid-column: 1;
    }
    
    #container>.b {
      grid-column: 2;
    }
    
    #container.reverse>.a {
      grid-column: 2;
    }
    
    #container.reverse>.b {
      grid-row: 1;
      grid-column: 1;
    }
    <div id="container" class="reverse" style="width: 800px;">
      <div class="a">A</div>
      <div class="b">B</div>
    </div>


    grid-row: 1

    Another solution would be to simply define the row for the second item.

    #container>.b {
      grid-column: 2;
      grid-row: 1; /* NEW */
    }
    

    #container {
      display: grid;
      grid-template-columns: 240px 1fr;
    }
    
    .a {
      background: yellow;
    }
    
    .b {
      background: blue;
      color: white;
    }
    
    #container>.a {
      grid-column: 1;
    }
    
    #container>.b {
      grid-column: 2;
      grid-row: 1; /* NEW */
    }
    
    #container.reverse>.a {
      grid-column: 2;
    }
    
    #container.reverse>.b {
      grid-row: 1;
      grid-column: 1;
    }
    <div id="container" class="reverse" style="width: 800px;">
      <div class="a">A</div>
      <div class="b">B</div>
    </div>

    0 讨论(0)
  • 2020-12-08 20:05

    I found out: I need to apply grid-auto-flow: dense; on the container:

    #container {
      grid-template-columns: 240px 1fr;
      display: grid;
      grid-auto-flow: dense;
    }
    

    According to MDN, this algorithm attempts to fill in holes earlier in the grid.

    0 讨论(0)
  • 2020-12-08 20:05

    There's a very simple CSS way. Just change:

    direction: rtl; 
    

    of the grid container.

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