Search Highlight Demo

The CSS Custom Highlight API provides a mechanism for styling arbitrary text ranges in a document by using JavaScript to create the ranges and CSS to style them. This avoids the need to modify the DOM structure, which can break event listeners or interfere with accessibility.

Common use cases include highlighting search results, marking spelling or grammar errors in text editors, and showing collaborative editing cursors. Because the API works at the rendering level rather than the DOM level, highlights are lightweight and can be updated frequently without triggering layout recalculations.

The API introduces three key primitives: Range objects that define character spans, Highlight objects that group ranges together, and the HighlightRegistry that maps names to highlights so CSS can target them with the ::highlight() pseudo-element.