我使用JQuery Packery,它运行良好。
我想到的问题是如何将项目容器居中放置在页面上?
<div id="container">
<div class="item">...</div>
<div class="item w2">...</div>
<div class="item">...</div>
...
</div>
我试过css:
#container {
marin: 0 auto;
}
但这并不奏效。
DeSandro已经有了一个居中包装布局的例子。
例子就在这里。
基本上,它只是通过CSS居中。我也在这里复制代码:
HTML:
<h1>Packery - centered</h1>
<div class="packery js-packery" data-packery-options='{ "gutter": ".gutter-sizer", "itemSelector": ".item", "columnWidth": ".grid-sizer" }'>
<div class="gutter-sizer"></div>
<div class="grid-sizer"></div>
<div class="item w2"></div>
<div class="item"></div>
<div class="item w2 h2"></div>
<div class="item"></div>
<div class="item w2"></div>
<div class="item"></div>
<div class="item h2"></div>
<div class="item"></div>
<div class="item h2"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item h2"></div>
<div class="item"></div>
<div class="item"></div>
</div>
CSS:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.packery {
background: #FDD;
background: hsla(45, 100%, 40%, 0.2);
max-width: 480px;
margin: 0 auto;
}
/* clearfix */
.packery:after {
content: ' ';
display: block;
clear: both;
}
.gutter-sizer {
width: 2%;
}
.grid-sizer {
width: 18.4%;
}
.item {
width: 18.4%;
height: 60px;
float: left;
background: #C09;
border: 4px solid #333;
border-color: hsla(0, 0%, 0%, 0.3);
}
.item.w2 { width: 38.8%; background: #9C0; }
.item.h2 { height: 120px; background: #0C9; }
看看这个。。这正是你所需要的。。
http://codepen.io/anon/pen/Dbpzu