CSS IE/EDGE 中的网格自动放置

CSS Grid auto placement in IE/EDGE

同时使用 IE 11 和 EDGE 支持的旧 CSS 网格规范。是否可以像当前规格一样自动放置网格项目?

即不必在网格项目上定义列:

.item:nth-child(1) {
    -ms-grid-column: 1;
}
.item:nth-child(2) {
    -ms-grid-column: 2;
}
.item:nth-child(n) {
    -ms-grid-column: n;
}

https://codepen.io/JoeHastings/pen/mMPoqB

答案是否定的(不幸的是)。

Old specs section about auto-placement 有这样的序言

This section describes early thinking around automatic placement of Grid Items. Multiple algorithms are possible for such a feature. One is proposed here.

运行 IE/Edge 中的这段代码,您会在控制台中看到很多带有 1 的行,因为 IE/Edge 将所有网格项目堆叠在第一个单元格中,而您无法强制 IE/Edge 自动放置网格项目。将 -ms-grid-column and -ms-grid-row 设置为 auto 不会改变任何内容,因为不支持此值(正如您在 MSDN 链接中看到的那样)。演示:

var gridItems = document.querySelectorAll(".grid__item");
for (var i = 0; i < gridItems.length; i++) {
  var gridItem = gridItems[i];
  console.log(window.getComputedStyle(gridItem)["-ms-grid-row"]);
  console.log(window.getComputedStyle(gridItem)["-ms-grid-column"]);
}
.grid {
  display: -ms-grid;
  -ms-grid-columns: 100px 100px 100px;
  -ms-grid-rows: 100px 100px 100px;
}

.grid__item {
  -ms-grid-row: auto;
  -ms-grid-column: auto;
  background-color: tomato;
  color: white;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="grid">
  <div class="grid__item">One</div>
  <div class="grid__item">Two</div>
  <div class="grid__item">Three</div>
  <div class="grid__item">Four</div>
  <div class="grid__item">Five</div>
  <div class="grid__item">Six</div>
  <div class="grid__item">Seven</div>
  <div class="grid__item">Eight</div>
  <div class="grid__item">Nine</div>
</div>