简体   繁体   English

是否可以精确居中Masonry.js内容?

[英]Is it possible to precisely center Masonry.js content?

I am trying to build a layout of one to four columns - variable per page designed on, with the Masonry plugin. 我正在尝试使用Masonry插件构建一到四列的布局-每页设计的变量。 So far I've liked how it works. 到目前为止,我已经喜欢它的工作原理。

However, it's leaving a highly annoying gap that with the configurations I cannot seem to get rid of. 但是,这留下了一个非常烦人的空白,我似乎无法摆脱这些配置。

To point out - it's already centered on the page. 要指出的是-它已经在页面上居中了。 However, within the centered block, the content appears left justified. 但是,在居中的块中,内容似乎左对齐。

砌体问题示例

The inner content has no margins either. 内部内容也没有空白。 Masonry is being initialized as: 砌体被初始化为:

$('.msgblock').masonry({
      columnWidth: '.grid-item',
      gutter: 8,
      itemSelector: '.grid-item',
      fitWidth: true,
      resize: false
}); 

I have eliminated the .msgblock width entirely in CSS, so it is being provided by the plugin. 我在CSS中完全消除了.msgblock宽度,因此由插件提供。

.msgblock {
    min-height: 100%;
    margin: 0 auto;
    z-index: 1;
}

.grid-item {
    min-height: 100%;
    padding-top: 10px;
}

Try using the flex method: 尝试使用flex方法:

.grid-item {
    min-height: 100%;
    padding-top: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
}

Update: to fill the gap you can use the stretch option. 更新:要填补这一空白,您可以使用stretch选项。 Here's a fiddle 这是一个小提琴

 .grid-item { background: ghostwhite; box-sizing: border-box; padding: 10px; display: inline-flex; min-height: 200px; align-self: stretch; flex-direction: column; flex: 1 0 auto; outline: 1px solid #dde; } .grid-item:nth-child(2n+1) { background: ghostwhite; } .grid-item:nth-child(2n) { background: #eef; } .row { width: 100%; background: gray; display: flex; flex-wrap: wrap; } 
 <div class="row"> <div class="grid-item" style='min-width:60%'>Cell 1</div> <div class="grid-item" style='min-width:35%'>Cell 2</div> <div class="grid-item" style='min-width:45%'>Cell 3</div> <div class="grid-item" style='min-width:25%'>Cell 4</div> <div class="grid-item" style='min-width:15%'>Cell 5</div> </div> 

After some research and testing, I've decided to go with the CSS Flexbox property. 经过一些研究和测试,我决定使用CSS Flexbox属性。 It does what I need to do without as much hassle as Masonry, and my browser support planing goes back for IE 11 - so it should be a better choice. 它可以做我需要做的事情,而无需像砌筑一样麻烦,并且我的浏览器支持规划可以追溯到IE 11-因此,它应该是一个更好的选择。 Doing this also eliminates some JS load from the system. 这样做还消除了系统的某些JS负载。

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

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