Today I learned
How do I stop long URLs from causing horizontal scroll on mobile?
· updated #Web development
A bare URL or a long identifier in inline code is one unbreakable "word" to the browser. On a phone it ran past the column and gave the whole page a sideways scrollbar.
.prose {
overflow-wrap: break-word;
}
.prose a,
.prose :not(pre) > code {
overflow-wrap: anywhere; /* also shrinks min-content width */
}
body {
overflow-x: clip; /* guard rail, not hidden */
}
Two details that matter:
break-wordonly wraps once the line already overflows. It doesn't lower the element's min-content width, so inside a flex or grid item the column still stretches to the full URL.anywheredoes, so it works there too. For flex rows, also putmin-width: 0on the text item.- Use
overflow-x: clip, nothidden.hiddenturns the element into a scroll container (and the other axis becomesauto), which quietly breaksposition: stickyheaders.clipjust cuts off the overflow.
Code blocks keep their own horizontal scroll, so only inline text wraps.
Hit this on the TIL pages of this site: one note with a long link widened every page on mobile.
Source: MDN: overflow-wrap, MDN: overflow clip