brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: marks where a long URL may break instead of overflowing

Web-Development
~/tips/tip-html-wbr-long-urls

https://example.com<wbr>/some<wbr>/very<wbr>/long<wbr>/path<wbr>/to<wbr>/a-file.pdf

Marks places where a long URL or path may wrap, without adding a hyphen.

One long word, usually a URL or a file path, is enough to push a layout wider than a phone screen. The <wbr> element tells the browser where it may wrap, without showing anything. I’d reach for it in any text that holds addresses or paths.

Put it at the spots where a break is fine:

<p>
  https://example.com<wbr>/some<wbr>/very<wbr>/long<wbr>/path<wbr>/to<wbr>/a-file.pdf
</p>

If the line has room, nothing changes. If not, the browser may break the line at a <wbr>. It behaves like a zero-width space and does not add a hyphen. For comparison, a soft hyphen (&shy;) shows a hyphen at the break, which is right for words but wrong for URLs, where an extra dash can be mistaken for part of the address.

The Yahoo Style Guide, which MDN cites, advises breaking before punctuation such as a dot or slash, so a mark is not left alone at the end of a line where it looks like the end of the URL. That is why the example above puts the tag before each slash.

Caveats: you have to place the tags yourself, so it suits content you control. For user-generated text, the CSS properties overflow-wrap: anywhere or word-break do it for you without touching the markup. Copying text across a <wbr> normally gives the original string back, but check it in the browsers you care about before relying on that for something like a link people copy.

It has been supported by all major browsers since July 2015, so it is a safe pick.

Further reading: MDN: <wbr>

More Web-Development tips ab -n 200 -c 10 https://example.com/ console.assert(items.length > 0, 'cart is empty', items) document.designMode = "on" function save(item) { Save draft All Web-Development tips (67) All topics

Link