Why a Long Title Stretches a Flex Card and How One Line of min-width: 0 Fixes ItBrief · digest for November 26
Brief≈ 1 min

Why a Long Title Stretches a Flex Card and How One Line of min-width: 0 Fixes It

You build a card with display: flex: title on the left, button on the right. You add white-space: nowrap, overflow: hidden, and text-overflow: ellipsis to the text, drop in a long string—and the layout breaks. The button gets pushed off the edge, and no ellipsis appears.

The cause sits in the CSS Flexbox specification. A flex item with the default overflow: visible gets min-width: auto. The browser computes its minimum width from its content, which for an unbreakable line is the entire string's width. So flex-shrink: 1 hits that floor and refuses to shrink the box.

.card {
  display: flex;
  align-items: center;
  gap: 8px;
}

.card__title {
  flex: 1;
  /* Override the automatic minimum based on text width */
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

Adding min-width: 0 removes that floor, letting the box shrink to the available space so text-overflow: ellipsis kicks in. If the title sits inside another flex wrapper with overflow: visible, put min-width: 0 on that wrapper too, or the parent will stretch the card anyway. Give adjacent buttons flex-shrink: 0 so they don't get squished, and in flex-direction: column, use min-height: 0.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 2