Reputation: 4675
I'm using Masonry 4.2.2 from cdnjs (docs), and jQuery 3.5.0.
I have JavaScript that uses an array and loop to generate HTML grid-items
for Masonry.
The items appear, but they will not align horizontal like they're supposed to, they will only stack vertical.
https://jsfiddle.net/r8u0n6jk/
<div id="gallery" data-masonry='{ "itemSelector": ".grid-item", "columnWidth": 150 }'>
<!-- Generated HTML -->
</div>
.grid-item {
height: 100px;
margin-bottom: 1em;
padding: 1em;
background: blue;
color: white;
}
// Gallery Item Array
var gallery = ['item 1', 'item 2', 'item 3'];
// Generate Gallery List HTML
$(document).ready(Generate);
function Generate() {
var list = document.getElementById("gallery");
var html = "";
for (var i = 0; i < gallery.length; i++) {
html += '<div class=\"grid-item\">' + gallery[i] + '</div>';
}
list.innerHTML = html;
}
// Masonry
$(document).ready(function() {
$('#gallery').masonry({
itemSelector: '.grid-item',
columnWidth: 150,
isFitWidth: true,
});
});
Stacks vertical
Should align horizontal and fluidly reposition on screen size change.
Working Example without JavaScript Generated HTML:
https://jsfiddle.net/3ys5k17p
Upvotes: 0
Views: 125
Reputation: 655
Add following statement in css:
.grid-item div {
display: inline
}
Also, increase the column width:
// Masonry
$(document).ready(function() {
$('#gallery').masonry({
itemSelector: '.grid-item',
columnWidth: 500,
isFitWidth: true,
});
});
Check example: https://jsfiddle.net/uszdtpb8/
Edit: See this example too: https://jsfiddle.net/hq0zLgfm/
Upvotes: 1