Skip to content
SIMON WARDMUSIC · TECHNOLOGY · CREATION
THE JOURNAL / SIMON WARD

How to Fix a Long URL That Overflows a CSS Flex Layout

20 November 2026 · Simon Ward · 9 min read
A laptop and phone showing the same card layout at different screen widths.

A long URL can push a two-column layout off the side of a phone even when the same page looks fine on desktop. A common cause is a flex item that refuses to shrink below the width of its contents. Give that item permission to shrink, then tell the browser how to wrap the URL. These are two separate checks, and making both explicit helps the fix survive longer titles and links.

This guide uses a card with a fixed icon and a flexible text column. The same diagnosis applies to a navigation row, a WordPress template or a list of search results. Start with the narrowest view where you can reproduce the problem. Keep the broken example open so you can compare the before and after sizes.

1. Find the row that is wider than the screen

Open the page in Chrome and use the device toolbar in DevTools to set a narrow viewport. Try about 390 pixels to begin with, then test a narrower width if that is where the issue appears. Reload and look for a horizontal scrollbar or a section that disappears off the right edge. Confirm that you are looking at the public page if the editor adds its own sidebar or protective padding.

Inspect the text that appears to be spilling out. In the Elements panel, walk up the parent elements until you find the row whose computed display is flex. Its immediate children are the flex items. In a card with an icon beside a column containing a heading and a URL, the column is the item you usually need to change. The anchor containing the URL can be several levels deeper.

Compare the row's highlighted box with the child's highlighted box. If the child's box itself extends past the row, investigate its minimum size and any explicit width. If the child fits but the letters go past its edge, investigate wrapping. Both conditions can happen at the same time. DevTools lets you try changes in the Styles pane without writing them into your stylesheet while you are working out which rule matters.

Disable a suspect declaration temporarily and reload once you have finished experimenting so you know which changes were made only in DevTools. Record the element or class you changed. It is easy to fix the right property on the wrong element, especially in a page builder that inserts wrappers around each card.

2. Let the flexible child shrink

A flex item in a horizontal row normally has an automatic minimum width based on its contents when its overflow is non-scrollable. A long token can contribute a surprisingly large minimum. A positive flex-shrink value lets the item participate in shrinking but does not by itself remove that lower bound. The Flexbox specification's automatic minimum size section describes the rule and its exceptions.

Select the immediate flex child that contains the heading and URL. Try applying min-width: 0 to that element in DevTools. If the child now fits beside the icon, you have identified the size constraint. In a component built for different writing modes, min-inline-size: 0 is the logical equivalent for the inline axis. Use the form that suits your existing stylesheet and test it in the direction the component actually lays out.

The zero value is a minimum, not a width target. The item can still take all the available space. Its flex basis, grow factor and shrink factor continue to do their jobs. If you gave the whole row a fixed width, or set a separate fixed width on another child, removing this minimum will not undo those declarations. Recheck the highlighted boxes if the test helps but does not fix the layout completely.

With nested flex rows, test the immediate child at each level. For instance, a link inside a card may fit within the card after your change, while the card remains too wide for a parent sidebar layout. Start at the outer boundary that exceeds its container and work inward. Apply the rule where there is an actual shrinking problem, instead of putting it on every child in the page.

3. Give long URLs somewhere to break

Once the column fits, inspect the URL itself. Long addresses, file names and identifiers may have few convenient break points. Add overflow-wrap: anywhere to the text that needs to wrap. It allows a line to break within an otherwise unbreakable word when needed to prevent overflow. Ordinary text can still break at spaces. The inserted soft wrap is visual and does not insert a new character into the underlying URL.

Check the computed white-space value if nothing changes. A component with white-space: nowrap can still force the text onto one line. That rule may be deliberate on a button or a short label, so reset it just for the link or text region that should wrap. Also check for a style on an inner element, since changing the containing column may leave a more specific rule in place on the text itself.

The MDN overflow-wrap reference explains an extra reason to prefer anywhere for this case. Its soft wrapping opportunities count when the browser calculates the min-content size. The legacy break-word value does not treat them the same way for that calculation. In a simple layout the two values can look similar, but a nested sizing calculation can expose the difference.

If the full URL does not need to be visible, a descriptive link label can make the card easier to scan. Keep the destination on the link. When a reader needs to compare or copy the full address, keep it visible and wrap it instead. Use the same approach for any long identifiers the component needs to display. Test real content as well as a deliberately long example so you can see whether the breaks look sensible.

4. Apply the rules to one component and retest

For the example card, the row uses flex with a gap between an icon and the text. The icon has a stable size. The text child grows and shrinks, and its minimum width is zero. The link can wrap anywhere when its address needs more space. The following rules belong in the component's stylesheet. Use class names that match your own markup.

.link-card {
  display: flex;
  gap: 0.75rem;
}
.link-card__icon {
  flex: none;
}
.link-card__body {
  flex: 1 1 auto;
  min-width: 0;
}
.link-card__url {
  overflow-wrap: anywhere;
}

Inside the card, make the icon and body siblings. Put the link with the URL class inside the body alongside any heading or description. The important relationship is that the body is a direct child of the element with display flex. A rule applied further down may help the text wrap while leaving an outer wrapper with an unwanted automatic minimum.

Try the component at a comfortable desktop width first and look at normal links. Then narrow the viewport and replace one visible address with a test string much longer than usual using the editor or a local demo. The card should stay inside its container. The icon should retain its size and the text should continue across as many lines as it needs. If the icon alone takes too much space at the smallest width, use a smaller icon or change the layout at that breakpoint.

5. Diagnose the cases where this still does not fix the page

If the row contains an image, inspect its width separately. A large intrinsic image or an explicitly sized thumbnail can be the item forcing the row wider. Responsive sizing can fix that case. If the browser is also downloading an unnecessarily large file for a small slot, see the guide to checking the image WordPress serves on mobile. The file selected by the browser and the display width in CSS are related checks with different remedies.

If the content is a table or a code sample, decide whether it makes sense to wrap it. A table may be easier to understand in its own horizontally scrollable wrapper than with every cell broken into small fragments. An example of source code should remain readable and copyable. Give its overflow a deliberate place to go, and test whether users can scroll within that area with their input device. Keep the rest of the page within the viewport.

Check any width rules that use viewport units inside a padded wrapper. A row set to the full viewport width can already be wider than the wrapper allows before a link is added. Also look for margin, gap and padding when multiple fixed-width children share a row. Removing the minimum from one child will not create more room if the siblings and spacing already consume all of it.

Changing flex-wrap on the parent can help when entire items are meant to move onto a new line. It solves a different problem from allowing words to break inside a flexible child. If your design should keep the icon beside its heading, make that pair fit before deciding to wrap the outer row. Where a second line is genuinely preferable, test how the component reads when the items stack.

6. Verify the final change with real content

Save the smallest stylesheet change that solves the case you reproduced. Reload the public page in a fresh browser tab. On a cached WordPress page, check that you are seeing the current stylesheet before making further changes. If your site builds or combines CSS files, verify that the public asset reflects the new version. A DevTools experiment that worked instantly can look ineffective on the live page when an earlier stylesheet is still being served.

Test a narrow phone width, a wider phone width and a desktop width. Try the longest actual content you have, then check a card with short text to make sure it still looks right. Zoom in and check for clipping or unreadable text. Tab to the link and confirm that its focus indicator remains visible. A quick copy test should produce the original full address without line breaks inserted into it.

Check that you can reach the page's controls without horizontal scrolling. If the page still has a scrollbar, identify the next oversized element rather than hiding overflow on the body. A site-wide overflow rule can conceal content or focus outlines while leaving the layout mistake in place. Once the card and its parent both fit, keep a long-address test case with the component so it is easy to spot the same issue after a template change. More practical walkthroughs are available in the Guides archive.

If you hand the fix to someone else, include the width that reproduced the problem, the test address and the name of the flex child that needed to shrink. Add the expected result in plain language: the icon stays beside the text, the address wraps and the page can be read without sideways scrolling. This gives a reviewer enough information to repeat the check without knowing how you found the faulty rule.

Keep the before and after results next to the change if it affects a shared template. A screenshot of each viewport and the actual class name will make the next regression quicker to diagnose.

Share this post