有没有办法在网格间隙上伸展网格项目?

发布于 2025-02-10 03:36:31 字数 415 浏览 2 评论 0原文

我有一个容器和一个孩子

.container {
  display: grid;
  grid-template-columns: repeat(21, 1fr);
  gap: 20px;
  width: 100%;
}

.container .child {
  grid-column: 1/ 4;
}

”在此处输入图像说明”

是否有一种方法可以将.Child在第5列旁边带上)

I have a container and a child

.container {
  display: grid;
  grid-template-columns: repeat(21, 1fr);
  gap: 20px;
  width: 100%;
}

.container .child {
  grid-column: 1/ 4;
}

enter image description here

Is there a way to bring the .child next to column 5 (so to column 4 plus the 20px)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2025-02-17 03:36:31

根据 mdn docs

在网格尺寸方面,缝隙的作用似乎是常规的网格轨道,但是没有任何东西可以放在缝隙中。差距的作用好像该位置的网格线已经获得了额外的尺寸,因此在该线路结束时放置的任何网格项目都在缝隙的末端开始。

因此,您必须将内容与rain -right:-20px重叠您的内容。

.container {
  background: green;
  display: grid;
  height: 200px;
  grid-template-columns: repeat(21, 1fr);
  gap: 20px;
  width: 100%;
}

.container .child {
  grid-column: 1/ 4;
  background: orange;
  margin-right: -20px;
}
<div class="container">
  <div class="child" />
</div>

According to the MDN docs :

In terms of grid sizing, gaps act as if they were a regular grid track however nothing can be placed into the gap. The gap acts as if the grid line at that location has gained extra size, so any grid item placed after that line begins at the end of the gap.

Therefore you have to overlap your content above that gap with margin-right: -20px

.container {
  background: green;
  display: grid;
  height: 200px;
  grid-template-columns: repeat(21, 1fr);
  gap: 20px;
  width: 100%;
}

.container .child {
  grid-column: 1/ 4;
  background: orange;
  margin-right: -20px;
}
<div class="container">
  <div class="child" />
</div>

樱&纷飞 2025-02-17 03:36:31

如果您想要.Child 列5 ie 第4列加上20px,如您所述。

解决的简单方法是将保证金:-20px添加到child

希望它能有所帮助!

If you want .child next to the column 5 i.e column 4 plus the 20px as you mentioned.

The easy way around is to add margin-right: -20px to the child

Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文