砌体-Divs不对齐

时间:2018-11-10 18:22:37

标签: jquery css masonry

我正在尝试为我网站上的产品页面创建砌体布局。有一个width: 50%;的正方形图像,然后应该有四个width: 25%的正方形图像,它们相互堆叠。

这是我到目前为止所取得的成就: https://prism-ferret-k6wy.squarespace.com/shop/

您将看到下面有两幅图像可以放入可用的空白区域。

Codepen:https://codepen.io/dwinnbrown/pen/aQmOVR

这是我的Javascript:

$('.ProductList-grid').masonry({
            itemSelector: '.ProductList-item',
            percentPosition: true
          });

和我的CSS:

.ProductList .ProductList-item:nth-child(5n-4) {
        width: 50% !important;
    }


.ProductList .ProductList-item {
        float: none !important;
        margin: 0 !important;
        width: 25% !important;
    }

它应该是这样的: enter image description here

2 个答案:

答案 0 :(得分:0)

enter image description here

HTML

<div class="container">
          <div class="grid-item">1</div>
          <div class="small-size">
                 <div class="grid-item">2</div>
                 <div class="grid-item">3</div>
                 <div class="grid-item">4</div>
                 <div class="grid-item">5</div>
          </div>
   </div>

CSS

.container {
  max-width: 1200px;
  margin: 0 auto;
}

.grid-item:first-of-type {
  background-color: pink !important;
  width: calc(50% - 10px);
  height: 300px;
}

.grid-item:nth-child(odd) {
  background-color: green;
}
.grid-item:nth-child(even) {
  background-color: red;
}

.small-size {
 display: flex;
 flex-wrap: wrap;
 width: 50%;
}

.small-size .grid-item {
  width: 50%;
  height: 150px;
}

JS

$(document).ready(function() {
  $('.container').masonry({
    percentPosition: true,
    gutter: 10
  });
});

答案 1 :(得分:0)

使用网格大小确定列宽也可以: https://codepen.io/anon/pen/pQEJXV

<div class="container">
  <!-- width of .grid-sizer used for columnWidth -->
  <div class="grid-sizer"></div>
  <div class="grid-item grid-item--width2 grid-item--height2">1
  </div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>
  <div class="grid-item">4</div>  
  <div class="grid-item">5</div>
</div>

CSS

* { box-sizing: border-box; }
.container {
  max-width: 1200px;
  margin: auto;
}

.grid-item:first-of-type {
  background-color: pink !important;
}

.grid-sizer,
.grid-item {
  width: 25%;
}

.grid-item {
  height: 150px;
  float: left;
}

.grid-item--width2 {
  width:50%;
}

.grid-item--height2{
  height:300px;
}

.grid-item:nth-child(odd) {
  background-color: green;
}
.grid-item:nth-child(even) {
  background-color: orange;
}

JS

$(document).ready(function() {
  $('.container').masonry({
    itemSelector: '.grid-item',
    columnWidth: '.grid-sizer',
    percentPosition: true
  });
});

响应式布局的文档:https://masonry.desandro.com/layout.html#responsive-layouts

enter image description here