---
title: "Why does my flex item overflow its container?"
description: "Flex items default to min-width: auto, so they refuse to shrink below their content. Learn why long text or wide children overflow and how min-width: 0 fixes it."
url: https://www.danny.engineering/answer/css/why-does-my-flex-item-overflow-its-container
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["CSS"]
---

# Why does my flex item overflow its container?

## The short answer


Flex items have a default `min-width` of `auto`, which means they will not shrink smaller than their content. A long URL, a wide table or a `pre` block inside the item pushes it past the edge of the container. Set `min-width: 0` on the flex item that should be allowed to shrink.


```css
.card-body {
  flex: 1;
  min-width: 0; /* let the item shrink below its content width */
}
```


## Why it happens


In normal block layout, a box takes the width its parent gives it and the content wraps or overflows inside it. Flexbox works differently. The flexbox spec gives flex items an automatic minimum size: for an item whose `overflow` is `visible`, `min-width: auto` resolves to the smallest width the content can have without overflowing. That is usually the width of the longest word, image or unbreakable string.


So when the container gets narrow, the browser tries to shrink the item, hits that content-based minimum and stops. The item stays wide and spills out of its parent, often taking a horizontal scroll bar with it.


The same thing happens vertically with `min-height: auto` in a column flex container, which is why a scrolling panel inside a full-height flex layout sometimes refuses to scroll and grows the page instead.


Common triggers are:

- Long URLs, email addresses or hashes with no spaces.
- Code blocks with `white-space: pre`.
- Text that uses `white-space: nowrap` with `text-overflow: ellipsis`.
- Nested flex or grid children, where the inner item is the one with the wide content.
- Images or tables with a fixed width.

## How to fix it


### Set min-width: 0 on the flex item


This is the standard fix. It replaces the content-based minimum with zero, so the item can shrink to whatever space is available. The content then wraps, truncates or overflows inside the item, where you can control it.


```css
.layout {
  display: flex;
  gap: 1rem;
}

.sidebar {
  flex: 0 0 16rem;
}

.main {
  flex: 1;
  min-width: 0;
}
```


If the wide content is several levels deep, every flex item between the container and that content needs `min-width: 0`. Check each level in DevTools until the overflow stops.


### Set an overflow value other than visible


An item with `overflow: hidden`, `auto` or `scroll` also gets a minimum size of zero. Use this when you want the item to clip or scroll its own content, for example a code block that should scroll sideways.


```css
.main {
  flex: 1;
  overflow-x: auto;
}
```


Be careful with `overflow: hidden`, because it can also clip focus rings, shadows and dropdowns.


### Let long words break


If the culprit is a long unbroken string, allow it to wrap as well as letting the item shrink.


```css
.main {
  min-width: 0;
  overflow-wrap: anywhere;
}
```


### Use minmax(0, 1fr) in grid


CSS Grid has the same automatic minimum. A `1fr` track has a minimum of `auto`, so wide content can stretch it. Use `minmax(0, 1fr)` to allow the track to shrink.


```css
.layout {
  display: grid;
  grid-template-columns: 16rem minmax(0, 1fr);
}
```


## Common pitfalls

- Adding `min-width: 0` to the container instead of the item. The rule belongs on the flex child.
- Fixing only the outer item when the wide content sits inside a nested flex item. Each level needs it.
- Reaching for `width: 100%` or `max-width: 100%`. These do not override the automatic minimum on their own.
- Using `flex-shrink: 1` and expecting it to help. Items already shrink by default; the minimum size is what stops them.
- Forgetting the vertical version. In a column layout with a scrolling child, use `min-height: 0`.

## Related reading

- [MDN: min-width](https://developer.mozilla.org/en-US/docs/Web/CSS/min-width)
- [MDN: Controlling ratios of flex items along the main axis](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_flexible_box_layout/Controlling_ratios_of_flex_items_along_the_main_axis)
