CSS ::selection Selector

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The ::selection pseudo-element styles the part of a document the user has highlighted—for example by dragging across text.

Only a small set of properties works (mainly color and background-color). Prefer readable contrast; avoid changing selection purely for decoration.

Limited availability.

Not Baseline. Support is broad for basic color/background, but some engines still have gaps—test your target browsers.

Legacy note:

Older Firefox used ::-moz-selection. Current Firefox accepts standard ::selection.

CSS Syntax

syntax.css
::selection { /* or */ E::selection { … }

Example: p::selection { color: red; background-color: yellow; }

Allowed Properties

Only these property groups apply to ::selection. Others are ignored.

AllowedNotesTry it
color / background-colorMain way to restyle the highlightTry it
Scoped E::selectionDifferent highlight per elementTry it
text-shadowAllowed on selectionTry it
text-decorationDecoration and related longhandsTry it

background-image, margins, padding, borders, and font-size do not apply.

How to Use ::selection

Pick global or scoped selection styles, then keep contrast high.

  1. Decide if the rule is global (::selection) or scoped (p::selection).
  2. Set color and background-color with strong contrast.
  3. Skip unsupported properties.
  4. Highlight sample text to verify the look.
PieceRequired?Role
Selectable textYesMust not be blocked by user-select: none
::selectionYesTargets the highlighted fragment
Allowed propertiesYesColor, background-color, limited text styles

Live Preview

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

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>
Try It Yourself

How It Works

  1. Target selection. p::selection styles only highlighted text inside paragraphs.
  2. Set colors. Red text on yellow background is allowed.
  3. User selects. Drag or keyboard-select to activate the style.
  4. 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>
Try It Yourself

How It Works

  1. Global rule. ::selection sets a default highlight.
  2. Scoped rule. p::selection overrides that default inside paragraphs.
  3. Compare. Select outside vs inside the p to see both looks.
  4. 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>
Try It Yourself

How It Works

  1. Allowed combo. Color, background, and text-shadow all work here.
  2. Highlight. Selected text turns white on purple.
  3. Shadow. A light shadow softens the selected glyphs.
  4. 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.

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 Chrome 1+
Supported*
Mozilla Firefox 62+
Standard ::selection
Apple Safari 1.1+
Supported*
Microsoft Edge 12+
Supported*
Opera 9.5+
Supported*
Samsung Internet 1+
Supported*
Internet Explorer 9+
Partial
::selection Limited / 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.

Key Takeaways

  • Role: style user-highlighted text.
  • Allowlist: mainly color, background-color, text-decoration, text-shadow.
  • A11y: keep selected text contrast readable.
  • 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.

Next: :target

Style the element that matches the current URL hash.

:target tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful