Choose a preset, then select the sample text to see ::selection styles.
::selection presets
Drag across the paragraph below to highlight it.
Sample text
Select a fragment of this paragraph to see how its appearance is affected.
Preset: yellow background, red text
Code
CSS Examples
Each snippet focuses on ::selection. Extra styles stay minimal for study. Use Try It Yourself for a live editor—then highlight the text.
Example 1 — Basic Highlight Colors
Change selected text color and background.
CSS
<style>
p::selection {
color: red;
background-color: yellow;
}
</style>
<p>Select a fragment of this paragraph, to see how its appearance is affected.</p>
Select a fragment of this paragraph, to see how its appearance is affected.
How It Works
Target selection.p::selection styles only highlighted text inside paragraphs.
Set colors. Red text on yellow background is allowed.
User selects. Drag or keyboard-select to activate the style.
Unselected text. Normal paragraph styles stay unchanged.
Example 2 — Different Styles Per Element
Give body text and paragraphs separate selection looks.
CSS
<style>
::selection {
color: gold;
background-color: red;
}
p::selection {
color: white;
background-color: blue;
}
</style>
This text has special styles when you highlight it.
<p>Also try selecting text in this paragraph.</p>
This text has special styles when you highlight it.
Also try selecting text in this paragraph.
How It Works
Global rule.::selection sets a default highlight.
Scoped rule.p::selection overrides that default inside paragraphs.
Compare. Select outside vs inside the p to see both looks.
Cascade. More specific selection rules win for matching text.
Example 3 — Selection with Text Shadow
text-shadow is allowed on ::selection.
CSS
<style>
p::selection {
color: #fff;
background-color: #7c3aed;
text-shadow: 1px 1px 0 #4c1d95;
}
</style>
<p>Highlight this line to see a purple selection with a subtle shadow.</p>
Highlight this line to see a purple selection with a subtle shadow.
How It Works
Allowed combo. Color, background, and text-shadow all work here.
Highlight. Selected text turns white on purple.
Shadow. A light shadow softens the selected glyphs.
Still limited. You still cannot set font-size or padding on selection.
Edge Cases & Pitfalls
Property limits and accessibility cause most surprises.
props
Most CSS properties are ignored
Font size, margin, padding, border, and background-image do not apply to ::selection.
a11y
Keep contrast high
Selected text must stay readable (aim for WCAG contrast). Do not restyle selection only for looks.
select
user-select: none blocks it
If text cannot be selected, ::selection never appears. See user-select.
legacy
::-moz-selection is legacy
Needed only for very old Firefox. Prefer standard ::selection for modern sites.
inputs
Form controls vary
Selection inside some input/textarea UIs may not fully honor author ::selection styles.
Compatibility
Browser Support
::selection is a browser CSS pseudo-element. Node.js, Deno, and Bun do not evaluate it.
✓ Limited availability
::selection
Useful in Chromium, Firefox, and Safari for basic color/background, but MDN marks it Limited availability (not Baseline). Always verify on your target browsers.
~90%Practical use
Google Chrome1+
Supported*
Mozilla Firefox62+
Standard ::selection
Apple Safari1.1+
Supported*
Microsoft Edge12+
Supported*
Opera9.5+
Supported*
Samsung Internet1+
Supported*
Internet Explorer9+
Partial
::selectionLimited / check engines
Bottom line: *Property support is limited to the allowlist. Older Firefox needed ::-moz-selection. Treat custom selection as progressive enhancement. It is not a JavaScript runtime API.
Status: Limited availability—test before relying on it.
One line:::selection restyles highlighted text with a small allowlist of color and text properties.
Frequently Asked Questions
It styles the part of a document the user has highlighted—by dragging with the mouse, using the keyboard, or similar selection gestures.
Only a small set: color, background-color, text-decoration (and related), text-shadow, and a few WebKit text stroke/fill properties. background-image is ignored.
Yes. Use scoped selectors such as p::selection or h1::selection for different highlight colors.
Often user-select: none blocks selection, or you used an unsupported property such as font-size or padding on ::selection.
Current Firefox supports standard ::selection. The older ::-moz-selection prefix was for legacy Firefox only.
No. It is a CSS pseudo-element evaluated by the browser layout engine, not a Node.js or server runtime feature.