CSS grid-autoflow如何工作?

时间:2018-12-10 14:48:13

标签: css css3 css-grid

我遇到的问题是我不了解隐式网格的工作方式。我阅读了文档,MDN和更多资源。但是还有一个悬而未决的问题。

.grid {
  display: grid;
  grid-template: repeat(2, 100px) / repeat(6, 1fr);
  grid-gap: 1rem;
  font-size: 2rem;
}

.grid__item {
  background-color: #BDBDBD;
}

.grid__item--1 {
  grid-row: 1 / -1;
}

.grid__item--5 {
  grid-row: 1 / -1;
}
<div class="grid">
  <div class="grid__item grid__item--1">1</div>
  <div class="grid__item grid__item--2">2</div>
  <div class="grid__item grid__item--3">3</div>
  <div class="grid__item grid__item--4">4</div>
  <div class="grid__item grid__item--5">5</div>
  <div class="grid__item grid__item--6">6</div>
</div>

结果如下: example

为什么第五个块位于第二个块之前?

代码https://jsfiddle.net/serhioses/51do02xa/

1 个答案:

答案 0 :(得分:1)

您首先在in this part of the specification中定义了您想要的内容:

  
      
  1. 处理锁定到给定行的项目。
  2.   
     

对于每个具有确定行位置的网格项目(即grid-row-start grid-row-end属性定义一个确定的网格位置),按顺序修改文档顺序:

然后

  
      
  1. 定位剩余的网格项。
  2.   

因此,基本上,我们将显式放置在行内的项目放置(grid-row-startgrid-row-end应该与auto不同),然后考虑在以上规格。

即使您修改顺序,您也将得到相同的结果:

.grid {
  display: grid;
  grid-template: repeat(2, 100px) / repeat(6, 1fr);
  grid-gap: 1rem;
  font-size: 2rem;
}

.grid__item {
  background-color: #BDBDBD;
}

.grid__item--1 {
  grid-row: 1 / -1;
}

.grid__item--5 {
  grid-row: 1 / -1;
  order:100;
}
<div class="grid">
  <div class="grid__item grid__item--1">1</div>
  <div class="grid__item grid__item--2">2</div>
  <div class="grid__item grid__item--3">3</div>
  <div class="grid__item grid__item--4">4</div>
  <div class="grid__item grid__item--5">5</div>
  <div class="grid__item grid__item--6">6</div>
</div>