本文介绍了使用packery.js在父div中居中多个div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div,使用基础。里面的div,我有215 x 215 divs有我做的工作的背景图片。

I have a div that is centered using foundation. Inside that div, I have 215 x 215 divs that have background images of the work I've done.

我已经得到了包装工作正常。我的问题是,我想把这些div在其容器内。但我从来不知道有多少div将在一行,所以我不能给它们填充,因为它会搞砸,如果图像我给填充去一个新的行。

I've gotten packery to work just fine. My issue is that I want to center these divs inside their container. But I never know how many divs will be on a single row, so I can't just give them padding because it'll mess up if the image i give the padding to goes to a new row.

如何解决这个问题?

谢谢!

这是我的HTML

<div id="myWork" class="row">
    <div id="container" class="js-packery large-11 large-centered columns" data-packery-options='{ "itemSelector": ".item", "gutter": 20 }'>
        <div class="item" id="abgCapital">

        </div>
        <div class="item" id="voipInnovations">

        </div>
        <div class="item" id="payday">

        </div>
        <div class="item" id="inspira">

        </div>
    </div>
</div>

CSS

#myWork {
  margin-top: 30px;
  display: block;
  background-color: orange; }

#container {
  background-color: green; }

.item {
  width: 215px; }

#abgCapital {
  height: 215px;
  width: 215px;
  background-image: url(../img/abg_square_icon.png);
  background-size: cover;
  margin-left: auto;
  margin-right: auto; }

#voipInnovations {
  height: 215px;
  width: 215px;
  background-image: url(../img/vi_square_icon.png);
  background-size: cover; }

#payday {
  height: 215px;
  width: 215px;
  background-image: url(../img/pday_icon_square.png);
  background-size: cover; }

#inspira {
  height: 215px;
  width: 215px;
  background-image: url(../img/insp_square_icon.png);
  background-size: cover; }


推荐答案

听起来你想要div中心对齐文本。我更新了你的样式来做:

It sounds like you want the divs to flow like center-aligned text. I updated your styles to do that:

#container {
  background-color: green;
  text-align: center;
  height: auto !important;
  padding-top: 5px; }

.item {
  width: 215px;
  position: static !important;
  display: inline-block;
  margin-bottom: 5px; }

似乎在这个小提琴中工作:

And it seems to work in this fiddle:http://jsfiddle.net/fiddlerjason/58Qm3/

这篇关于使用packery.js在父div中居中多个div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:30