I’m working on a project where I have a CMS Collection List displaying a gallery of images. My goal is to achieve the following layout:
- Horizontal images (landscape orientation) should span the full width of the grid (taking up a single row).
- Vertical images (portrait orientation) should display two per row, side by side in their respective columns.
- On mobile, I would like the images to be in one column.
What I’ve Done So Far
- Grid Setup:
- The parent container (
project_collection-list
) is set to Grid Layout with:- 2 columns.
- Auto rows enabled.
- Dynamic items (
project_collection-item
) are auto-placed by Webflow.
- JavaScript Logic:
- I wrote custom JavaScript to dynamically adjust the
gridColumn
property based on image orientation:
document.addEventListener(“DOMContentLoaded”, function () {
const images = document.querySelectorAll(‘.project_item-image’); // Select all images
images.forEach((img) => {
const parent = img.closest(‘.project_collection-item’); // Get the image’s wrapper
img.onload = () => {
if (img.naturalWidth > img.naturalHeight) {
parent.style.gridColumn = “1 / -1”; // Horizontal images span full width
parent.style.gridRow = “auto”; // Ensure proper row placement
} else {
parent.style.gridColumn = “span 1”; // Vertical images stay in one column
}
};
});
});
With this code the vertical images are displaying side by side in two columns as expected, but the horizontal images are not spanning the full width of the grid. Instead, they remain confined to a single column.
Any advice, insights, or alternate approaches would be greatly appreciated!
Thank you so much in advance for your help!
Here is my site Read-Only LINK: Webflow - [WIP] McCall Designs
And live version to see how the code is working: [WIP] McCall Designs