Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I stop a long word from stretching a grid or flex layout?

Quick answer

A long unbroken string can affect an item’s minimum size and make a grid or flex layout wider than expected. Try min-width: 0 on the grid or flex item to let it shrink, and overflow-wrap: anywhere on the text to let an overlong word break. These solve different parts of the problem, so use both when needed and check that the text remains readable. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

A card in my grid is making the whole page wider when it contains a long URL or identifier with no spaces. Why does that happen?

Geez

An unbroken string has no ordinary word boundary where it can wrap. If it is too long for its container, it can overflow. [S1]

There can also be a sizing issue: grid and flex items can have an automatic minimum size based on their min-content size. A long word can constrain how narrow the item gets, so the layout may grow instead of shrinking the item. [S2]

Here’s a small reproduction. The long value is deliberately unbroken:

<div class="layout">
  <aside>Sidebar</aside>
  <main class="card">
    <p>Reference: ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMN</p>
  </main>
</div>
.layout {
  display: grid;
  grid-template-columns: 12rem 1fr;
  gap: 1rem;
}

.card {
  border: 1px solid #888;
  padding: 1rem;
}

If the available space is narrow, check both the item’s minimum size and how its text is allowed to wrap.

Follow-up question

What’s the focused fix, and how do I know whether I need one rule or both?

Geez

Use min-width: 0 on the grid or flex item that needs to shrink. This removes its automatic minimum-size constraint; it does not, by itself, guarantee that the text will wrap. [S2]

Then use overflow-wrap: anywhere on the text container if the string itself needs to break. This can break an overlong word when it cannot fit. [S1]

.card {
  min-width: 0;
}

.card p {
  overflow-wrap: anywhere;
}

Try the rules separately. If min-width: 0 lets the card fit but the string still spills out, add the wrapping rule. If the text wraps but the grid or flex item still refuses to shrink, check that min-width: 0 is on the actual item participating in that layout. Use both when both symptoms occur.

Follow-up question

Can you show the corresponding flex setup, and where should each rule go?

Geez

In the grid example, put min-width: 0 on the card, then put the wrapping rule on the element containing the long text:

.card {
  min-width: 0;
  border: 1px solid #888;
  padding: 1rem;
}

.card p {
  overflow-wrap: anywhere;
}

For a flex layout, apply the same idea to the flex item that needs to shrink and to the text it contains:

.row {
  display: flex;
  gap: 1rem;
}

.content {
  min-width: 0;
}

.content p {
  overflow-wrap: anywhere;
}

The key is to put min-width: 0 on the constrained grid or flex item, not just on an unrelated ancestor. Put overflow-wrap on the element containing the long text. These selectors are illustrative; use the elements that play those roles in your markup. [S1] [S2]

Follow-up question

How should I check the fix without just hiding a real problem?

Geez

Compare the layout before and after at the widths where the issue appeared. Check that the page no longer gains unwanted horizontal width, that the long value breaks inside its intended container, and that nearby columns or cards still have usable space. If the value is meaningful as one unit, consider whether breaking it makes it harder to read.

These rules address minimum sizing and text wrapping. They do not establish why a page is overflowing if another element or layout constraint is responsible. If the issue remains, inspect the overflowing element and its parent layout rather than adding broad overflow clipping, which can conceal content.

A practical checklist

  • Identify the grid or flex item that is expected to shrink.

  • Add min-width: 0 to that item and check whether it can become narrower.

  • Apply overflow-wrap: anywhere to the text containing the unbroken value if it still spills out.

  • Check the layout at the narrow widths where the problem occurs.

  • Confirm that the value remains readable and is not merely clipped or hidden.

A few more useful answers

Does `min-width: 0` make a long word wrap?

Not necessarily. It removes an automatic minimum-size constraint on a grid or flex item; text wrapping depends on the text’s styling. Use overflow-wrap: anywhere when the long string needs to break. [S1] [S2]

Should I put `overflow-wrap: anywhere` on the whole page?

Start with the specific text container that has the problem. A focused change makes it easier to assess whether breaking that particular content is readable.

Why does the problem remain after adding `min-width: 0`?

That rule allows the item to shrink but does not guarantee that the unbroken text will wrap. Add a wrapping rule to the text container, and check that min-width: 0 is applied to the item that actually participates in the grid or flex layout. [S1] [S2]

Sources

References used for this guide.

  1. [S1] Wrapping and breaking text — CSS | MDN
    developer.mozilla.org · Checked
  2. [S2] min-width — CSS | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work