简体   繁体   English

如何创建此 css 网格

[英]How to create this css grid

I want to acheive this design any next category will automatically adjust to the previous div.我想实现这个设计,任何下一个类别都会自动调整到前一个 div。 Every category have not specific product but need to just after the previous product like we use float left or display-flex But don't want to create multiple css for responsive.每个类别都没有特定的产品,但需要像我们使用float left 或 display-flex一样在上一个产品之后但不想创建多个 css 来响应。 Because it has more than 25 categories.因为它有超过25个类别。 ` `

 .offerpage.product-box { margin: 0 5px; }.categoryProductsWrap{ width: 100%; }.product-img{ width: 100%; }.categoryProductsWrap.categoryProducts { display: flex; flex-wrap: wrap; width: 100%; }.categoryProductsWrap.categoryProducts.product-box { margin-bottom: 10px; float: left; }.offerpage.categoryName { color: #161719; font-size: 24px; font-weight: bold; margin-bottom: 0.5em; border-bottom: solid 1px #dddddd; padding-bottom: 0.125em; }.img-box{ position: relative; padding-top: 124%; display: inline-block; width: 100%; vertical-align: top; }.product-box.product-img{ position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; }.img-responsive { display: block; max-width: 100%; height: auto; }.txt-content { overflow: hidden; position: relative; }.img-box{ position: relative; padding-top: 124%; display: inline-block; width: 100%; vertical-align: top; }.offerpage{ display: flex; flex-wrap: wrap; }.categoryProductsWrap.categoryProducts.product-box{ width: 33.333% } @media (min-width: 1200px){.categoryProductsWrap.cooling_fan { width: 50%; }.categoryProductsWrap.accessories { width: 50%; } } @media (min-width: 1440px){.categoryProductsWrap.accessories.categoryProducts.product-box, .categoryProductsWrap.cooling_fan.categoryProducts.product-box { width: calc(50% - 10px); } }
 <div class="offerpage"> <div class="categoryProductsWrap accessories"> <div class="categoryName">Accessories</div> <div class="categoryProducts clearfix offerzone-slider products-wrapper"> <div class="product-box"> <div class="product-inner-box"> <div class="txt-content"> <div class="img-box"> <a href="#" class="product-thumbnail"> <img class="product-img img-responsive" src="https://picsum.photos/400"> </a> </div> </div> </div> </div> <div class="product-box"> <div class="product-inner-box"> <div class="txt-content"> <div class="img-box"> <a href="#" class="product-thumbnail"> <img class="product-img img-responsive" src="https://picsum.photos/400"> </a> </div> </div> </div> </div> </div> </div> <div class="categoryProductsWrap cooling_fan"> <div class="categoryName">Cooling Fan</div> <div class="categoryProducts clearfix offerzone-slider products-wrapper"> <div class="product-box"> <div class="product-inner-box"> <div class="txt-content"> <div class="img-box"> <a href="#" class="product-thumbnail"> <img class="product-img img-responsive" src="https://picsum.photos/400"> </a> </div> </div> </div> </div> <div class="product-box"> <div class="product-inner-box"> <div class="txt-content"> <div class="img-box"> <a href="#" class="product-thumbnail"> <img class="product-img img-responsive" src="https://picsum.photos/400"> </a> </div> </div> </div> </div> </div> </div> <div class="categoryProductsWrap asus"> <div class="categoryName">Asus</div> <div class="categoryProducts clearfix offerzone-slider products-wrapper"> <div class="product-box"> <div class="product-inner-box"> <div class="txt-content"> <div class="img-box"> <a href="#" class="product-thumbnail"> <img class="product-img img-responsive" src="https://picsum.photos/400"> </a> </div> </div> </div> </div> </div> </div> <div class="categoryProductsWrap audio"> <div class="categoryName">Audio Device</div> <div class="categoryProducts clearfix offerzone-slider products-wrapper"> <div class="product-box"> <div class="product-inner-box"> <div class="txt-content"> <div class="img-box"> <a href="#" class="product-thumbnail"> <img class="product-img img-responsive" src="https://picsum.photos/400"> </a> </div> </div> </div> </div> <div class="product-box"> <div class="product-inner-box"> <div class="txt-content"> <div class="img-box"> <a href="#" class="product-thumbnail"> <img class="product-img img-responsive" src="https://picsum.photos/400"> </a> </div> </div> </div> </div> </div> </div> </div>
` `

If you want to use CSS grid, there no big difficulties simply check the documentation to understand the main CSS properties you need to add.如果您想使用 CSS 网格,没有太大困难,只需查看文档以了解您需要添加的主要 CSS 属性。
Also check this guide on CSS grid to undertand more and see examples.另请查看CSS 网格上的本指南以了解更多信息并查看示例。

Quick help快速帮助

If I understand correctly you want 3 items max per row, to do that you will need to set:如果我理解正确,您希望每行最多 3 个项目,为此您需要设置:

grid-template-columns: repeat(3, 1fr);

The equivalent to flex-direction: row;相当于flex-direction: row; is grid-auto-flow: row;grid-auto-flow: row;

You will I presume also need a gap (spacing between items), something like that:我认为您还需要一个间隙(项目之间的间距),例如:

gap: 10px;

So a good starting point will be:所以一个好的起点是:

display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-flow: row;
gap: 10px;

I leave you do the rest according to your needs.我让你根据你的需要做 rest。

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

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