Grid

详细介绍了CSS Grid Layout (w3schools.com)open in new window

grid-template-areas

W3Schools Tryit Editor 示例open in new window

fr unit

An Introduction to the fr CSS unit | CSS-Tricks - CSS-Tricksopen in new window

setting each column to 1fr takes that 10px into account automatically and subtracts it from the total width available for each column.

The fr unit (a “fraction”) can be used when defining grids like any other CSS lengthopen in new window such as %, px or em

很方便来计算长度,并且不会担心overflow.

.grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);  // 会将10px包括进来,repeat(4,25%)则不会,实际是(25%+10px)
  grid-column-gap: 10px;  
}

如下面的快速布局

.grid {
  display: grid;
  grid-template-columns: 250px repeat(12, 1fr);
  grid-column-gap: 10px;
}

image-20220927173343800

repeat function

grid-template-columns: 25% 25% 25% 25%
// 新特性
grid-template-columns: repeat(4, 25%);
// repeat(number of columns/rows, the column width we want);

属性