了解网格负值 [英] Understanding grid negative values

查看:137
本文介绍了了解网格负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个在底部具有完整跨度行的网格.

对于全跨度列,我可以使用grid-column: 1/-1.

对于单跨度列,我可以使用grid-column: 1/1.

对于单跨度行,我可以使用grid-row: 1/1.

但是,如果要定义最后一列或最后一行,则必须编写grid-column: -2/-1.

为什么第一列/行的语法与1/1不同?还是我在某个地方犯了错误?

我还制作了一个jsfiddle来演示我的问题: jsfiddle

 .grid-container {
  display: grid;
  grid-template-columns: 5px 1fr 1fr;
  grid-template-rows: minmax(50px, 2fr) 1fr 1fr 1fr 1fr 1fr 15px;
}

.grid-item {
  width: 1fr;
}

.header {
  display: flex;
  grid-column: 2/-1;
  grid-row: 1/1;
  justify-content: center;
}

.border-left {
  background: purple;
  grid-column: 1/1;
  grid-row: 1/-1;
}

.border-bottom {
  background: #410266;
  grid-column: 2/-1;
  /* grid-row: -2 / -1;  this will work, -1/-1 will not */
  grid-row: -1 / -1;
} 

 <div class="grid-container">
  <div class="header"> HEADER </div>
  <div class="border-left"></div>
  <div class="grid-item">1 </div>
  <div class="grid-item">2 </div>
  <div class="grid-item">3 </div>
  <div class="grid-item">4 </div>
  <div class="border-bottom"></div>
</div> 

解决方案

grid-column/grid-row中使用相同的值时,您会陷入以下规则:

如果网格项目的放置包含两行,并且起始行比结束行更靠末端,则交换两行. 如果起始行等于结束行,则删除结束行. 参考

所以说grid-column:-1/-1表示grid-column:-1,即grid-column:-1/auto

auto

该属性不会对网格项目的位置产生任何影响,表示自动放置或默认跨度为. (请参见上方的第8节放置网格项".)

因此,基本上,您对元素说要从最后一行开始并跨越一列,这将创建一个隐式的新列:

一个基本的例子来说明:

 .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;
} 

 <div class="box">
  <span></span>
</div> 

您会看到范围具有100px,这意味着它将在隐式网格内创建一个新列,而不是在20px

所定义的显式网格内.

使用-2/-1时,很明显,您将在最后 last 行之前考虑,并将元素放置在最后一个显式列中:

 .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;
} 

 <div class="box">
  <span></span>
</div> 

使用正值时适用相同的逻辑,但是您不会注意到奇怪的行为,因为您很可能会跨过一个明确的列,以为指定正确,例如grid-column:1/1

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 can use grid-column: 1/1.

For single span rows I can use grid-row: 1/1.

But if I want to define the last column or row, I have to write grid-column: -2/-1.

Why is the syntax not the same as with 1/1 for the first column/row? Or am I making a mistake somewhere?

I also made a jsfiddle to demonstrate my problem: jsfiddle

.grid-container {
  display: grid;
  grid-template-columns: 5px 1fr 1fr;
  grid-template-rows: minmax(50px, 2fr) 1fr 1fr 1fr 1fr 1fr 15px;
}

.grid-item {
  width: 1fr;
}

.header {
  display: flex;
  grid-column: 2/-1;
  grid-row: 1/1;
  justify-content: center;
}

.border-left {
  background: purple;
  grid-column: 1/1;
  grid-row: 1/-1;
}

.border-bottom {
  background: #410266;
  grid-column: 2/-1;
  /* grid-row: -2 / -1;  this will work, -1/-1 will not */
  grid-row: -1 / -1;
}

<div class="grid-container">
  <div class="header"> HEADER </div>
  <div class="border-left"></div>
  <div class="grid-item">1 </div>
  <div class="grid-item">2 </div>
  <div class="grid-item">3 </div>
  <div class="grid-item">4 </div>
  <div class="border-bottom"></div>
</div>

解决方案

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;
}

<div class="box">
  <span></span>
</div>

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;
}

<div class="box">
  <span></span>
</div>

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

这篇关于了解网格负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆