砌体元素上的 50% 宽度

50% width on masonry elements

目前我正在尝试使用 Masonry.desanto

将砌体布局应用到我的网站

问题是我需要前两个块的大小占总父宽度的 50%,但我无法使用此库做到这一点。秒元素总是低于第一个元素。不管我是否使用一些 css 之类的 width: calc(50% - 100px)。 请在此处查看示例:https://codepen.io/josebelisario/pen/JjXvPKL

// external js: masonry.pkgd.js

$('.grid').masonry({
  itemSelector: '.grid-item',
  columnWidth: '.grid-sizer',
  percentPosition: true,
  horizontalOrder: true,
  gutter: 15
});
* { box-sizing: border-box; }

body { font-family: sans-serif; }

/* ---- grid ---- */

.grid {
  background: #EEE;
  max-width: 1200px;
}

/* clearfix */
.grid:after {
  content: '';
  display: block;
  clear: both;
}

/* ---- grid-item ---- */

.grid-sizer,
.grid-item {
  width: calc(33.33% - 30px);
}

.grid-item {
  color: white;
  height: 120px;
  float: left;
  background: #D26;
  border: 2px solid #333;
  border-color: hsla(0, 0%, 0%, 0.5);
  border-radius: 5px;
  margin-bottom: 15px;
}

.grid-item--height2 { height: 200px; }
.grid-item--height3 { height: 260px; }
.grid-item--height4 { height: 360px; }

.item-40 { width: calc(40% - 30px); }
.item-50 { width: calc(50% - 30px); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://masonry.desandro.com/masonry.pkgd.js"></script>
<h1>Masonry - fluid columnWidth</h1>

<div class="grid">
  <div class="grid-sizer"></div>
  <div class="grid-item item-50">1</div>
  <div class="grid-item item-50">2</div>
  <div class="grid-item grid-item--height2">3</div>
  <div class="grid-item grid-item--height3">4</div>
  <div class="grid-item grid-item--height2">5</div>
  <div class="grid-item item-40">6</div>
  <div class="grid-item">7</div>
  <div class="grid-item">8</div>
  <div class="grid-item grid-item--height2">9</div>
  <div class="grid-item item-40 grid-item--height3">10</div>
  <div class="grid-item">11</div>
  <div class="grid-item grid-item--height2">12</div>
  <div class="grid-item">13</div>
  <div class="grid-item item-50 grid-item--height2">14</div>
  <div class="grid-item item-40">15</div>
  <div class="grid-item">16</div>
  <div class="grid-item grid-item--height2">17</div>
  <div class="grid-item">18</div>
  <div class="grid-item">19</div>
  <div class="grid-item grid-item--height3">20</div>
  <div class="grid-item grid-item--height2">21</div>
  <div class="grid-item">22</div>
  <div class="grid-item">23</div>
  <div class="grid-item grid-item--height2">24</div>
  <div class="grid-item">25</div>
</div>

对于您的具体示例,您需要考虑 .grid-sizer class 的最小公分母(想想分数)。 (在这种情况下,您的网格看起来应该是 6 列,其中 50% 的框占 3/6,常规 .grit-item 框占 2/6。为了安全地计算宽度砖石网格,我也从不对 % 进行四舍五入,所以你的 .grid-sizer 宽度应该是 width:calc(16.66% - 15px)

还有一件事要注意...如果您希望垂直和水平装订线 space 均匀,请从等式中减去相同的像素值(而不是 33.33% - 30px 尝试33.33% - 15px。这将使它具有真正的砖石网格外观。

我已经为下面的示例提供了更新的 CSS。将其复制并粘贴到您的 CodePen 中,您应该会看到很大的改进。

* { box-sizing: border-box; }

body { font-family: sans-serif; }

/* ---- grid ---- */

.grid {
  background: #EEE;
  max-width: 1200px;
}

/* clearfix */
.grid:after {
  content: '';
  display: block;
  clear: both;
}

/* ---- grid-item ---- */

.grid-sizer{
  width: calc(16.66% - 15px);
}
.grid-item {
  width: calc(33.33% - 15px);
}

.grid-item {
  color: white;
  height: 120px;
  float: left;
  background: #D26;
  border: 2px solid #333;
  border-color: hsla(0, 0%, 0%, 0.5);
  border-radius: 5px;
  margin-bottom: 15px;
}

.grid-item--height2 { height: 200px; }
.grid-item--height3 { height: 260px; }
.grid-item--height4 { height: 360px; }

.item-40 { width: calc(40% - 15px); }
.item-50 { width: calc(50% - 15px); }

让我知道这个答案是否适合您! :)