简体   繁体   English

将网格项目设置为相同的高度

[英]Set grid items to be same height

这种四张牌布局 I have quite a specific issue with grid.我对网格有一个非常具体的问题。 I was trying to make with all grid items the same size.我试图使所有网格项目的大小相同。 stretch is not an option in this case, because some of the items span across two rows.在这种情况下, stretch不是一个选项,因为一些项目跨越两行。 When I added align-items: center , that didn't solve the issue.当我添加align-items: center时,这并没有解决问题。 I really didn't find anything to be of help.我真的没有找到任何可以帮助的东西。

 #cards { padding: 0 7vw; margin-top: 3vh; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2, 1fr); align-items: center; justify-content: center; }.card:nth-child(1), .card:nth-child(3) { grid-row: span 2; }.card { display: block; border: solid 5px #000; box-shadow: 0 20px 30px -15px hsl(215, 49%, 82%); border-radius: 8px; margin: 5%; overflow: hidden; text-align: center; }
 <div id="cards"> <div class="card"> <div class="card-content"> <h2 class="card-title">Item 1</h2> </div> </div> <div class="card"> <div class="card-content"> <h2 class="card-title">Item 2</h2> </div> </div> <div class="card"> <div class="card-content"> <h2 class="card-title">Item 3</h2> <p>Hello world</p> </div> </div> <div class="card"> <div class="card-content"> <h2 class="card-title">Item 4</h2> <p>I built this two line paragraph oh yes I really want it to be two lines or yes finally.</p> </div> </div> </div>

Thank you very much for your ideas.非常感谢您的想法。 (I am new on Stack Overflow so feel free to give any other suggestions to help me further improve the question) (我是 Stack Overflow 上的新手,所以请随时提供任何其他建议来帮助我进一步改进问题)

You can add more rows like this:您可以像这样添加更多行:

grid-template: ".   two   ." 1fr
               "one two  three" 1fr
               "one four three" 1fr
               ".   four  ." 1fr / 
               1fr  1fr  1fr;

With the appropriate grid-area on children.在儿童上使用适当的grid-area

Here is the snippet (I have also added a height: 90%; on children to make them fit the cells height but keeping the margin):这是片段(我还添加了一个height: 90%;在孩子身上,以使它们适合单元格高度但保持边距):

 #cards { padding: 0 7vw; margin-top: 3vh; display: grid; grid-template: ". two." 1fr "one two three" 1fr "one four three" 1fr ". four." 1fr / 1fr 1fr 1fr; align-items: center; justify-content: center; }.card:nth-child(1){ grid-area: one; }.card:nth-child(2){ grid-area: two; }.card:nth-child(3){ grid-area: three; }.card:nth-child(4){ grid-area: four; }.card { display: block; border: solid 5px #000; box-shadow: 0 20px 30px -15px hsl(215, 49%, 82%); border-radius: 8px; margin: 5%; overflow: hidden; text-align: center; height: 90%; }
 <div id="cards"> <div class="card"> <div class="card-content"> <h2 class="card-title">Item 1</h2> </div> </div> <div class="card"> <div class="card-content"> <h2 class="card-title">Item 2</h2> </div> </div> <div class="card"> <div class="card-content"> <h2 class="card-title">Item 3</h2> <p>Hello world</p> </div> </div> <div class="card"> <div class="card-content"> <h2 class="card-title">Item 4</h2> <p>I built this two line paragraph oh yes I really want it to be two lines or yes finally.</p> </div> </div> </div>

Try this: I had tried many units and when % worked, I just used it试试这个:我尝试了很多单位,当 % 工作时,我只是使用它

 #cards { padding: 0 7vw; margin-top: 3vh; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2, 1fr); align-items: center; justify-content: center; }.card:nth-child(1), .card:nth-child(3) { grid-row: span 2; }.card { display: block; border: solid 5px #000; box-shadow: 0 20px 30px -15px hsl(215, 49%, 82%); border-radius: 8px; margin: 5%; overflow: hidden; text-align: center; height: 90%; }.card:nth-child(1), .card:nth-child(3) { height: 45%; }
 <div id="cards"> <div class="card"> <div class="card-content"> <h2 class="card-title">Item 1</h2> </div> </div> <div class="card"> <div class="card-content"> <h2 class="card-title">Item 2</h2> </div> </div> <div class="card"> <div class="card-content"> <h2 class="card-title">Item 3</h2> <p>Hello world</p> </div> </div> <div class="card"> <div class="card-content"> <h2 class="card-title">Item 4</h2> <p>I built this two line paragraph oh yes I really want it to be two lines or yes finally.</p> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM