Full answer
1fr is not a fixed share of the width. The fr unit divides the free space, and a grid track has an automatic minimum size. For 1fr the track is really minmax(auto, 1fr). The auto minimum means the track will not shrink below the min-content size of its items. If an item contains something that cannot wrap, such as a long word, a wide image, a pre block, a table or an input with a fixed width, then the track grows to fit it. The column becomes wider than the container, and the page may get a horizontal scrollbar.
This is the same problem as a flex item with min-width: auto, and the fix is similar: allow the track or the item to shrink.
Fixes:
- Use
minmax(0, 1fr)instead of1fr. The zero minimum lets the track shrink below the content, and then the content can overflow the track or wrap, depending on its own rules. - Or set
min-width: 0on the grid item that holds the wide content. - Then decide what the wide content should do. Give a
preor a tableoverflow-x: autoin its own box, let long words break withoverflow-wrap: anywhere, and give imagesmax-width: 100%.
Setting only the track to minmax(0, 1fr) stops the page from stretching, but the content can then spill over. Handle the content too.
repeat(auto-fit, minmax(200px, 1fr)) has a similar trap on very narrow screens: when the container is narrower than 200px, the track cannot be smaller than 200px. Use minmax(min(200px, 100%), 1fr) to prevent it.
In the first grid, the second column grows to fit the code line, so the grid is wider than 300px. In the second grid, the column stays at 200px (300 minus 100 minus the 8px gap), and the code scrolls inside its own box.
Look at the track sizes in the grid inspector of your browser tools to see which one grew.
<style>
.layout { display: grid; grid-template-columns: 100px 1fr; width: 300px; gap: 8px; }
.fixed { grid-template-columns: 100px minmax(0, 1fr); }
pre { margin: 0; background: #eee; overflow-x: auto; }
</style>
<div class="layout" id="bad"><nav>Menu</nav><main id="m1"><pre>const a = "a very long line of code that cannot wrap at all";</pre></main></div>
<div class="layout fixed" id="good"><nav>Menu</nav><main id="m2"><pre>const a = "a very long line of code that cannot wrap at all";</pre></main></div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- 1fr is minmax(auto, 1fr).
- Long words, pre blocks and wide media set a large minimum.
- minmax(0, 1fr) lets the track shrink.
- Wide content needs its own overflow rule.
- Use min() inside minmax for auto-fit.
Common mistakes
- Setting width: 100% on the item and expecting the track to obey.
- Fixing the track with minmax(0, 1fr) but not giving wide content its own scroll or wrapping.
- Using minmax(200px, 1fr) in auto-fit with no min() for very small screens.
Follow-ups
- What is the real value of 1fr?
- How is this related to flex items and min-width: auto?
Tips
- Explain that 1fr has an automatic minimum, and what content sets it.
- Give minmax(0, 1fr) as the fix, then handle the content.