Create a responsive Beercss grid system with a Pinterest-style masonry layout using CSS…
- User
Create a responsive Beercss grid system with a Pinterest-style masonry layout using CSS Grid with row-span calculations in JS. Provide a fallback for browsers that don't support native masonry.
- Assistant
Creating Beercss Masonry Grid with CSS Grid and JS Fallback
The solution uses CSS Grid with native masonry if supported; otherwise, it applies JS to calculate row spans for a Pinterest-style masonry layout. For browsers without grid support, it falls back to a multi-column layout. The snippet integrates BeerCSS classes for styling and demonstration. Here's a minimal example that uses BeerCSS for look‑and‑feel, CSS Grid for the layout, a simple JS helper to calculate
grid-row-endspans, and progressive enhancement to fall back to native masonry if it ever becomes available. A last‑ditch column layout is also provided for browsers without CSS Grid.<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>BeerCSS Masonry Grid</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- BeerCSS --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/beercss@latest/dist/beer.min.css"> <style> :root { --masonry-min: 220px; --masonry-gap: 12px; --masonry-row: 8px; /* base row height for span calc */ } .masonry-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(var(--masonry-min), 1fr)); grid-auto-rows: var(--masonry-row); grid-auto-flow: dense; gap: var(--masonry-gap); } .masonry-item { width: 100%; display: block; box-shadow: var(--shadow-1); border-radius: 8px; overflow: hidden; background: var(--surface); /* Helpful for column fallback */ break-inside: avoid; } .masonry-item img { max-width: 100%; display: block; height: auto; } /* Progressive enhancement: use native masonry when/if supported */ @supports (grid-template-rows: masonry) { .masonry-grid { grid-template-rows: masonry; grid-auto-rows: auto; } .masonry-item { grid-row-end: auto !important; /* JS not needed */ } } /* Last resort: column-based fallback if no grid support */ @supports not (display: grid) { .masonry-grid { columns: 3 240px; column-gap: var(--masonry-gap); } .masonry-item { display: inline-block; width: 100%; } } </style> </head> <body class="beer"> <header class="app-bar primary"> <div class="bar"> <span class="title">BeerCSS Masonry</span> </div> </header> <main class="responsive"> <div class="masonry-grid" id="masonry"> <!-- Repeat these items; images just for demo --> <div class="masonry-item"> <img src="https://source.unsplash.com/random/400x320?sig=1" alt=""> <div class="padding"> <h6 class="text">Card 1</h6> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> </div> </div> <div class="masonry-item"> <img src="https://source.unsplash.com/random/400x500?sig=2" alt=""> <div class="padding"> <h6 class="text">Card 2</h6> <p>Praesent mattis magna ut consequat commodo.</p> </div> </div> <div class="masonry-item"> <img src="https://source.unsplash.com/random/400x280?sig=3" alt=""> <div class="padding"> <h6 class="text">Card 3</h6> <p>Sed sed justo venenatis, pretium dolor sed, viverra lectus. Morbi nec orci quis nibh placerat aliquet.</p> </div> </div> <div class="masonry-item"> <img src="https://source.unsplash.com/random/400x620?sig=4" alt=""> <div class="padding"> <h6 class="text">Card 4</h6> <p>Curabitur euismod ligula et magna consequat, vitae condimentum velit placerat.</p> </div> </div> <div class="masonry-item"> <img src="https://source.unsplash.com/random/400x360?sig=5" alt=""> <div class="padding"> <h6 class="text">Card 5</h6> <p>Fusce facilisis lorem eu turpis ultrices, ac vestibulum metus pretium.</p> </div> </div> <!-- …add as many as you like… --> </div> </main> <script> (() => { const grid = document.getElementById('masonry'); // Skip JS calculation if native masonry is supported if (!grid || CSS.supports('grid-template-rows', 'masonry')) return; function resizeMasonryItem(item) { const computed = getComputedStyle(grid); const rowHeight = parseFloat(computed.getPropertyValue('grid-auto-rows')); const rowGap = parseFloat(computed.getPropertyValue('gap')); // Use item content height to determine how many rows to span const itemHeight = item.getBoundingClientRect().height; const span = Math.ceil((itemHeight + rowGap) / (rowHeight + rowGap)); item.style.gridRowEnd = `span ${span}`; } function resizeAll() { grid.querySelectorAll('.masonry-item').forEach(resizeMasonryItem); } // Recalculate on load/resize and when images finish loading window.addEventListener('resize', resizeAll); grid.querySelectorAll('img').forEach(img => { if (img.complete) { resizeAll(); } else { img.addEventListener('load', resizeAll); } }); resizeAll(); })(); </script> </body> </html>How it works:
- The
.masonry-gridusesgrid-template-columns: repeat(auto-fill,minmax()), a smallgrid-auto-rowsvalue, andgrid-auto-flow: dense. - JS measures each
.masonry-item’s height and setsgrid-row-end: span nbased on the base row height + gap so items stack like Pinterest. @supports (grid-template-rows: masonry)enables native masonry if/when the browser implements it.- A column‑based fallback is provided for browsers without Grid support.
- The
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.