The CSS Custom Highlight API: highlight search terms without changing the DOM
Highlighting the search term in results used to mean wrapping every match in a <mark> element: a regular expression over HTML, broken markup when the term appears inside an attribute, and a re-render of the content. The Custom Highlight API highlights text ranges without touching the DOM: the script creates Range objects, registers them under a name, and CSS styles them with ::highlight( name ).
Problem: A theme highlights the search term on the search results page with a preg_replace() in PHP. It breaks links when the term appears in a URL, does not handle accents, and on pages with many results the replaced HTML is slow to render.
Solution: Leave the result markup untouched, find the matches in text nodes with a TreeWalker, create a Range for each, register them as a Highlight in CSS.highlights, and style them with ::highlight( ha-search ). Browsers without the API simply show no highlight.
Find and register the ranges
// js/search-highlight.js
function haHighlight( root, term, name = 'ha-search' ) {
if ( ! ( 'highlights' in CSS ) || ! term ) return;
const needle = term.toLocaleLowerCase();
const ranges = [];
const walker = document.createTreeWalker( root, NodeFilter.SHOW_TEXT, {
acceptNode: ( node ) => node.parentElement.closest( 'script, style, textarea' )
? NodeFilter.FILTER_REJECT
: NodeFilter.FILTER_ACCEPT,
} );
for ( let node = walker.nextNode(); node; node = walker.nextNode() ) {
const text = node.nodeValue.toLocaleLowerCase();
let index = text.indexOf( needle );
while ( index !== -1 ) {
const range = new Range();
range.setStart( node, index );
range.setEnd( node, index + needle.length );
ranges.push( range );
index = text.indexOf( needle, index + needle.length );
}
}
CSS.highlights.set( name, new Highlight( ...ranges ) );
}
const term = new URLSearchParams( location.search ).get( 's' );
haHighlight( document.querySelector( '.search-results, main' ), term );
Only text nodes are searched, so attributes and URLs are never affected. A Range marks a start and end position in a text node; the Highlight object groups them, and CSS.highlights (a map) registers the group under a name. Updating the highlight later is one call: CSS.highlights.set() with a new set of ranges, or CSS.highlights.delete( 'ha-search' ) to remove it.
Style it
::highlight(ha-search) {
background-color: #fde68a;
color: inherit;
}
Only a few properties are allowed in ::highlight(): colors, background color, text decoration, text shadow. Layout properties (padding, font weight, border) are not, because the highlight does not create a box. That is also why it is fast: no reflow happens.
Load it only on search pages
add_action( 'wp_enqueue_scripts', function (): void {
if ( is_search() ) {
wp_enqueue_script( 'ha-search-highlight', get_theme_file_uri( 'js/search-highlight.js' ), array(), wp_get_theme()->get( 'Version' ), array( 'strategy' => 'defer' ) );
}
} );
With defer, the script runs after the HTML is parsed, so all result text exists. For results loaded later with AJAX or infinite scroll, call haHighlight() again with the new container and merge the ranges: infinite scroll with the REST API.
Other uses
The same technique highlights the current sentence of a text-to-speech player, spelling hints in a custom editor field, or the differences between two revisions, all without touching the content’s markup.
NOTE: The Custom Highlight API works in Chrome and Edge 105 (2022), Safari 17.2 (December 2023) and Firefox 140 (June 2025). The 'highlights' in CSS check keeps older browsers safe. Screen readers do not announce custom highlights, which is fine for search terms; for meaning that must be conveyed, <mark> is still the semantic element.