Skip to content
TheChatArchiveThe Chat Archive
Grok 4.1 Fast· xaiHugging Face Frontier Reasoning Traces

Create a CSS-only masonry layout that adapts to dynamic content sizes.

  1. User

    Create a CSS-only masonry layout that adapts to dynamic content sizes.

  2. Assistant
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>CSS-Only Masonry Layout</title>
      <style>
        body {
          font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
          margin: 0;
          padding: 2rem;
          background-color: #f5f5f5;
          line-height: 1.5;
        }
    
        .masonry {
          /* Key properties for masonry effect using CSS Columns */
          columns: 18em; /* Adaptive: creates columns based on width (e.g., ~300px wide) */
          column-gap: 2rem;
          column-fill: balance; /* Balances column heights for better distribution */
          column-rule: 1px solid #eee; /* Optional subtle divider between columns */
          
          /* Ensure container doesn't overflow */
          max-width: 1400px;
          margin: 0 auto;
        }
    
        .masonry__item {
          /* Prevent item from breaking across columns */
          break-inside: avoid;
          page-break-inside: avoid;
          /* Add spacing below each item */
          margin-bottom: 2rem;
          /* Fill the full column width */
          width: 100%;
          /* Smooth transitions for dynamic changes */
          transition: transform 0.2s ease, box-shadow 0.2s ease;
        }
    
        .masonry__item:hover {
          transform: translateY(-4px);
          box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
        }
    
        .masonry__item img {
          /* Images adapt and trigger reflow on load */
          width: 100%;
          height: auto;
          border-radius: 8px;
          display: block;
        }
    
        .masonry__item h3 {
          margin: 1rem 0 0.5rem;
          color: #333;
          font-size: 1.25rem;
        }
    
        .masonry__item p {
          color: #666;
          margin: 0;
        }
    
        /* Responsive adjustments */
        @media (max-width: 768px) {
          .masonry {
            columns: 14em;
            column-gap: 1.5rem;
            padding: 0 1rem;
          }
        }
    
        @media (max-width: 480px) {
          .masonry {
            columns: 100%; /* Single column on mobile */
            column-gap: 0;
          }
          
          .masonry__item {
            margin-bottom: 1.5rem;
          }
        }
      </style>
    </head>
    <body>
      <div class="masonry" id="masonry">
        <!-- Sample dynamic content: varying heights, images that load async -->
        <article class="masonry__item">
          <img src="https://picsum.photos/400/250?random=1" alt="Random image 1" loading="lazy">
          <h3>Short Item</h3>
          <p>Brief description. Heights adapt automatically.</p>
        </article>
        
        <article class="masonry__item">
          <img src="https://picsum.photos/400/400?random=2" alt="Random image 2" loading="lazy">
          <h3>Taller Content</h3>
          <p>This item has more text and a taller image, demonstrating how the layout packs items into the shortest available column without JavaScript. CSS reflow handles dynamic size changes (e.g., lazy-loaded images).</p>
        </article>
        
        <article class="masonry__item">
          <img src="https://picsum.photos/400/300?random=3" alt="Random image 3" loading="lazy">
          <h3>Medium Height</h3>
          <p>Balanced content with image. Fully responsive.</p>
        </article>
        
        <article class="masonry__item">
          <h3>Text-Only Item</h3>
          <p>No image, just text. Varying content lengths are handled seamlessly as the browser reflows the layout on resize or content change.</p>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
        </article>
        
        <article class="masonry__item">
          <img src="https://picsum.photos/400/500?random=4" alt="Random image 4" loading="lazy">
          <h3>Very Tall</h3>
          <p>Tall item pushes to next column if needed.</p>
        </article>
        
        <article class="masonry__item">
          <img src="https://picsum.photos/400/200?random=5" alt="Random image 5" loading="lazy">
          <h3>Short Image</h3>
          <p>Compact item fills gaps efficiently.</p>
        </article>
        
        <!-- Add more items for demo -->
        <article class="masonry__item">
          <img src="https://picsum.photos/400/350?random=6" alt="Random image 6" loading="lazy">
          <h3>Dynamic Demo</h3>
          <p>Images load lazily; layout reflows automatically when sizes are known. No JS required!</p>
        </article>
        
        <article class="masonry__item">
          <h3>Another Text Item</h3>
          <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
        </article>
      </div>
    
      <script>
        // Optional: Demo dynamic content addition (pure CSS handles reflow)
        // Uncomment to test adding items dynamically
        /*
        setTimeout(() => {
          const masonry = document.getElementById('masonry');
          const newItem = document.createElement('article');
          newItem.className = 'masonry__item';
          newItem.innerHTML = `
            <img src="https://picsum.photos/400/450?random=7" alt="Dynamic image" loading="lazy">
            <h3>New Dynamic Item</h3>
            <p>Added after page load. CSS columns reflow instantly!</p>
          `;
          masonry.appendChild(newItem);
        }, 2000);
        */
      </script>
    </body>
    </html>
    

    How It Works (CSS-Only Masonry)

    • CSS Columns: columns: 18em creates flexible columns (width-based, responsive). Items flow sequentially into columns, mimicking masonry.
    • Dynamic Adaptation:
      • Varying heights (text/images) are handled by break-inside: avoid—tall items stay intact and flow to the next column.
      • Image loading/reflow: Lazy-loaded images trigger automatic browser reflow when dimensions are known.
      • Content changes: Adding/removing items or resizing text/images causes instant CSS reflow (no JS needed).
    • Responsiveness: Media queries adjust column width; stacks to single column on mobile.
    • Performance: Native CSS—no layout thrashing, works in all modern browsers (IE10+ with prefixes if needed).
    • Limitations: Sequential fill (not perfect "shortest column" like JS masonry), but excellent for most use cases. For experimental true masonry, see CSS Grid masonry (Firefox dev flag only).

    Copy-paste into an HTML file to test. Resize window or wait for images to see adaptation!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.