When using flex for creating a grid of images, how can I horizontally center the grid itself on the page? I still want the images to left-align on each row. I would like it to be dynamic to the number of elements per row.
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>
This solution will work regardless of the number of elements and width.
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>
As stated by many answer here and on the web, you'll need (at least, for now) dummies elements.
Here's a way to do it easily, without calculation, and in plain JavaScript:
.gallery-artwork
elementsThis method has an advantage: you don't need to do any calculation! You're sure to always have enough dummies (whatever the screen width, the number of elements and their amount per row) to push your elements on the left side of your container.
So here's the code!
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With