The CSS Custom Highlight API is a browser API that lets you style arbitrary ranges of text by registering Range objects in a Highlight , adding it to CSS.highlights under a name, and targeting that name with the ::highlight() pseudo-element. The DOM stays exactly as it was: no <span> , no <mark> , no extra nodes.
I ran into this while building a highlights-and-notes feature, and it changed how I think about the problem. This post covers why the old approach hurts, how the native API works, what support looks like today, and what you still have to build yourself.
What is wrong with wrapping highlights in spans?
For about fifteen years, the standard way to highlight text on a web page was to split text nodes and wrap the pieces in elements. mark.js wraps matches in <mark> . Rangy's highlighter and web-highlighter wrap them in <span> . It works, but it has costs:
You are editing a DOM someone else owns. In React, Vue or Svelte, the framework assumes it controls the nodes it rendered.…