我正在构建一个 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;
}
<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>
我想要的结果:
我希望#bottom 项尽可能靠近#top 项。我希望它们缩小到它们的内容大小。
实际结果:
CSS 网格不允许元素的高度小于 1fr 单位(总高度的 50%)——这取决于 #one 元素,它有很多文本。
视觉解释:我想要右边的结果,而不是左边的结果:
最佳答案
根据您在问题和答案中所写的内容,您似乎忽略了网格布局(以及一般情况下的网格)的一个重要概念。
一行横跨整个网格。它不局限于单个列。
所以当你写的时候:
If we had three rows in our right column...
没有这样的东西。所有行都存在于所有列中(反之亦然)。
这是来自开发工具的布局图:
如您所见,所有行都延伸到所有列。
第三行的高度由#one
中的内容设置,正如您指定的那样。
右列中的第三行必须与左列中的第三行的高度相同,因为一行只能有一个高度。
但是,您可以调整行和列内网格区域的大小和对齐方式。
align-self: stretch(默认值)
align-self: end
align-self: center
align-self: start(您可能正在寻找的内容)
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;}
<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>
关于css - 使 CSS Grid 行高灵活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50570356/