Controlling where URLs wrap

Long URLs that spill past their container are easy to handle with the usual CSS escape hatches, which let the browser apply its own word-breaking and hyphenation rules. When a style guide demands that breaks land at specific points, those automated tools are too blunt. Getting precise control means telling the browser exactly where a line may break.

Why the placement matters

Overflowing URLs look bad, which is reason enough to care. The firmer motivation is editorial: The Chicago Manual of Style defines where URLs should break in print, while granting that reflowable electronic formats generally don't need specified breaks:

It is generally unnecessary to specify breaks for URLs in electronic publications formats with reflowable text, and authors should avoid forcing them to break in their manuscripts.

Chicago 17th ed., 14.18

That exemption stops applying when the output is print: a book, a PDF rendered from HTML, a résumé, or a reference list at the end of a post. As Rachel Andrew has argued, designing for print with CSS is a real workflow, and there line breaks in URLs become imperative rather than optional.

Punctuation-driven break points

Following Chicago's approach, breaks should follow punctuation so a reader can tell the URL continues onto the next line. Acceptable locations are:

  • After a colon or a double slash (//)
  • Before a single slash (/), a tilde (~), a period, a comma, a hyphen, an underline (underscore, _), a question mark, a number sign, or a percent symbol
  • Before or after an equals sign or an ampersand (&)

Equally important is what not to introduce. hyphens: auto; and soft hyphens work well for ordinary words but are wrong for URLs. On screen a soft hyphen doesn't break copy-and-paste, but a reader can still mistake it for a character that belongs to the address — hyphens occur in URLs often enough to make the confusion plausible. In print an inserted hyphen is simply incorrect. Extremely long words inside a URL can still be broken, but not with hyphens; Chicago leaves that decision to discretion, with the main goal remaining breaks around punctuation.

The <wbr> element

HTML has a dedicated element for this: <wbr>, which marks a line break opportunity — a signal to break here if needed, and not just anywhere. Applied "Chicago style" to a long URL, it produces something like:

http:<wbr>//<wbr>www<wbr>.<wbr>amazon<wbr>.com<wbr>/<wbr>s/<wbr>ref<wbr>=<wbr>sr<wbr>_<wbr>nr<wbr>_<wbr>i<wbr>_o<wbr>?rh<wbr>=<wbr>k<wbr>%3Ashark<wbr>+vacuum<wbr>%2Ci<wbr>%3Agarden<wbr>&<wbr>keywords<wbr>=<wbr>shark+vacuum<wbr>&ie<wbr>=<wbr>UTF8<wbr>&<wbr>qid<wbr>=<wbr>1327784979

Inserting these tags by hand gets old almost immediately, so the process can be automated with JavaScript and suitably placed regular expressions:

/**
 * Insert line break opportunities into a URL
 */
function formatUrl(url) {
  // Split the URL into an array to distinguish double slashes from single slashes
  var doubleSlash = url.split('//')

  // Format the strings on either side of double slashes separately
  var formatted = doubleSlash.map(str =>
    // Insert a word break opportunity after a colon
    str.replace(/(?<after>:)/giu, '$1<wbr>')
      // Before a single slash, tilde, period, comma, hyphen, underline, question mark, number sign, or percent symbol
      .replace(/(?<before>[/~.,\-_?#%])/giu, '<wbr>$1')
      // Before and after an equals sign or ampersand
      .replace(/(?<beforeAndAfter>[=&])/giu, '<wbr>$1<wbr>')
    // Reconnect the strings with word break opportunities after double slashes
    ).join('//<wbr>')

  return formatted
}

The result checks all three boxes: URLs break at appropriate spots, no extra punctuation appears that could be read as part of the URL, and the <wbr> tags are generated rather than written into the markup by hand. The approach suits static site generators especially well, since the script doesn't need to run on the client. A working Eleventy example lives in the author's personal site repository. For breaks inside particularly long words within a URL, hand-placed <wbr> tags remain the recommendation, guided by Chicago's section on word division (7.36–47).

Compatibility and a fallback

<wbr> has existed in the wild since 2001 and was standardized with HTML5, so support is now near-universal. Oddly, it worked in Internet Explorer 6 and 7 but was dropped starting with IE 8, and it has always been supported in Edge. Legacy IE is therefore the main gap, along with some HTML-to-PDF tools: Prince needs help with <wbr>.

For those environments, a pseudo-element can insert a zero-width space, which is how <wbr> is meant to behave in UTF-8 pages anyway. That pushes support back to IE 9 and, more usefully, works with Prince:

/** 
 * IE 8–11 and Prince don’t recognize the `wbr` element,
 * but a pseudo-element can achieve the same effect with IE 9+ and Prince.
 */
wbr:before {
  /* Unicode zero width space */
  content: "\200B";
  white-space: normal;
}

Precision with URLs and line breaks is worth pursuing even without a print workflow or a style guide behind it.