水平居中的Flex容器

时间:2016-02-09 15:59:15

标签: html css css3 flexbox

使用flex创建图像网格时,如何在网页上水平居中网格?我仍然希望图像在每一行上左对齐。我希望每行的元素数量是动态的。

Jsfiddle with what I have so far



.gallery {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-content: flex-start;
  align-items: flex-start;
}

.gallery-artwork {
  width: 400px;
  display: flex;
  margin: 10px;
}

<div class="gallery">
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
</div>
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:2)

无论元素数量和宽度如何,此解决方案都能正常工作。

https://jsfiddle.net/p01mx39h/14/

function addDummies() {
  imgsPerRow = Math.floor($(".gallery").width() / $(".gallery-artwork").outerWidth(true));
  if (imgsPerRow > 1) {
    missingImages = imgsPerRow - $(".gallery-artwork").length % imgsPerRow;
    while (missingImages > 0) {
      $(".gallery").append("<div class='gallery-artwork-dummy'></div>");
      $(".gallery-artwork-dummy").css('width', $('.gallery-artwork').outerWidth(true));
      missingImages--;
    }
  }
}

$(document).ready(function() {
	addDummies();
});
.gallery {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  align-content: flex-start;
  align-items: center;
}

.gallery-artwork {
  width: 400px;
  display: flex;
  margin: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gallery">
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
</div>

答案 1 :(得分:1)

正如许多人在网上和网上回答的那样,你需要(至少现在)虚拟元素。
这是一种轻松实现的方法,无需计算,只需简单的JavaScript:

  • 添加与.gallery-artwork元素
  • 的数量相同的虚拟对象
  • 给这些假人提供相同的风格,没有任何高度,也没有垂直边距

此方法有一个优点:您无需进行任何计算!您确定总是有足够的假人(无论屏幕宽度,元素数量和每行的数量)将元素推到容器的左侧。

所以这是代码!

var gallery = document.querySelector('.gallery'),
    artworks = gallery.querySelectorAll('.gallery-artwork'),
    dummy = document.createElement('div');
dummy.classList.add('gallery-dummy');

// One element = one dummy
[].forEach.call(artworks, function() {
  gallery.appendChild(dummy.cloneNode());
});
.gallery {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}
.gallery-artwork {
  width: 400px;
  margin: 10px;
}
.gallery-dummy {
  width: 400px;
  margin: 0 10px;
  height: 0;
}
<div class="gallery">
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
  <div class="gallery-artwork">
    <img src="http://placehold.it/400x225">
  </div>
</div>

答案 2 :(得分:0)

  • 你可以围绕整个事物包裹一个元素,然后像往常一样居中。
  • 将flex项目左对齐的问题就像使用默认值justify-content: flex-start一样简单,但它不会居中。因此,为了维护中心对齐的flex项,我们使用 justify-content: center
  • 它完美居中但如果第二行的弹性项目数量较少,那么它看起来就像 this
  • 为了保持水平居中的所有内容,对最后一行有一个很好的左对齐行为,我们可以添加一个额外的flex项目并将其赋予:visibility: hidden 。如果弹性项的数量是动态的,我们使用以下选择器:.gallery-artwork:last-of-type { visibility: hidden; }

这是Demo

<强> CSS

/* The wrap element that surrounds and centers everything */
.exhibition {
  padding: 10px;
  margin: 20px auto;
}
/* justify-content: center is needed for centering of the flex-items */   
.gallery {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
}

.gallery-artwork {
  width: 400px;
  margin: 10px;
}
 /* This is the last of the gallery-artwork (flex-item). 
 Being of the same dimensions as the other flex-items and hidden from site,
fills in the gap of an uneven row of flex-items. */

.gallery-artwork:last-of-type {
  visibility: hidden;
}

<强> HTML

<section class="exhibition">
  <div class="gallery">
    <div class="gallery-artwork">
      <img src="http://placehold.it/400x225">
    </div>
    <div class="gallery-artwork">
      <img src="http://placehold.it/400x225">
    </div>
    <div class="gallery-artwork">
      <img src="http://placehold.it/400x225">
    </div>
    <div class="gallery-artwork">
      <img src="http://placehold.it/400x225">
    </div>
    <div class="gallery-artwork">
      <img src="http://placehold.it/400x225">
    </div>
    <div class="gallery-artwork">
      <img src="http://placehold.it/400x225">
    </div>
  </div>
</section>

答案 3 :(得分:0)

这是您更新的CSS:

  Foo foo = new Foo();

  foo.Bar = 123;
  foo.Bar = "Nothing";

  String test = foo.Bar;
  Byte testByte = foo.Bar;   

jQuery的:

.gallery {
  display: flex;
  flex-flow: row wrap;
  justify-content:center;
}

.gallery-artwork {
  width: 400px;
  display: flex;
  flex-flow: column;
  margin: 0.5em;
}

小提琴:https://jsfiddle.net/debraj/p01mx39h/9/

答案 4 :(得分:-1)

略微改变你的画廊css:

justify-content: center;

我今天用过这个,所以这就是我立刻把它弹出来的原因。但是这个CSS-tricks页真的帮助了灵活点点滴滴。