我有两个网格嵌套在网格中.不幸的是,正确的嵌套网格.grid-3设置行的高度,以便左右网格都是相同的高度,额外的空间在具有类.right的div之间共享.如何设置右嵌套网格的行以调整内容的大小,因此它们与左嵌套行的高度相同?
div { border: 1px dotted black; } .grid-2 { display: grid; grid-template-columns: repeat(2,1fr); grid-auto-rows auto; } .grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-auto-rows auto; } .left { background-color: red; } .right { background-color: green; }
<div class="grid-2"> <div class="grid-2"> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> </div> <div class="grid-3"> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> </div> </div>
解决方法
您可以尝试minmax(min-content,max-content)
ref
div { border: 1px dotted black; } .grid-2 { display: grid; grid-template-columns: repeat(2,1fr); grid-auto-rows: minmax(min-content,max-content); } .grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-auto-rows: minmax(min-content,max-content); } .left { background-color: red; } .right { background-color: green; }
<div class="grid-2"> <div class="grid-2"> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> <div class="left">L</div> </div> <div class="grid-3"> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> <div class="right">R</div> </div> </div>