Skip to content
TheChatArchiveThe Chat Archive
GPT 5.1 Codex Max· openaiHugging Face Frontier Reasoning Traces

Create a masonry layout using grid-template-rows: masonry for Firefox with a JavaScript…

  1. User

    Create a masonry layout using grid-template-rows: masonry for Firefox with a JavaScript fallback for other browsers that calculates item heights and uses absolute positioning or CSS columns to pack items without vertical gaps.

  2. Assistant

    Creating masonry layout with Firefox CSS and JS fallback

    Proposing a masonry layout using Firefox’s experimental grid-template-rows: masonry with a JS fallback that uses absolute positioning to manually arrange items without vertical gaps; this ensures correct order and minimal gaps across browsers. The fallback JavaScript measures item heights and positions items in the shortest column, updating on load and resize, while CSS feature-detects support and switches layouts accordingly. Below is a simple cross–browser pattern:

    • In Firefox (with layout.css.grid-template-masonry-value.enabled), you get native masonry via grid-template-rows: masonry.
    • Everywhere else, a small script measures item heights and absolutely positions them into the shortest column (preserving DOM order).
    • If you prefer a no‑JS fallback, you can swap the absolute‑positioning part for column-count/break-inside (shown in comments).
    <style>
      :root {
        --gap: 16px;
        --min: 220px;
      }
    
      .masonry {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(var(--min), 1fr));
        grid-template-rows: masonry; /* Firefox Nightly */
        gap: var(--gap);
      }
    
      /* Firefox Nightly dense packing */
      @supports (grid-template-rows: masonry) {
        .masonry > * {
          grid-column: auto;
          grid-row: auto;
        }
      }
    
      /* Fallback base styles */
      @supports not (grid-template-rows: masonry) {
        .masonry {
          display: block;
          position: relative; /* for absolute children */
        }
        .masonry > * {
          position: absolute;
          box-sizing: border-box;
        }
      }
    
      /* Optional CSS Columns fallback instead of JS (will change visual order) */
      /*
      @supports not (grid-template-rows: masonry) {
        .masonry {
          column-count: 3;
          column-gap: var(--gap);
        }
        .masonry > * {
          position: static;
          display: inline-block;
          width: 100%;
          break-inside: avoid;
          margin-bottom: var(--gap);
        }
      }
      */
    
      /* Demo item styling */
      .item {
        background: #e8f0fe;
        border-radius: 8px;
        padding: 12px;
      }
    </style>
    
    <div class="masonry" id="masonry">
      <div class="item">Short</div>
      <div class="item">A bit taller<br>Line 2</div>
      <div class="item">Even taller<br>Line 2<br>Line 3</div>
      <div class="item">Medium<br>Line 2</div>
      <div class="item">Tall<br>Line 2<br>Line 3<br>Line 4</div>
      <!-- ...more items... -->
    </div>
    
    <script>
    (function() {
      const container = document.getElementById('masonry');
      const items = Array.from(container.children);
      const gap = parseInt(getComputedStyle(document.documentElement).getPropertyValue('--gap'), 10) || 16;
    
      const supportsMasonry =
        CSS.supports('grid-template-rows', 'masonry') ||
        CSS.supports('grid-template-rows: masonry');
    
      if (supportsMasonry) return; // Firefox will handle it natively
    
      function layout() {
        const containerWidth = container.clientWidth;
        if (!items.length) return;
    
        // Compute column width from min value or actual item width
        const min = parseInt(getComputedStyle(document.documentElement).getPropertyValue('--min'), 10) || 220;
        const cols = Math.max(1, Math.floor((containerWidth + gap) / (min + gap)));
        const columnWidth = (containerWidth - (cols - 1) * gap) / cols;
    
        const colHeights = new Array(cols).fill(0);
    
        items.forEach(item => {
          item.style.width = `${columnWidth}px`;
          item.style.position = 'absolute';
    
          const { height } = item.getBoundingClientRect();
          const minCol = colHeights.indexOf(Math.min(...colHeights));
    
          const x = minCol * (columnWidth + gap);
          const y = colHeights[minCol];
    
          item.style.transform = `translate(${x}px, ${y}px)`;
    
          colHeights[minCol] = y + height + gap;
        });
    
        container.style.height = Math.max(...colHeights) + 'px';
      }
    
      // Initial layout & on resize
      window.addEventListener('load', layout);
      window.addEventListener('resize', () => {
        // simple debounce
        clearTimeout(layout._t);
        layout._t = setTimeout(layout, 100);
      });
    })();
    </script>
    

    How it works:

    • Firefox: grid-template-rows: masonry packs items without vertical gaps.
    • Other browsers: JS computes how many columns fit, tracks each column’s cumulative height, and absolutely positions each item at the current shortest column’s top and the corresponding left. The container’s height is set to the tallest column.
    • If you prefer no JS and can accept column‑based reordering, uncomment the CSS columns block and remove/ignore the JS.

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