Wat is Custom Highlights API Generator?
The Custom Highlights API provides a way to style text ranges in the browser without modifying the DOM. Instead of wrapping text in span elements with inline styles, you create Range objects, collect them into a Highlight object, and register it with the CSS.highlights registry under a name. The ::highlight() pseudo-element then applies styles to those ranges. Because highlights do not modify the DOM, they are ideal for transient visual effects like search highlighting, find-in-page results, and syntax highlighting, where you want to avoid cluttering the DOM with temporary span wrappers.
Snel antwoord
The Custom Highlights API styles arbitrary text ranges via CSS without modifying the DOM. Use CSS.highlights.set() to register named highlights and ::highlight() pseudo-element for styling. Supports color, background-color, text-decoration, and text-shadow. Chrome 105+, Safari 18.2+ (Baseline 2026). Ideal for find-in-page, syntax highlighting, and search result markers with clean DOM preservation.
Beperkingen
- Browser support: Chrome 105+, Edge 105+, and Safari 18.2+. Firefox does not support the API as of version 138. A DOM-based fallback (span wrapping) is required for Firefox and other unsupported browsers.
- ::highlight() supports a limited set of CSS properties: color, background-color, text-decoration, text-decoration-color, text-decoration-style, text-shadow, and stroke-related properties. Layout properties like font-size, padding, margin, or display cannot be applied to highlights.
- Highlights are not automatically maintained when the DOM changes. Adding, removing, or modifying text content that overlaps with a highlighted range invalidates the highlight. You must recreate highlights after DOM mutations that affect the targeted text.
Zo gebruik je deze tool
- Write or paste the text content into the editor, or use the sample text provided. The tool demonstrates how highlights apply to ranges within the text.
- Configure the highlight name (used for the ::highlight() CSS selector) and the text patterns or ranges to highlight. Choose between word matching, regex matching, or manual range specification.
- Customize the highlight styling: background color, text color, border radius, and underline options. The ::highlight() pseudo-element supports a subset of CSS properties including color, background-color, text-decoration, and text-shadow.
- Copy the generated JavaScript (for creating and managing highlights) and CSS (for styling them). The output includes a self-contained example that can be embedded directly into a web page.
Waarvoor je het kunt gebruiken
- Implement find-in-page highlighting that marks all occurrences of a search term without wrapping every match in a span element and without disrupting the existing DOM structure.
- Add syntax highlighting to code blocks in a documentation site by creating highlights for keywords, strings, and comments based on regex pattern matching.
- Build a text comparison tool that highlights added, removed, and changed words between two versions of text, with each type using a different ::highlight() color.