使用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;
答案 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)
justify-content: flex-start
,一样简单,但它不会居中。因此,为了维护中心对齐的flex项,我们使用 justify-content: center
。 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;
}
答案 4 :(得分:-1)