在CSS Grid中混合固定长度和repeat()

Qua*_*kle 5 html css css3 css-grid

我正在尝试使用网格布局制作日历。一周中的每一天都有标题。我的目标是将标题的高度(即:网格中的第一行)设置为30px,并让其余的行拆分剩余的可用空间。

我的日历CSS如下所示:

.calendar{
    display:grid;
    grid-template-columns:repeat(1,1fr);
    grid-template-rows:30px repeat(auto-fill,1fr);
}
Run Code Online (Sandbox Code Playgroud)

现在,我认为这完全可以实现我想要的功能,但是30px没有效果,并且每一行都是相等的高度。是否可以将静态值与混合repeat()

我意识到我只能制作2个网格-一个用于标题,一个用于日间网格-但我很好奇是否有一种更清洁的方法。

演示在这里:https : //codepen.io/anon/pen/yGEaOm

.calendar{
    display:grid;
    grid-template-columns:repeat(1,1fr);
    grid-template-rows:30px repeat(auto-fill,1fr);
}
Run Code Online (Sandbox Code Playgroud)
.calendar {
  display: grid;
  grid-template-columns: repeat(1, 1fr);
  grid-template-rows: 30px repeat(auto-fill, 1fr);
}


/** Appearance styles that don't affect the layout */
.calendar {
  min-height: 200px;
}
.day {
  border: 1px solid black;
  display: flex;
  align-items: center;
  justify-content: center;
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*l_B 5

简答

代替:

grid-template-rows: 30px repeat(auto-fill, 1fr)
Run Code Online (Sandbox Code Playgroud)

尝试这个:

grid-template-rows: 30px repeat(auto-fit, minmax(10px, 1fr))
Run Code Online (Sandbox Code Playgroud)

grid-template-rows: 30px repeat(auto-fill, 1fr)
Run Code Online (Sandbox Code Playgroud)
grid-template-rows: 30px repeat(auto-fit, minmax(10px, 1fr))
Run Code Online (Sandbox Code Playgroud)


解释

请注意您的代码中的此规则:

grid-template-rows: 30px repeat(auto-fill, 1fr)
Run Code Online (Sandbox Code Playgroud)

此规则无效。

在此处输入图片说明

因此,它会被浏览器忽略并grid-template-rows回退到其默认设置:(none这意味着所有行都将由 隐式创建和调整大小grid-auto-rows,默认值为auto)。

从规范:

§ 7.2。Explicit Track Sizing: thegrid-template-rowsgrid-template-columns 属性

none值。

表示此属性未创建显式网格轨迹(尽管仍可通过 来创建显式网格轨迹 grid-template-areas)。

注意:在没有显式网格的情况下,将隐式生成任何行/列,它们的大小将由grid-auto-rowsgrid-auto-columns属性确定 。

主要问题auto-fill(and auto-fit) 不能与固有灵活的长度组合,例如auto,min-contentfr

§ 7.2.2.1。的语法 repeat()

  • 自动重复 (auto-fillauto-fit) 不能与固有灵活的大小相结合。

§ 11. 网格大小

因此,因为您的规则 ( (auto-fill, 1fr)) 包含auto-fill一个灵活的大小调整函数 ( fr),它无法工作。

解决方案可能是使用minmax().

代替:

grid-template-rows: 30px repeat(auto-fill, 1fr)
Run Code Online (Sandbox Code Playgroud)

尝试这个:

grid-template-rows: 30px repeat(auto-fill, minmax(10px, 1fr))
Run Code Online (Sandbox Code Playgroud)

更多细节: