I have made the masonry grid gallery layout using Isotope and Vanilla JS. Everything seems to work fine, but on resizing there is some kind of the right margin on the masonry element, which is getting bigger as the viewport size gets smaller.
Everything would be fine but there is no trace of any kind of margin or padding or anything in Chrome dev tools.
I have made a blue background on the masonry element so this margin could be clearly visible...
Here is the link to the JSfiddle
CSS:
:root {
font-family: "Inter", sans-serif;
line-height: 1.5;
font-weight: 400;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
box-sizing: border-box;
scroll-behavior: smooth;
}
body {
background-color: white;
scrollbar-width: none;
width: 100%;
margin: 0;
padding: 0;
box-sizing: border-box;
}
body::-webkit-scrollbar {
display: none;
}
.masonry-grid {
margin: 0 auto;
width: 100%;
max-width: 1440px;
background-color: royalblue;
}
.masonry-grid,
.masonry-grid * {
box-sizing: border-box;
}
.hidden {
display: none !important;
}
.item {
position: relative;
width: calc(100% - 10px);
margin-bottom: 10px;
/* overflow: hidden; */
/* overflow: hidden; Ensures the pseudo-element stays within the bounds of .item */
border-radius: 20px;
/* object-fit: cover; */
/* margin: 10px 10px 0 10px; */
}
.item img {
/* max-width: 100%; */
width: 100%;
display: block;
border-radius: 20px;
transition: all 0.3s; /* Added transition for the blur effect */
/* object-fit: cover; */
}
.item-info {
display: none;
}
/* Responsive breakpoints */
@media (min-width: 600px) {
.item {
width: calc(50% - 10px);
}
}
@media (min-width: 960px) {
.item {
width: calc(33.33% - 10px);
}
}
@media (min-width: 1280px) {
.item {
width: calc(25% - 10px);
}
}
@media (min-width: 1600px) {
.item {
width: calc(20% - 10px);
}
}
JS:
let iso
window.onload = function () {
const grid = document.querySelector(".masonry-grid")
const filterButtons = document.querySelectorAll(".filter-buttons button")
// Initialize Isotope after ensuring images have loaded
imagesLoaded(grid, function () {
iso = new Isotope(grid, {
itemSelector: ".item",
layoutMode: "masonry",
containerstyle: null,
masonry: {
columnWidth: ".item",
gutter: 12,
itemSelector: ".item",
},
})
})
// Re-layout on window resize
window.addEventListener("resize", () => {
iso.layout()
// grid.style.objectFit = "cover"
})
}
Please help, I am slowly going crazy :)
THX
JR
First of all I have removed all other code from HTML, CSS and JS, hoping that some other element could be a problem. I have tried several solutions in CSS and JS:
- box-sizing, object-fit
- tried to reinitialize Isotope on window viewport size change, to "redraw" the masonry grid on viewport change
- tried inspecting with Chrome and Firefox dev tools
- Several other solutions suggested by Google search, ChatGPT, Bard....
Nothing helped :(
The margin I do see on left and right sides is the area where there is no masonry grid. To fix this, set the masonry grid to consume 100% of the width available.
In your CSS set
max-widthto 100% in themasonry-gridclass.The
margin: 0 auto;is what had centered the content, and thus margins appeared on both sides as opposed to one side. With max-width at 100%, there are no margins now.