pseudo-element

::selection — CSS reference

Styles text ranges selected by the user. Highlight pseudo-elements accept only a subset of properties and have special inheritance through both the originating element and ancestor highlights. Ensure selected text still has sufficient contrast.

Formal Syntax & Structure

css syntaxcss
p::selection { background-color: gold; }

Working Example

Example Snippetcss
<!doctype html><html lang="en"><meta charset="utf-8"><title>CSS reference lab</title><style>#a::selection {color:#202020;background-color:#ffd966}</style><body><p id="a">Select this sentence.</p><script>const r=document.createRange();r.selectNodeContents(document.querySelector("#a"));getSelection().removeAllRanges();getSelection().addRange(r);</script></body></html>
CSS Reference