防止内容扩展网格项目
tl; dr:是否有table-layout之类的东西:CSS网格的修复
?
我试图在几个月内创建一个带有大4x3网格的年度观看日历,并在其中嵌套了7x6网格。
日历应填写页面,因此年度网格容器的宽度和高度为100%。
.year-grid {
width: 100%;
height: 100%;
display: grid;
grid-template: repeat(3, 1fr) / repeat(4, 1fr);
}
.month-grid {
display: grid;
grid-template: repeat(6, 1fr) / repeat(7, 1fr);
}
这是一个工作示例:
”简单性,每月在那支笔中有31天,并从星期一开始。
我还选择了一个荒谬的小字体大小来证明问题:
网格项目(=日单元格)非常凝结,因为页面上有几百个。而且,一旦白天的标签变得太大(可以随意使用左上方的按钮在笔中的字体大小上播放),网格的大小就会增长并超过页面的身体尺寸。
有什么方法可以防止这种行为?
我最初宣布我的一年网格在宽度和高度上是100%,因此这可能是开始的一点,但是我找不到任何与网格相关的CSS属性,这些属性适合所需的需求。
免责声明:我知道,在不使用CSS网格布局的情况下,有一些非常简单的方式来设置该日历。但是,这个问题更多地是关于该主题的一般知识,而不是解决具体示例。
TL;DR: Is there anything like table-layout: fixed
for CSS grids?
I tried to create a year-view calendar with a big 4x3 grid for the months and therein nested 7x6 grids for the days.
The calendar should fill the page, so the year grid container gets a width and height of 100% each.
.year-grid {
width: 100%;
height: 100%;
display: grid;
grid-template: repeat(3, 1fr) / repeat(4, 1fr);
}
.month-grid {
display: grid;
grid-template: repeat(6, 1fr) / repeat(7, 1fr);
}
Here's a working example: https://codepen.io/loilo/full/ryXLpO/
For simplicity, every month in that pen there has 31 days and starts on a Monday.
I also chose a ridiculously small font size to demonstrate the problem:
Grid items (= day cells) are pretty condensed as there are several hundreds of them on the page. And as soon as the day number labels become too large (feel free to play around with the font size in the pen using the buttons on the upper left) the grid will just grow in size and exceed the page's body size.
Is there any way to prevent this behaviour?
I initially declared my year grid to be 100% in width and height so that's probably the point to start at, but I couldn't find any grid-related CSS properties that would've fitted that need.
Disclaimer: I'm aware that there are pretty easy ways to style that calendar just without using CSS Grid Layout. However, this question is more about the general knowledge on the topic than solving the concrete example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
默认情况下,网格项目不能小于其内容的大小。
网格项目的初始大小为
最小宽度:自动
和最小值:自动
。您可以通过将网格项目设置为
最小宽度来覆盖此行为:0
,min-height:0
或溢出
,除<<代码>可见。从规格:
这是一个更详细的说明涵盖了flex项目,但也适用于网格项目:
此帖子还涵盖了嵌套容器的潜在问题和已知的渲染差异在主要浏览器中。
要修复布局,请对您的代码进行这些调整:
jsfiddle demo
vs vs vs <代码> minmax(0,1fr)1fr
1fr上面的解决方案在网格项目级别运行。有关容器级别的解决方案,请参阅此文章:
By default, a grid item cannot be smaller than the size of its content.
Grid items have an initial size of
min-width: auto
andmin-height: auto
.You can override this behavior by setting grid items to
min-width: 0
,min-height: 0
oroverflow
with any value other thanvisible
.From the spec:
Here's a more detailed explanation covering flex items, but it applies to grid items, as well:
This post also covers potential problems with nested containers and known rendering differences among major browsers.
To fix your layout, make these adjustments to your code:
jsFiddle demo
1fr
vsminmax(0, 1fr)
The solution above operates at the grid item level. For a container-level solution, see this post:
以前的答案非常好,但我也想提一下,是网格的固定布局,您只需要编写
minmax(0,1fr)
而不是编写1fr
作为您的轨道大小。The previous answer is pretty good, but I also wanted to mention that there is a fixed layout equivalent for grids, you just need to write
minmax(0, 1fr)
instead of1fr
as your track size.现有答案解决了大多数情况。但是,我遇到了一个情况,我需要网格电池的内容为
溢出:可见
。我通过绝对放置包装器(不是理想的,但我知道最好的)来解决它:https:> https: //codepen.io/bjnsn/pen/vyyvpzv
The existing answers solve most cases. However, I ran into a case where I needed the content of the grid-cell to be
overflow: visible
. I solved it by absolutely positioning within a wrapper (not ideal, but the best I know), like this:https://codepen.io/bjnsn/pen/vYYVPZv
花了整整一天的时间处理这个问题。对于使用尾风的人:使用
grid-cols-1
。我有md:grid-cols-2
,并假设我不需要指定较小宽度的列数,因为1是默认值,而是陷入了此问题。作为参考,
grid-cols-1
解决网格 - 拼写 - 柱:重复(1,minmax(0,1fr));
>https://tailwindcss.com/docs/docs/grid-template-template-columns
Spent a whole day dealing with this. For those using Tailwind: use
grid-cols-1
. I hadmd:grid-cols-2
and assumed that I didn't need to specify the number of columns for smaller widths, since 1 is the default, but was running into this issue.For reference,
grid-cols-1
resolves togrid-template-columns: repeat(1, minmax(0, 1fr));
https://tailwindcss.com/docs/grid-template-columns