始终填满全宽的砌体

发布于 2025-01-17 23:56:44 字数 2534 浏览 0 评论 0原文

我正在使用 Masonry(https://masonry.desandro.com) 在我的网站上显示小预览。 我希望砌体始终 100% 适合分配的区域。 为了实现这一点,项目的宽度或装订线可以改变。 我附有一个代码笔,它展示了这个问题。

https://codepen.io/bvonr/pen/GRyvEra

蓝色区域是分配的空间需要填写。 将项目居中

.grid {
  margin: auto;
}

并不是我想要实现的目标。 每行的第一项应接触分配区域的左边界,每行的最后一项应接触分配区域的右边界。 类似于Flexbox中的space- Between。 有办法做到这一点吗?

// external js: masonry.pkgd.js

$('.grid').masonry({
  itemSelector: '.grid-item',
  columnWidth: 160,
  isFitWidth: true,
  gutter: 20
});
* { box-sizing: border-box; }

body { font-family: sans-serif; }

/* ---- grid ---- */
.frame {
  width: 90%;
  height: 100%;
  background: blue;
  border-top: solid 20px blue;
}
.grid {
  width: 100%;
  max-width: 100%;
}

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

.grid-item {
  width: 160px;
  height: 120px;
  float: left;
  background: #D26;
  margin-bottom: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://masonry.desandro.com/masonry.pkgd.js"></script>
<div class="frame">
<div class="grid">
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
</div>
</div>

I am using Masonry(https://masonry.desandro.com) to display little previews on my website.
I want the masonry to always fit 100% of the allocated area.
To accomplish this, either the width of the items or the gutter can vary.
I have a codepen attached that showcases the problem.

https://codepen.io/bvonr/pen/GRyvEra

The blue area is the allocated space that needs to be filled.
Centering the items with

.grid {
  margin: auto;
}

is not what I am trying to accomplish.
The first item of each row should touch the left border of the allocated area, and the last item of each row should touch the right border of the allocated area.
Similar to space-between in flexbox.
Is there a way to accomplish this?

// external js: masonry.pkgd.js

$('.grid').masonry({
  itemSelector: '.grid-item',
  columnWidth: 160,
  isFitWidth: true,
  gutter: 20
});
* { box-sizing: border-box; }

body { font-family: sans-serif; }

/* ---- grid ---- */
.frame {
  width: 90%;
  height: 100%;
  background: blue;
  border-top: solid 20px blue;
}
.grid {
  width: 100%;
  max-width: 100%;
}

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

.grid-item {
  width: 160px;
  height: 120px;
  float: left;
  background: #D26;
  margin-bottom: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://masonry.desandro.com/masonry.pkgd.js"></script>
<div class="frame">
<div class="grid">
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
</div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文