我正在构建一个 CSS 网格布局,不知何故我无法获得“自动”值来调整行高的大小。
项目保持最小高度为 1fr,即使它们的内容小到足以让它们缩小。
这是一个解释问题的代码示例 - 您也可以在https://codepen.io/16kbit/pen/RJbMWM上查看
section {
display: grid;
grid-template-areas: "one top" "one bottom";
align-items: start;
border: 1px solid hotpink;
}
article {
border: 1px solid green;
}
#one {
grid-area: one;
}
#top {
grid-area: top;
}
#bottom {
grid-area: bottom;
background: yellow;
}Run Code Online (Sandbox Code Playgroud)
<section>
<article id=one>
<h1>One</h1>
<p>Lorem cool dolizzle sit amizzle, dope sizzle elizzle. Nullam shiznit velizzle, get down get down volutpizzle, suscipizzle quizzle, dizzle mammasay mammasa mamma oo sa, arcu. Pellentesque sheezy tortizzle. Sed erizzle. Fusce izzle dolor shiznit pimpin'
tempizzle tempor. Maurizzle pellentesque nibh shizzlin dizzle turpizzle. Vestibulum in tortor. Pellentesque cool rhoncus black. In hac fo shizzle my nizzle check out this dictumst. Black uhuh ... yih!. Mammasay mammasa mamma oo sa tellizzle shiz,
pretizzle shiznit, mattizzle fo, gangster vitae, nunc. Get down get down suscipizzle. Own yo' away izzle sed cool.Nullizzle fizzle shut the shizzle up yo mamma orci daahng dawg viverra. Phasellus nizzle shizzle my nizzle crocodizzle. Curabitizzle
sure velit vizzle check out this dizzle doggy. Maecenas sapien nulla, iaculis shiz, molestie hizzle, egestas a, erizzle. Shit vitae turpis quizzle nibh bibendizzle boom shackalack. Nizzle pulvinar dope velizzle. Aliquizzle mammasay mammasa mamma
oo sa volutpat. Nunc izzle its fo rizzle at lectus pretizzle faucibizzle. We gonna chung nec lacizzle own yo' fizzle pizzle ultricizzle. Ut nisl. Crunk et owned. Integer laoreet ipsum shizzlin dizzle mi. Donizzle at shiz.</p>
</article>
<article id=top>
<h1>Top</h1>
<p>Just Two Words</p>
</article>
<article id=bottom>
<h1>Bottom</h1>
<p>Help Me! How can I get closer to my Top neighbour?</p>
</article>
</section>Run Code Online (Sandbox Code Playgroud)
我希望#bottom 项目尽可能靠近#top 项目。我希望它们缩小到它们的内容大小。
CSS Grid 不允许项目的高度小于 1fr 单位(总高度的 50%)——这取决于#one 项目,它有很多文本。
视觉解释:我想要右边的结果,而不是左边的结果:
Mic*_*l_B 10
根据您在问答中所写的内容,您似乎忽略了网格布局(以及一般的网格)的一个重要概念。
一行延伸到整个网格。它不限于单个列。
所以当你写:
如果我们的右列中有三行......
没有这样的事情。所有行都存在于所有列中(反之亦然)。
这是来自开发工具的布局图:
如您所见,所有行都扩展到所有列。
第三行的高度由 中的内容设置#one,正如您指定的那样。
右列第三行必须与左列第三行高度相同,因为一行只能有一个高度。
但是,您可以调整行和列内网格区域的大小和对齐方式。
align-self: 拉伸(默认值)
对齐自我:结束
对齐自我:居中
align-self: 开始(你可能在找什么)
section {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: auto auto 1fr;
grid-template-areas:
"one top"
"one center"
"one bottom";
border: 1px solid hotpink;
}
article {
border: 1px solid green;
}
#one { grid-area: one;}
#top { grid-area: top;}
#center { grid-area: center;}
#bottom { grid-area: bottom; align-self: start; background-color: aqua;}Run Code Online (Sandbox Code Playgroud)
<section>
<article id=one><h1>One</h1><p>Lorem cool dolizzle sit amizzle, dope sizzle elizzle. Nullam shiznit velizzle, get down get down volutpizzle, suscipizzle quizzle, dizzle mammasay mammasa mamma oo sa, arcu. Pellentesque sheezy tortizzle. Sed erizzle. Fusce izzle dolor shiznit pimpin' tempizzle tempor. Maurizzle pellentesque nibh shizzlin dizzle turpizzle. Vestibulum in tortor. Pellentesque cool rhoncus black. In hac fo shizzle my nizzle check out this dictumst. Black uhuh ... yih!. Mammasay mammasa mamma oo sa tellizzle shiz, pretizzle shiznit, mattizzle fo, gangster vitae, nunc. Get down get down suscipizzle. Own yo' away izzle sed cool.Nullizzle fizzle shut the shizzle up yo mamma orci daahng dawg viverra. Phasellus nizzle shizzle my nizzle crocodizzle. Curabitizzle sure velit vizzle check out this dizzle doggy. Maecenas sapien nulla, iaculis shiz, molestie hizzle, egestas a, erizzle. Shit vitae turpis quizzle nibh bibendizzle boom shackalack. Nizzle pulvinar dope velizzle. Aliquizzle mammasay mammasa mamma oo sa volutpat. Nunc izzle its fo rizzle at lectus pretizzle faucibizzle. We gonna chung nec lacizzle own yo' fizzle pizzle ultricizzle. Ut nisl. Crunk et owned. Integer laoreet ipsum shizzlin dizzle mi. Donizzle at shiz.</p>
</article>
<article id=top><h1>Top</h1> <p>Just Two Words</p></article>
<article id=center><h1>Center</h1></article>
<article id=bottom><h1>Bottom</h1><p>Help Me! How can I get closer to my Top neighbour?</p></article>
</section>Run Code Online (Sandbox Code Playgroud)
经过更多的反复试验,我找到了一个可行的解决方案:
grid-template-rows: auto 1fr;
当最后一项的值为 时1fr,允许另一项使用 调整其大小auto。
如果我们的右列中有三行,则工作 CSS 将如下所示:
grid-template-rows: auto auto 1fr;
这样做的结果是:前两个项目 ( auto) 根据其内容进行调整,第三个项目 ( 1fr) 将填充剩下的任何垂直空间。
换句话说:您需要一个项目的值为1fr,以便其他项目能够完全灵活地使用auto。