CSS ::target-text for Text Highlighting

A look at how browsers can highlight text fragments using CSS ::target-text, making text sharing and navigation more user-friendly

Ever needed to share a specific part of a webpage with someone? We often resort to screenshots or vague instructions like “search for this text” or “scroll down to the third paragraph.”

Another approach is to add anchor points with IDs <section id="section-1"> to enable direct navigation, but this requires modifying the HTML structure and only works for predefined sections. While these approaches work, they’re not ideal for sharing specific text content.

The ::target-text pseudo-element offers a native solution for text highlighting. When someone visits a URL containing a text fragment (like #:~:text=hello), browsers that support this feature will automatically find and highlight that text. Let’s look at how this works and where it might be useful.

The ::target-text pseudo-element simplifies this entire process. When a user visits a URL with a text fragment (like #:~:text=hello), the browser automatically matches and scrolls to that text. ::target-text lets us style this highlighted section without writing a single line of JavaScript.

The implementation is surprisingly straightforward. First, we define how highlighted text should appear:

Then, we can create links that highlight specific text when clicked:

Here’s a simple example. Click the link below to see the text fragment highlighting in action. The browser will scroll to the matching text and apply the yellow background color.

The quick brown fox jumps over the lazy dog. This is a demonstration of text fragments. Try clicking the links above to see how text fragments work. The browser will automatically scroll to and highlight the specified text.

  1. Click on the 'Click to highlight quick brown fox' text above
  2. Notice the URL change. The browser will find and highlight the specified text
  3. Share these links with others to help them find specific text

::target-text can be particularly helpful in several scenarios:

  1. Documentation websites where users need to reference specific sections
  2. Educational platforms where teachers want to highlight particular passages
  3. Support pages where staff need to guide users to specific information
  4. Blog posts or articles where readers want to share specific quotes. (via #:~:text= inside the URL)

Pro tip: On most browsers, you can also right-click on selected text and choose “Copy link to highlight” for the same functionality without any custom code.

Browser right-click menu on selected text with the 'Copy Link to Highlight' option underlined

The ::target-text pseudo-element brings native text highlighting to the web, making content sharing more precise and user-friendly. Whether through custom implementations or the browser’s built-in features, we can now share exact passages instead of vague directions - a small but significant step toward better web navigation.


Trevor I. Lasn

Trevor I. Lasn

Tech executive with over a decade of experience programming and building products. Building 0xinsider.com, ceodumps.com and whiteheap.com.



Related Articles

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

CSS
4 min read

Exploring new CSS line-height units that eliminate guesswork from vertical rhythm

Dec 3, 2024
Read article
CSS
3 min read

The ::details-content pseudo-element lets you style the expandable content of details elements separately from the summary, no divs needed.

Nov 11, 2025
Read article
CSS
3 min read

Finally, we can center things in block layouts without flexbox gymnastics

Dec 13, 2024
Read article
CSS
3 min read

Use scrollbar-width to thin or hide scrollbars and scrollbar-gutter to prevent layout shift. Browser support, examples, and gotchas.

Dec 19, 2024
Read article
CSS
4 min read

Style connected UI with the :interest-source and :interest-target pseudo-classes. Interactive examples of tooltips, forms and navigation without JavaScript.

Nov 12, 2025
Read article
CSS
7 min read

The CSSOM, or CSS Object Model, is the browser's map of every style rule. How it is built beside the DOM, how it computes styles, and how to keep it fast.

Oct 25, 2024
Read article
CSS
4 min read

CSS nesting is finally supported in all major browsers. Write cleaner, organized stylesheets without Sass or Less

Dec 6, 2024
Read article
CSS
8 min read

Create animated transitions between different states of your app without complex animation libraries.

Nov 11, 2025
Read article
CSS
3 min read

content-visibility: auto lets the browser skip layout and paint for offscreen elements until they are needed. How to use it with contain-intrinsic-size.

Dec 5, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/css-target-text. It was written by a human and polished using grammar tools for clarity.