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

时间:2017-08-01 11:20:55

标签: css css3 css-grid

使用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

1 个答案:

答案 0 :(得分:4)

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

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

  

本节介绍有关网格项自动放置的早期思考。这种特征可以有多种算法。这里提出一个。

在IE / Edge中运行此代码,您将在控制台中看到许多行1,因为IE / Edge会堆叠第一个单元格中的所有网格项,并且您无法强制IE / Edge放置网格物品自动。将-ms-grid-column-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>