Selection & search
Native text highlights styled through CSS highlight pseudo-elements. The tokens keep the colors themeable, while :root::selection and :root::search-text preserve the browser's highlight inheritance model.
0 KB JavaScript
34 CSS lines
native UX
native fallback
Preview
Select this text to see the custom selection style. You can also open the
browser search with Ctrl + F or Cmd +
F, then search for the word highlight.
A highlight can guide attention without changing the document itself. This
highlight appears when the browser matches your search, and the current
highlight becomes stronger as you move through the results. Try searching
highlight again, then jump between each highlight to compare the active match
with the other matches.
The code
CSS mechanisms
/* ----------------------------------------------------------------------------
* Native Highlight Tokens
* ---------------------------------------------------------------------------- */
:root {
--selection-bg: Highlight;
--selection-color: HighlightText;
--search-text-bg: Mark;
--search-text-color: MarkText;
--search-text-current-bg: Highlight;
--search-text-current-color: HighlightText;
}
/* ----------------------------------------------------------------------------
* Text Selection
* ---------------------------------------------------------------------------- */
/**
* :root::selection (not *::selection) respects the special inheritance model
* of highlight pseudo-elements. Applying *::selection can disrupt descendant
* highlight pseudo-elements in unexpected ways.
*
* Always declare both background-color and color together; providing only
* one leaves the contrast at the browser's mercy.
*/
:root::selection {
background-color: var(--selection-bg);
color: var(--selection-color);
}
/* ----------------------------------------------------------------------------
* Find-in-Page Highlight
* ---------------------------------------------------------------------------- */
/**
* Styles the matches produced by the browser's in-page search (Ctrl+F / Cmd+F)
* via the native CSS Highlight pseudo-element `::search-text`.
*
* Experimental: unsupported browsers safely ignore these selectors and keep
* their native find-in-page highlight.
*
* Only a restricted set of properties is allowed on highlight pseudo-elements
* (color, background-color, text-decoration, text-shadow).
* Layout-affecting properties (padding, border, box-shadow…) are ignored.
*
* @see https://drafts.csswg.org/css-pseudo-4/#highlight-pseudos
*/
/**
* ::search-text - All Matches
*
* Applies a branded highlight to every occurrence found by the user's
* find-in-page query, using the dedicated design tokens.
*/
:root::search-text {
background-color: var(--search-text-bg);
color: var(--search-text-color);
}
/**
* ::search-text:current - Active Match
*
* Highlights the currently focused match (the one the user navigates to with
* Enter / arrows) with a stronger color for clear visual distinction.
*/
:root::search-text:current {
background-color: var(--search-text-current-bg);
color: var(--search-text-current-color);
}Benefits
-
01.
Native behavior
The browser keeps handling text selection and find-in-page results. No JavaScript, no custom ranges, no duplicated search UI.
-
02.
Themeable tokens
Colors are exposed as custom properties, so the mechanism stays stable while each theme can define its own contrast pair.
-
03.
Clean inheritance
Using
:rootpreserves the special inheritance model of CSS highlight pseudo-elements and keeps descendant overrides predictable. -
04.
Native fallback
Unsupported browsers ignore
::search-textand keep their native find-in-page highlight unchanged.
Current limitations
-
01.
Experimental search styling
::search-textis still experimental and currently limited to Chromium-based browsers. -
02.
Restricted properties
Highlight pseudo-elements only accept paint-related properties such as
color,background-color,text-decoration, andtext-shadow. -
03.
Contrast responsibility
Always define foreground and background colors together. A single custom color can break contrast against browser defaults.