CSS Grid Overflow — Why Content Escapes and How to Fix It

Quick answer

💡1fr in CSS Grid has an implicit minimum of auto (content size), not zero. When content is wider than the column, the column expands past the grid boundary. Replace 1fr with minmax(0, 1fr) to allow the column to shrink freely. Also add max-width: 100% to images and overflow-wrap: break-word for long text.

Error symptoms

  • A grid column is wider than it should be and pushes past the container edge
  • The page scrolls horizontally on mobile but not on desktop
  • An image inside a grid cell extends beyond its column boundary
  • A long URL or code snippet overflows the grid track
  • Adding content to one cell makes the entire column wider
  • repeat(auto-fit, minmax(200px, 1fr)) overflows on narrow screens

Common causes

  • 1fr columns have an implicit minimum of auto — they do not shrink below content width
  • Images retain their natural pixel dimensions inside grid tracks without max-width: 100%
  • Long words, URLs, or code strings do not break automatically — overflow-wrap needed
  • The minmax minimum in auto-fit is too large for the screen width causing overflow
  • A grid item has a fixed pixel width wider than its column track
  • Nested grids inherit the wrong width from a parent with overflow issues

When it happens

  • Displaying user-generated content where text length is unpredictable
  • Rendering code blocks or terminal output inside grid layout cards
  • Placing images from a CMS that have not been resized or constrained
  • Building a responsive grid that uses a minmax minimum too large for small screens

Examples and fixes

A grid with two 1fr columns where content in one column forces both columns wider.

1fr column overflowing due to auto minimum

❌ Wrong

<div class="two-col-layout">
  <aside class="sidebar">Short sidebar</aside>
  <main class="main-area">
    <pre>very-long-unbroken-content-string-that-exceeds-column-width</pre>
  </main>
</div>

/* CSS */
.two-col-layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

✅ Fixed

<div class="two-col-layout">
  <aside class="sidebar">Short sidebar</aside>
  <main class="main-area">
    <pre>very-long-unbroken-content-string-that-exceeds-column-width</pre>
  </main>
</div>

/* CSS */
.two-col-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 1rem;
}
.main-area pre {
  overflow-x: auto;
}

The specification states that 1fr is equivalent to minmax(auto, 1fr). The auto minimum resolves to the content's intrinsic size — the full width of the pre element's text. This pushes both columns wider than their expected 50% width. Changing to minmax(0, 1fr) sets the minimum to zero, allowing the column to shrink below content size. The pre element receives overflow-x: auto to handle the long text with a scrollbar rather than breaking the layout.

Images inside grid cells retain their natural dimensions and break out of the column boundary.

Image overflowing its grid column

❌ Wrong

<div class="photo-gallery">
  <figure class="gallery-item">
    <img src="landscape.jpg" alt="Mountain landscape">
    <figcaption>Mountain view</figcaption>
  </figure>
  <figure class="gallery-item">
    <img src="portrait.jpg" alt="City skyline">
    <figcaption>City view</figcaption>
  </figure>
</div>

/* CSS */
.photo-gallery {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1rem;
}

✅ Fixed

<div class="photo-gallery">
  <figure class="gallery-item">
    <img src="landscape.jpg" alt="Mountain landscape">
    <figcaption>Mountain view</figcaption>
  </figure>
  <figure class="gallery-item">
    <img src="portrait.jpg" alt="City skyline">
    <figcaption>City view</figcaption>
  </figure>
</div>

/* CSS */
.photo-gallery {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem;
}
.gallery-item img {
  width: 100%;
  height: 240px;
  object-fit: cover;
  display: block;
}

Images have an intrinsic size based on their pixel dimensions. Without explicit width constraints, an image inside a grid cell will display at its natural size and force the column to expand. Setting width: 100% on the img element makes it fill its containing column exactly. height: 240px combined with object-fit: cover ensures a consistent thumbnail height — the image is cropped to fill the rectangle without distortion. display: block removes the default inline bottom gap.

A responsive grid whose minimum track size is too large for small viewports causes horizontal scroll.

auto-fit minmax overflowing on narrow screen

❌ Wrong

<div class="product-cards">
  <div class="card">Product A</div>
  <div class="card">Product B</div>
  <div class="card">Product C</div>
</div>

/* CSS */
.product-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 1.5rem;
}

✅ Fixed

<div class="product-cards">
  <div class="card">Product A</div>
  <div class="card">Product B</div>
  <div class="card">Product C</div>
</div>

/* CSS */
.product-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(320px, 100%), 1fr));
  gap: 1.5rem;
}

minmax(320px, 1fr) means each column is always at least 320px wide. On a 375px phone, two columns would need 640px minimum — wider than the screen. The browser creates two columns anyway because auto-fit asks for the best fit, causing overflow. The fix uses min(320px, 100%) as the minimum: on screens wider than 320px, 320px is used; on narrower screens, 100% is used, allowing a single column that fits the viewport. This is the modern technique for truly responsive grids.

Why 1fr does not prevent overflow

The CSS Grid specification defines 1fr as shorthand for minmax(auto, 1fr). The auto minimum means the column will not shrink below the intrinsic minimum content size of its items. For a column containing a paragraph of normal text, auto resolves to a reasonable small width because words can break at spaces. For a column containing a pre element, a very long URL, or an image with no width constraint, auto resolves to the full content width — which may be larger than the column track you intended.

This behavior exists for a good reason: it prevents text content from being crushed so narrow that words are illegible. But it becomes a problem when you have explicit content like code blocks, images, or user-submitted text that should scroll or clip rather than expand the column. The solution is to opt out of the auto minimum by using minmax(0, 1fr) explicitly. A zero minimum tells the grid algorithm that it may shrink the column all the way to zero if needed.

Images are a separate class of overflow source. By default, images are inline-block elements with their natural pixel dimensions. Inside a grid cell, an image will display at its intrinsic size — a 1200px image placed in a 400px column will overflow by 800px. Adding width: 100% to img elements inside grid items is one of the most important global CSS rules in any project that displays images within a grid layout.

Long strings without space characters — URLs, API keys, code identifiers — also cause column overflow. The browser cannot break a string without whitespace to insert a line break. The CSS property overflow-wrap: break-word instructs the browser to break long words at the column boundary if no natural break point exists. Combined with word-break: break-all for aggressive breaking, this handles nearly all long-string overflow cases in grid items.

Tracking down grid overflow in DevTools

Open Chrome DevTools and select the grid container. Enable the Grid overlay from the Elements panel by clicking the green grid badge. The overlay shows column boundaries as dashed lines. If content is overflowing, you will see the content extending visually past the dashed column line — a clear indicator which column and which element are responsible.

In the Computed tab of the overflowing grid item, look at the element's actual width. If the computed width exceeds the column track size shown in the overlay, the item is wider than its column allows. Check whether the item contains an image, a pre, or other non-wrapping content.

Select the grid container and check the computed value of grid-template-columns in the Computed tab. The browser shows fully resolved values — repeat(2, 1fr) will appear as something like 640px 640px if both columns are sized correctly. If one column shows a larger resolved value, that column's content minimum is overriding the fr calculation.

Toggle overflow from the Styles panel to identify the overflow source. Temporarily adding overflow: hidden to the grid container hides the overflow but confirms the container is the scope of the problem. Then move the overflow: hidden down to individual grid items to find which item is the source. Once identified, address the root cause — constrain images, set overflow-wrap, or change 1fr to minmax(0, 1fr) — rather than relying on overflow: hidden permanently.

Chrome DevTools shows an orange dot on elements that are causing overflow in their ancestor, visible in the Elements panel without needing to open the grid overlay. When you see this indicator on a grid item or its descendant, select that element first and inspect its width, min-width, and content dimensions. This overflow indicator narrows the search to a specific element immediately, which is especially useful in dense DOM trees where the overflowing element is nested several levels inside a grid cell.

Targeted fixes for each overflow type

For columns that overflow because of wide text or block content, change all 1fr column definitions to minmax(0, 1fr). This is the universal fix for columns that should shrink below content size. It applies to both grid-template-columns and grid-auto-columns. For existing projects, a global rule of grid-template-columns: minmax(0, 1fr) or applying it through a CSS custom property helps standardize this across all grid usages.

For images overflowing grid cells, add a rule targeting all images inside grid items: .grid-container > * > img { max-width: 100%; height: auto; display: block; }. The max-width: 100% prevents the image from exceeding its containing element's width. height: auto maintains the aspect ratio. display: block removes the inline bottom gap that creates a thin sliver of space below images.

For long words and URLs breaking grid layouts, add overflow-wrap: break-word to the grid items or their text containers. For code blocks specifically, wrap them in an element with overflow-x: auto so the code can scroll horizontally rather than force the column wider. Applying overflow: hidden clips content without a scroll affordance, which loses information — use it only for decorative content.

For responsive auto-fit grids that overflow on mobile, use the min() function inside minmax: minmax(min(MIN_WIDTH, 100%), 1fr). This technique caps the minimum at 100% of the container on narrow screens, allowing a single-column layout without overflow. It is a one-line fix that eliminates the need for a media query override.

For images that need to fill a grid cell completely without distorting aspect ratio, use width: 100%; height: 100%; object-fit: cover on the img element and ensure the grid cell or a wrapping div has a defined height. The object-fit: cover property scales the image to fill its box and crops the overflow rather than distorting the image. This is the correct approach for photo gallery grids where consistent thumbnail dimensions are required regardless of the source image's original aspect ratio.

Mobile, images, and subgrid overflow

On iOS Safari, images inside a grid may ignore max-width: 100% in certain flex or grid nesting configurations. Adding flex-shrink: 0 to the parent or using width: 100% instead of max-width: 100% resolves most iOS cases. The distinction is that max-width constrains the maximum but does not override an element's natural desire to be its intrinsic size; width forces the dimension absolutely.

SVG images behave differently from raster images in grid layouts. An SVG without explicit width and height attributes defaults to a 300px by 150px intrinsic size according to the specification. Some browsers render them at their viewBox size; others use the default. For consistent behavior, always add width: 100% and height: auto to SVGs inside grid cells, or set explicit width and height attributes on the SVG element itself.

Subgrid can introduce inherited overflow from the parent grid. When a grid item uses grid-template-columns: subgrid, its children participate in the parent's column tracks. If the parent grid has 1fr columns that are wider than expected due to content in other rows, the subgrid children inherit those inflated widths. Debugging requires inspecting the parent grid's overflow first before investigating the subgrid item.

Flexbox inside a grid cell can cause unexpected overflow. A flex container defaults to min-width: auto for its flex items, and a flex item inside a grid cell inherits the grid column width as its containing block. If the flex container itself has content wider than the grid column, the grid column will expand. Apply min-width: 0 to the flex container and min-width: 0 to the grid cell to address this nested layout interaction.

Grid overflow mistakes in production

Using overflow: hidden on the grid container as a permanent solution masks the overflow without fixing it. The content is still wider than intended — it is just clipped. This can hide interactive elements like buttons that are partially obscured, create confusing user experience where content appears cut off, and make debugging harder because the visual symptom disappears. Treat overflow: hidden as a diagnostic tool to identify the overflow source, then fix the root cause.

Setting a fixed width on grid items inside a 1fr column causes overflow when the fixed width exceeds the column track size. A card with width: 400px inside a 350px column will always overflow by 50px. Replace fixed widths on grid children with max-width or remove the width entirely and let the grid column control the sizing through its track definition.

Not testing with real content is the single largest source of overflow bugs reaching production. A grid looks correct with two-word labels and 100px images but breaks with a 400-character description or a 2000px photograph. Always test grids with realistic content lengths at multiple viewport widths before deploying. Take a CMS-driven grid especially seriously — CMS users will write long headlines and upload high-resolution images.

Using percentage widths on grid items inside a 1fr column creates compounding sizing. A grid item with width: 80% inside a 1fr column is 80% of the column track width. The column width is already fractional. Combined with padding and borders (if box-sizing is content-box), the calculated width may exceed the column track. Use max-width: 100% and let the grid column handle the primary sizing.

Preventing grid overflow from the start

Establish a global image rule in every project that uses grid layouts: img { max-width: 100%; height: auto; display: block; }. This single rule prevents the majority of image-related overflow across all grid and flex contexts. It does not affect images with explicit width attributes in HTML and does not distort aspect ratios.

Prefer minmax(0, 1fr) over 1fr for all grid columns by default. The behavior difference is rarely noticeable for standard text content, but it prevents the entire class of content-overflow problems caused by images, code blocks, and long strings. A CSS custom property — --col: minmax(0, 1fr) — can standardize this across a design system.

Add overflow-wrap: break-word to all grid items as a baseline style. In most layouts, words should break at the column boundary rather than overflow. The few cases where you do not want words to break (code, technical identifiers) should explicitly set overflow-x: auto and white-space: nowrap to signal intent.

Test your grid layouts at 320px, 375px, 768px, and 1280px viewport widths as a minimum set. The 320px test catches the most aggressive mobile overflow scenarios. Use Chrome's responsive design mode or a physical device to verify that no horizontal scrollbar appears at any tested width.

For auto-fit grids, always use the min() technique for the minmax minimum: minmax(min(IDEAL_MIN, 100%), 1fr). This future-proofs the grid against narrow viewports without requiring a separate media query. Document the intended minimum column width in a comment so the value can be updated if the design changes.

Apply contain: layout to grid items that contain absolutely positioned children or other elements that could escape their bounding box. The contain property creates a new containing block and prevents interior layout from affecting the grid track sizes, isolating any overflow to within the item rather than allowing it to push column boundaries outward.

Quick fix checklist

  • Replace 1fr with minmax(0, 1fr) in grid-template-columns for shrinkable columns
  • Add max-width: 100%; height: auto; display: block to all images inside grid items
  • Add overflow-wrap: break-word to grid item containers to handle long strings
  • For code blocks in grid cells, use overflow-x: auto instead of overflow-wrap
  • Test at 320px and 375px viewport widths to catch mobile overflow
  • Use minmax(min(MIN, 100%), 1fr) for auto-fit grids to prevent minimum overflow
  • Enable the Grid overlay in DevTools to visually identify which column is overflowing
  • Avoid overflow: hidden as a permanent fix — address the root cause instead

Related guides

Frequently asked questions

What is the difference between 1fr and minmax(0, 1fr) in CSS Grid?

1fr is shorthand for minmax(auto, 1fr). The auto minimum means the column will not shrink below the intrinsic content size of its items. minmax(0, 1fr) sets the minimum to zero, allowing the column to shrink freely. Use minmax(0, 1fr) when columns should be allowed to shrink below their content width, which prevents overflow from images, code blocks, or long strings.

Why does my grid overflow on mobile but not on desktop?

On desktop the grid column track is wide enough to accommodate the content intrinsic size. On mobile the viewport is narrower, so the content minimum width exceeds the available track width and causes overflow. Fix by changing 1fr to minmax(0, 1fr) and adding overflow-wrap: break-word to grid items. For auto-fit grids, use minmax(min(MIN, 100%), 1fr) to prevent the minimum from exceeding the viewport.

How do I prevent images from overflowing grid cells?

Add width: 100%; height: auto; display: block to images inside grid items. The width: 100% forces the image to fill its containing cell without exceeding it. height: auto preserves the aspect ratio. display: block removes the inline bottom gap. For fixed-height thumbnails, use height: 200px; object-fit: cover instead of height: auto.

Why does a long word cause grid overflow?

The browser does not insert line breaks inside a continuous string of characters without whitespace. A long URL, API key, or code identifier will extend to its full text width and force the grid column to expand. Add overflow-wrap: break-word to the grid item to allow breaking at arbitrary character positions. For code blocks, use overflow-x: auto with white-space: nowrap instead.

How do I fix a grid that overflows on 375px screens?

Identify the overflowing column by enabling the Grid overlay in DevTools. The most common cause on narrow screens is a fixed minimum in minmax — change minmax(300px, 1fr) to minmax(min(300px, 100%), 1fr). This caps the minimum at 100% of the container on narrow screens. Also confirm images have max-width: 100% and long strings have overflow-wrap: break-word.

Can I use overflow: hidden to fix grid overflow?

overflow: hidden clips the overflowing content, hiding the visual symptom but not fixing the underlying cause. Content is still wider than the column — it is just invisible. Clipped content may include interactive elements like buttons or links. Use overflow: hidden only as a diagnostic step to confirm which element is overflowing, then fix the root cause by constraining that element's dimensions.

Why does overflow-wrap: break-word not always work?

overflow-wrap: break-word only breaks at character boundaries when no other break opportunity exists. If the containing element has white-space: nowrap set (common in code blocks), the browser will not insert line breaks regardless of overflow-wrap. For code blocks, use overflow-x: auto instead of overflow-wrap, and keep white-space: pre or white-space: nowrap to preserve code formatting.

Does minmax(0, 1fr) affect all browsers?

minmax(0, 1fr) is supported in all browsers that support CSS Grid — Chrome 57+, Firefox 52+, Safari 10.1+, and Edge 16+. It is a standard function with no vendor prefix needed. The behavior difference from 1fr is fully defined in the specification and consistent across all modern browsers. Internet Explorer 11 has a partial Grid implementation that does not fully support minmax in all contexts.

All tools run in your browser. Your data never leaves your device. Last updated: 2026-05-06.