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: #:~:text= in a link scrolls to and highlights one sentence on any page

Web-Development
~/tips/tip-text-fragment-link-to-sentence

https://example.com/docs/setup#:~:text=back%20up%20the%20database

Link to the exact sentence you mean, even on a page without anchors.

When I refer to one sentence in a long article, a plain link drops the reader at the top. A text fragment adds the words to look for, and the browser scrolls to them and highlights them.

https://example.com/docs/setup#:~:text=back%20up%20the%20database
https://example.com/docs/setup#:~:text=Before%20you%20start,restart%20the%20service
https://example.com/docs/setup#:~:text=step-,3,-:%20open

Everything after #:~:text= is the text to find, URL encoded. A single string matches that exact text. Two strings separated by a comma match a range from the first words to the last. The optional prefix- and -suffix parts (note where the hyphen goes) say what must come right before or after, which helps when the same words appear more than once, like the “3” in the last example.

Chrome and Edge have a “Copy link to highlight” item in the context menu that builds the link for you. As the page owner you can style the highlight with the ::target-text pseudo-element.

Support: Chrome 80, Safari 16.1 and Firefox 131. A browser without support just opens the page at the top, so the link still works. It breaks quietly when the page text changes, so for links that should last years a real id anchor is more reliable. MDN also notes it only works on user initiated navigations and never inside an iframe.

Further reading: MDN: Text fragments, MDN: ::target-text

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