002

How to size grid columns from a design file

Column widths copied from a design as fr units shrink every column by the same share, and the one holding short labels breaks first. Three track sizes and one padding rule decide which column gives way instead.

6 min read

A design file shows a layout at one width. Say three columns: a 240px nav, a 720px article and a 240px aside, 1200px in all. The quick way to carry that into CSS is to keep the numbers and change the unit.

.page {
  display: grid;
  grid-template-columns: 240fr 720fr 240fr;
}

At 1200px it matches the design to the pixel. On a narrower screen, every column gives up the same share. That sounds fair, and it is the problem. The three columns hold different kinds of content, and they do not all bear the same cut.

Shares and floors

An fr is a share of the free space. 240fr 720fr 240fr splits the row 1 : 3 : 1 at any width. On a 1024px screen with 20px page gutters, the row is 984px and the nav gets 197px.

The nav holds labels. A label needs one width: its words on one line, plus its icon and padding. Say the longest, "Get started", needs 230px. At 197px it wraps, and a nav that wraps looks broken. The article loses the same share, but prose rewraps and the reader never notices.

Every grid track also has a floor. A bare 240fr is shorthand for minmax(auto, 240fr), and auto lets the column shrink to its longest word. So the nav breaks between words first. It runs into the article only once a single word no longer fits.

Narrow the row and watch "Get started".

<div class="controls">
  <label>Row <input type="range" min="360" max="720" value="640" aria-label="Row width" /></label>
  <label><input type="checkbox" /> Decide who gives way</label>
  <output></output>
</div>
<div class="stage">
  <div class="row">
    <nav>
      <span>Get started</span>
      <span>Theming</span>
      <span>Dark mode</span>
      <span>Deploying</span>
    </nav>
    <main>
      <h3>Theming</h3>
      <p>Every surface reads its color from a token. Change the token, and every surface that uses it follows.</p>
    </main>
    <aside>
      <b>On this page</b>
      <span>Tokens</span>
      <span>Modes</span>
    </aside>
  </div>
</div>
One share eachEdit to run

The readout gives the three widths in order. Here the labels need 108px on one line, so below a 540px row the nav's share falls short and "Get started" wraps. Narrower still, all three columns keep shrinking at the same rate until the nav reaches its longest word. Tick the box to turn on the fix in the next section.

Decide who gives way

Each column gets its own rule.

.page {
  display: grid;
  grid-template-columns:
    minmax(max-content, 240fr)
    minmax(0, 720fr)
    calc(100% * 240 / 1200);
}

The nav: minmax(max-content, 240fr). It still takes its 240 share when there is room. Its floor is now max-content, the labels on one line, so it never gets narrower than that.

The article: minmax(0, 720fr). It takes its 720 share, and its floor is zero. This is the column built to give way: text rewraps and images scale. The zero floor also stops a long URL or code block from pushing the column wider than its share.

The aside: calc(100% * 240 / 1200). 240 out of 1200 is 20% of the row. Percentage tracks are measured against the whole row, so the aside keeps its exact share whatever the other two do.

The browser settles it in order. It sizes the percentage track, then splits what is left between the fr tracks. When a track's share comes out below its floor, the browser holds that track at its floor and splits the rest again among the others. On the 984px row:

  • The aside takes 20%: 197px.
  • The nav's share would be 197px, under its 230px floor, so it holds at 230px.
  • The article takes the rest: 557px.

At 1200px every share clears its floor, and the layout is the design's again, to the pixel.

The aside could have stayed 240fr. It would then join the second split, and the nav's extra 33px would come out of the article and the aside together. The percentage keeps the aside out of it, so the article is the only column that pays.

The column that gives way has a floor too

The article is now the column losing width, and it has limits of its own. Say its header sets an image beside a caption, and a container query decides when they fit side by side:

.article {
  container-type: inline-size;
  padding-inline: 48px;
}

@container (min-width: 480px) {
  .media {
    flex-direction: row;
  }
}

A container query measures the content box: the column's width minus its padding. On the 984px row the article is 557px wide. Less 96px of gutters, it measures 461px, under the 480px line, so the image drops below its caption. Fixing the nav pushed the article past a limit of its own.

The cheapest width on the page is the gutter. Padding is empty space, and on a narrower screen it can shrink without the reader losing any content.

@media (max-width: 1199px) {
  .article {
    padding-inline: 32px;
  }
}

The article now measures 557 − 64 = 493px, and the image stays beside its caption. From 1200px up, the gutters return to the design's 48px.

<div class="controls">
  <label>Row <input type="range" min="360" max="720" value="640" aria-label="Row width" /></label>
  <label>Gutters
    <select>
      <option value="48">48px</option>
      <option value="24">24px</option>
    </select>
  </label>
  <output></output>
</div>
<div class="stage">
  <div class="row">
    <nav>
      <span>Get started</span>
      <span>Theming</span>
      <span>Dark mode</span>
      <span>Deploying</span>
    </nav>
    <main>
      <div class="media">
        <div class="swatch"></div>
        <p>Each token holds one value per mode. Switching modes swaps the values; the rules stay as written.</p>
      </div>
    </main>
    <aside>
      <b>On this page</b>
      <span>Tokens</span>
      <span>Modes</span>
    </aside>
  </div>
</div>
Spend the gutters firstEdit to run

The query here switches at 300px. At a 620px row, 48px gutters leave the article measuring 276px, and the swatch drops below its text. Switch to 24px gutters: the article measures 324px, and they sit side by side again. Below a 580px row, even 24px gutters fall short. At that width the page should switch to its single-column layout.

The habit

Before typing fr, ask two questions of each column. What is its floor? And when the row narrows, should it give way?

  • Words that must stay whole (nav labels, tabs, buttons, prices): minmax(max-content, Nfr).
  • Content that can reflow (prose, images, charts): minmax(0, Nfr).
  • Columns that keep their share: a percentage of the row.
  • Gutters: tighten them per breakpoint before any content has to change shape.

One caution. max-content has no ceiling. A longer label, or the same label in another language, takes all the width it needs from the article. Keep labels short, and switch to one column before the article's zero floor is reached.

A design file settles one width. Every other width is a choice about which column gives way, and three tracks are enough to write that choice down.