我遇到的问题是我不了解隐式网格的工作方式。我阅读了文档,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>
为什么第五个块位于第二个块之前?
答案 0 :(得分:1)
您首先在in this part of the specification中定义了您想要的内容:
- 处理锁定到给定行的项目。
对于每个具有确定行位置的网格项目(即
grid-row-start
和grid-row-end
属性定义一个确定的网格位置),按顺序修改文档顺序:
然后
- 定位剩余的网格项。
因此,基本上,我们将显式放置在行内的项目放置(grid-row-start
和grid-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>