Understanding grid negative values

后端 未结 2 1518
攒了一身酷
攒了一身酷 2020-11-28 15:55

I\'m trying to make a grid that has a full span row at the bottom.

For full span columns I can use grid-column: 1/-1.

For single span columns I

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 16:42

    when using the same value inside grid-column/grid-row you will fall into this rule:

    If the placement for a grid item contains two lines, and the start line is further end-ward than the end line, swap the two lines. If the start line is equal to the end line, remove the end line.ref

    So saying grid-column:-1/-1 means grid-column:-1 which is grid-column:-1/auto

    auto

    The property contributes nothing to the grid item’s placement, indicating auto-placement or a default span of one. (See § 8 Placing Grid Items, above.)

    So basiclly you said to your element to start at the last line and span one column which will create an implicit new column:

    A basic example to illustrate:

    .box {
      display:grid;
      grid-template-columns:20px 20px 20px;
      grid-auto-columns:100px;
      grid-gap:5px;
    }
    
    span {
      grid-column:-1/-1;
      height:40px;
      background:red;
    }

    You can see that the span is having 100px which means it create a new column inside the implicit grid and is not inside the explicit one defined by 20px

    When using -2/-1 it's clear that you will consider the before the last and the last line and the element will be placed in the last explicit column:

    .box {
      display:grid;
      grid-template-columns:20px 20px 20px;
      grid-auto-columns:100px;
      grid-gap:5px;
    }
    
    span {
      grid-column:-2/-1;
      height:40px;
      background:red;
    }

    Same logic apply when using positive value but you won't notice a strange behavior since you will most likely span an explicit column thinking it's correct to specify, for example, grid-column:1/1

提交回复
热议问题