Up to date
Published
3 min read

Trevor I. Lasn

Staff Software Engineer, Engineering Manager

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.

img

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.


Found this article helpful? You might enjoy my free newsletter. I share dev tips and insights to help you grow your coding skills and advance your tech career.

Interested in supporting this blog in exchange for a shoutout? Get in touch.


Liked this post?

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

Webdev
4 min read

Open Dyslexic Font: Improve Your Web Accessibility

How to implement the Open-Dyslexic font to enhance readability for users with dyslexia

Oct 12, 2024
Read article
Webdev
4 min read

LH and RLH: The CSS Units That Make Vertical Spacing Easy

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

Dec 3, 2024
Read article
Webdev
6 min read

Inside the CSS Engine: CSSOM Explained

A deep dive into how browsers parse and manipulate CSS, its impact on web performance, and why it matters

Oct 25, 2024
Read article
Webdev
12 min read

Robust Data Fetching Architecture For Complex React/Next.js Apps

How I use the 'Three Layers of Data' architecture pattern for React and Next.js apps to avoid common pitfalls, tech debt, and improve performance

May 4, 2025
Read article
Webdev
3 min read

Form Validation That Doesn't Annoy Users: CSS :user-valid and :user-invalid

The new pseudo-classes :user-valid and :user-invalid give us a smarter way to style form validation states based on user interaction

Dec 12, 2024
Read article
Webdev
3 min read

Improve PageSpeed Insights Score with Lazy Loading Iframes

How to save bandwidth and speed up your site by lazy-loading iframes

Sep 13, 2024
Read article
Webdev
8 min read

Become a Web Developer in 180 Days

A comprehensive roadmap to becoming a proficient web developer

Oct 29, 2019
Read article
Webdev
3 min read

CSS @supports: Write Future-Proof CSS

Detect CSS feature support and provide smart fallbacks with @supports

Dec 6, 2024
Read article
Webdev
4 min read

How To Implement Content Security Policy (CSP) Headers For Astro

Content Security Policy (CSP) acts like a shield against XSS attacks. These attacks are sneaky - they trick your browser into running malicious code by hiding it in content that seems trustworthy. CSP's job is to spot these tricks and shut them down, while also alerting you to any attempts it detects.

Oct 16, 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.