::highlight()Style arbitrary text ranges without modifying the DOM. Uses CSS.highlights registry and the ::highlight() pseudo-element.
Type a query to highlight all matching text ranges. The API creates StaticRange objects for each match and registers them as a Highlight.
::highlight(search-result) {
background-color: rgba(255, 166, 87, 0.35);
color: #ffa657;
}
Multiple named highlights with distinct colors. Select a color, then click words in the text to highlight them. Each color is a separate Highlight in the registry.
::highlight(color-blue) {
background-color: rgba(88, 166, 255, 0.3);
}
::highlight(color-green) {
background-color: rgba(63, 185, 80, 0.3);
}
::highlight(color-purple) {
background-color: rgba(210, 168, 255, 0.3);
}
::highlight(color-orange) {
background-color: rgba(255, 166, 87, 0.3);
}
::highlight(color-red) {
background-color: rgba(255, 123, 114, 0.3);
}
Simulates a spellchecker with wavy red underlines on "misspelled" words using ::highlight(spell-error) and text-decoration: underline wavy.
::highlight(spell-error) {
text-decoration: underline wavy red;
text-decoration-thickness: 1.5px;
text-underline-offset: 3px;
}
Basic syntax highlighting using multiple named highlights for keywords, strings, comments, numbers, and function names — all without wrapping tokens in <span> elements.
::highlight(hl-keyword) {
color: #ff7b72;
font-weight: 600;
}
::highlight(hl-string) {
color: #a5d6ff;
}
::highlight(hl-comment) {
color: #8b949e;
font-style: italic;
}
::highlight(hl-number) {
color: #79c0ff;
}
::highlight(hl-function) {
color: #d2a8ff;
}
Simulates multiple users editing the same document, each with their own colored highlight selection. Each user's selection is a separate named Highlight in the registry.
::highlight(cursor-alice) {
background-color: rgba(88, 166, 255, 0.25);
border-bottom: 2px solid #58a6ff;
}
::highlight(cursor-bob) {
background-color: rgba(63, 185, 80, 0.25);
border-bottom: 2px solid #3fb950;
}
::highlight(cursor-carol) {
background-color: rgba(210, 168, 255, 0.25);
border-bottom: 2px solid #d2a8ff;
}
Visualize StaticRange objects by setting start and end offsets. See how the Highlights API maps character positions to visual highlights.
::highlight(range-inspect) {
background-color: rgba(255, 166, 87, 0.3);
outline: 1px dashed #ffa657;
}