简体   繁体   English

CSS 网格溢出

[英]CSS Grid overflowing

I am trying for a couple of hours to make this work, but I still failed.我尝试了几个小时来完成这项工作,但我仍然失败了。 I need to put labels in the grid, but maximally 3 columns.我需要在网格中放置标签,但最多 3 列。 If it doesn't fit, then wrap it to the next row.如果不合适,则将其包装到下一行。 If it is possible items can reorder to fit as many as possible to row.如果有可能,项目可以重新排序以适应尽可能多的行。

I made a code snippet here:我在这里做了一个代码片段:

 body { background-color: #555; margin: 0; padding: 0; }.main-wrapper { background-color: #fff; margin: auto; max-width: 280px; } label { white-space: nowrap; }.wrapper { padding-bottom: 50px; display: grid; */ grid-gap: 10px; grid-template-columns: repeat(auto-fit, minmax(max-content, 30%)); grid-auto-rows: minmax(min-content, max-content); }
 <div class="main-wrapper"> <div class="wrapper"> <label><input type="checkbox"/>Bafang M500</label> <label><input type="checkbox"/>Bafang MaxDrive</label> <label><input type="checkbox"/>Bosch Active Plus</label> <label><input type="checkbox"/>Bosch Performance</label> <label><input type="checkbox"/>Bosch Performance CX</label> <label><input type="checkbox"/>OLI SPORT</label> <label><input type="checkbox"/>Shimano StePS E6000</label> <label><input type="checkbox"/>Shimano STePS E6100</label> <label><input type="checkbox"/>Yamaha PW-ST</label> </div> <div class="wrapper"> <label><input type="checkbox"/>500<</label> <label><input type="checkbox"/>500</label> <label><input type="checkbox"/>400</label> <label><input type="checkbox"/>300</label> <label><input type="checkbox"/>200</label> <label><input type="checkbox"/>100</label> <label><input type="checkbox"/><100</label> </div> </div>

just the minmax, it works on different size.只是最小最大值,它适用于不同的尺寸。

  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));

I think @gregory-broyer answer is appliable for the upper wrapper, but not fully for the lower.我认为@gregory-broyer 的答案适用于上层包装,但不完全适用于下层。 But I define a 2nd class for 2n wrapper, and set grid-template-columns to the original.但是我为 2n 包装器定义了第二个 class,并将grid-template-columns设置为原始值。

The demo is available here:该演示可在此处获得:

 body { background-color: #555; margin: 0; padding: 0; }.main-wrapper { background-color: #fff; margin: auto; max-width: 280px; } label { white-space: nowrap; }.wrapper { padding-bottom: 50px; display: grid; */ grid-gap: 10px; //grid-template-columns: repeat(auto-fit, minmax(max-content, 30%)); grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); grid-auto-rows: minmax(min-content, max-content); }.wrapper-h{ grid-template-columns: repeat(auto-fit, minmax(max-content, 30%)); }
 <div class="main-wrapper"> <div class="wrapper"> <label><input type="checkbox"/>Bafang M500</label> <label><input type="checkbox"/>Bafang MaxDrive</label> <label><input type="checkbox"/>Bosch Active Plus</label> <label><input type="checkbox"/>Bosch Performance</label> <label><input type="checkbox"/>Bosch Performance CX</label> <label><input type="checkbox"/>OLI SPORT</label> <label><input type="checkbox"/>Shimano StePS E6000</label> <label><input type="checkbox"/>Shimano STePS E6100</label> <label><input type="checkbox"/>Yamaha PW-ST</label> </div> <div class="wrapper wrapper-h"> <label><input type="checkbox"/>500<</label> <label><input type="checkbox"/>500</label> <label><input type="checkbox"/>400</label> <label><input type="checkbox"/>300</label> <label><input type="checkbox"/>200</label> <label><input type="checkbox"/>100</label> <label><input type="checkbox"/>&lt100</label> </div> </div>

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

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