了解CSS网格中的溢出 / minmax行为

发布于 2025-02-09 01:52:16 字数 2292 浏览 1 评论 0原文

tldr:嵌套网格溢流的codepen列:重复(16,minmax(0,1fr));


因此,我有一个元素容器,带有两个主要元素,summary-wrapper和<代码> stats-wrapper

容器具有以下网格:

.container {
    display: grid;
    grid-template-columns: repeat(16, 1fr);

    column-gap: 4rem;
    row-gap: 4rem;
    padding: 5rem;
    width: 100%;
}

summary-wrapper放置在grid> grid-column:1/11; (第96行)和 stats-wrapper at 网格列:11/17;(第25行)

在我心中,将统计数据的网格列更改为网格 - 列表:12/17应该只移动元素的开始,它在chrome中进行:

chrome(无溢出):

“

但是在firefox中:

现在,浏览器之间的行为不同似乎有些怪异,但并没有真正改变我对网格容器的工作方式的心理模型只是……错误 - 而且我经历过(并且在我的项目中经历)这种行为在Chrome中,这并不是我问题的重点。

我以前有这个问题,但是有输入,并且被定向到这个答案这允许更长的元素使用<<<代码>网格 - 板块列:重复(16,minmax(0,1fr));

这似乎适用于STATS元素(如果您查找标题,则不再溢出页面)。但是,由于该元素的内容(不包括标头)也是嵌套网格的一部分,所以我遇到了同样的问题,但是它并未使用网格 - 板块 - 列来固定:重复(16,Minmax(Minmax(0,1fr)) );(第50行)这次:

“

我觉得很奇怪,因为我看不出任何原因(无论哪种情况)需要溢出。甚至使用minmax(0,1fr)的修复程序也依赖于说明

1fr等效于MinMax(Auto,1FR)

,并且auto阻止轨道小于项目。但是在这种情况下(afaik)项目stats__section-查看 stats__section-sate-section-applications and stats__section-stats__section-trend ,除了他们的文字,

我只是很困惑!

codepen

任何帮助!

TLDR: Codepen of nested grid overflowing and not being fixed by using grid-template-columns: repeat(16, minmax(0,1fr));


So I've an element container, with two main elements, summary-wrapper and stats-wrapper

container has the following grid:

.container {
    display: grid;
    grid-template-columns: repeat(16, 1fr);

    column-gap: 4rem;
    row-gap: 4rem;
    padding: 5rem;
    width: 100%;
}

summary-wrapper is placed at grid-column: 1/11; (line 96) and
stats-wrapper at grid-column: 11/17; (line 25)

In my mind, changing the grid column of the stats-wrapper to grid-column: 12/17 should just move the start of the element, which it does in Chrome:

Chrome (no overflow):

chrome grid-column change

But in Firefox:

Firefox grid-column change

Now the behaviour being different between browsers seems a bit weird, but doesn't really change the fact that my mental model for how grid containers works is just... wrong - and I've experienced (and am experiencing in my project) this behaviour in Chrome, so that's not really the focus of my question.

I've had this issue before, but with inputs, and was directed to this answer that allows longer elements to collapse using grid-template-columns: repeat(16, minmax(0,1fr));

This seems to work for the stats element (if you look the header no longer overflows the page). But because the contents of that element (excluding the header) are part of a nested grid too, I get the same problem, but it's not fixed using grid-template-columns: repeat(16, minmax(0,1fr)); (line 50) this time:

Firefox grid-column nested problem

I just find it very strange, as I can't see any reason why the elements (in either case) need to overflow. Even the fix using minmax(0,1fr) relies on the explanation that

1fr is equivalent to minmax(auto, 1fr)

and that auto is preventing the track being smaller than the item. But in this case (afaik) the items stats__section--viewed stats__section--applications and stats__section--trend have no min size, other than their text

I'm just stumped!

codepen

Any help appreciated!

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

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

发布评论

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

评论(1

溺渁∝ 2025-02-16 01:52:16

溢出的原因是嵌套网格.stats__content中的列隙规则。

.stats__content {
  position: relative;
  display: grid;
  grid-template-columns: repeat(16, minmax(0, 1fr));
  column-gap: 4rem; <----- CULPRIT
  row-gap: 3rem;
  background-color: white;
  padding: 2rem 0.1rem 0.1rem 0.1rem;
}

由于它是一个固定的宽度,重复15次(因为缝隙仅在轨道之间,而不在轨道和容器边缘之间),在这种情况下,溢出是不可避免的。

(4rem) * (15 gaps) = 60rem of inflexible width

减少间隙的数量或找到其他解决项目的解决方案。

The reason for the overflow is a column-gap rule in the nested grid .stats__content.

.stats__content {
  position: relative;
  display: grid;
  grid-template-columns: repeat(16, minmax(0, 1fr));
  column-gap: 4rem; <----- CULPRIT
  row-gap: 3rem;
  background-color: white;
  padding: 2rem 0.1rem 0.1rem 0.1rem;
}

Being that it's a fixed width, repeated 15 times (because gaps only go between tracks, not between tracks and container edges), an overflow is inevitable in this case.

(4rem) * (15 gaps) = 60rem of inflexible width

Reduce the number of gaps or find another solution for separating the items.

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