【问题标题】:Why does Masonry add a right margin to a layout with Twitter Bootstrap?为什么 Masonry 使用 Twitter Bootstrap 为布局添加右边距?
【发布时间】:2015-01-14 06:31:01
【问题描述】:

在使用 Masonry (v3.2.2) 时,它会添加一个不必要的右边距,这会导致 masonry 无法在页面上居中。它也可以在没有“容器流体”类的情况下执行相同的操作,而只需使用“容器”。

HTML:(我也在加载最新版本的 Masonry 和 ImagesLoaded

    <div class="container-fluid" id="content_container">
      <div class="row">
        <div class="col-sm-12">
            <div class="masonry js-masonry" data-masonry-options='{ "columnWidth": 350, "gutter": 0}'>
              <div class="item w2"><img src="http://placehold.it/350x350"></div>
              <div class="item w3"><img src="http://placehold.it/350x350"></div>
              <!-- etc...until 8 images are rendered -->
            </div>
        </div>
      </div>
    </div>

CSS:

.item {
 width: 350px;
 margin: 0px;
 float: left;}

 .item.w2 { width:  350px; }
 .item.w3 { width:  350px; }
 .item.w4 { width:  350px; }

在 Firefox 的检查元素中,它清楚地显示了这个额外的边距被添加。检查代码,没有产生任何边距/填充。请注意,较浅的蓝色是附加填充,白色区域是引导容器中的正常排水沟。理想情况下,每一侧的白色区域都相同,以砖石布局为中心。

查看图片:http://i.stack.imgur.com/YNSKh.jpg

JSFiddle:http://jsfiddle.net/jco4xrvr/2/

【问题讨论】:

  • 您需要发布您的砌体代码。一张图是不够的。另外,您使用的是什么版本的 Masonry?
  • 我已经用版本(v3.2.2)更新了原始问题。我已经提供了 HTML/css,还需要什么?图片是为了直观地展示问题。
  • 你需要提供你用来调用masonry的javascript。那个不在html或css中。此类问题的最佳选择是制作 jsfiddle 或 codepen。图片不足以回答大多数问题。
  • 感谢您的 cmets。我已经更新它以包含一个 jsfiddle。我相信我已经包含了所有必要的组件。

标签: twitter-bootstrap jquery-masonry masonry


【解决方案1】:

这是一个修复jsfiddle

基本上你需要使用 isFitWidth 并为你的砖石容器设置 css:

HTML:

 <div class="container-fluid" id="content_container">
  <div class="row">
    <div class="col-sm-12">
        <div class="masonry js-masonry" data-masonry-options='{ "columnWidth": 350, "gutter": 0,"isFitWidth": true}'>
            <div class="item w2">
                <img src="http://placehold.it/350x350">
            </div>
            <div class="item w3">
                <img src="http://placehold.it/350x350">
            </div>
            <div class="item w4">
                <img src="http://placehold.it/350x350">
            </div>
            <div class="item w4">
                <img src="http://placehold.it/350x350">
            </div>
            <div class="item w4">
                <img src="http://placehold.it/350x350">
            </div>
            <div class="item w4">
                <img src="http://placehold.it/350x350">
            </div>
            <div class="item w4">
                <img src="http://placehold.it/350x350">
            </div>
            <div class="item w4">
                <img src="http://placehold.it/350x350">
            </div>
        </div>
    </div>
</div>
</div>

添加这个 CSS:

.masonry {
margin: 0 auto;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多