How to Style Custom Text Selection in CSS: Colors, Gradients & Highlights

The Quick Answer

To style text selection in CSS, use the ::selection pseudo-element. Browsers strictly restrict the properties that can be styled inside ::selection to: color, background-color, text-decoration, text-shadow, and stroke/fill. Always ensure selected text maintains a minimum 4.5:1 WCAG contrast ratio between the foreground text and the highlight background color.

Interactive Demo Live Preview
Interactive Text Selection Preview

Highlight any part of this text to see the custom branded selection in action!

Step 1: Global vs Scoped ::selection Pseudo-Elements

You can apply text selection highlights globally across the entire website on ::selection, or scope unique highlight styles to specific sections (e.g. dark indigo in body copy, vibrant cyan inside code blocks).

selection-rules.css
/* Global Brand Selection Highlight */
::selection {
  background-color: #6366f1;
  color: #ffffff;
}

/* Scoped Code Block Selection Highlight */
pre code::selection {
  background-color: #38bdf8;
  color: #0f172a;
}

Step 2: Allowed Properties in ::selection: What Works & What Fails

For browser security and rendering performance, the CSS specification strictly limits what properties can be declared inside ::selection. Properties like font-size, transform, padding, and border are completely ignored.

allowed-properties.css
::selection {
  /* ALLOWED: */
  color: #ffffff;
  background-color: #ec4899;
  text-decoration: underline wavy #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);

  /* IGNORED (Do not use): */
  font-size: 18px; /* Ignored */
  margin: 10px;    /* Ignored */
  padding: 4px;    /* Ignored */
}

Step 3: Guaranteeing WCAG Contrast on Selected Text

A frequent bug occurs when developers set a light background on ::selection without changing color. On dark mode websites with white text, selecting text makes the white text invisible against a pastel highlight. Always declare both background-color AND color together.

selection-contrast.css
/* SAFE: Both foreground and background are explicitly paired */
::selection {
  background-color: #a78bfa; /* Light Purple Highlight */
  color: #110e1f;            /* Dark Navy Text (11.8:1 Contrast) */
}

Step 4: Semi-Transparent Alpha Highlights with color-mix()

Setting an opaque selection background completely hides underlying image backgrounds. By using color-mix() or hex alpha, selection feels integrated and luminous.

alpha-selection.css
::selection {
  background-color: color-mix(in srgb, var(--accent) 45%, transparent);
  color: #ffffff;
}

Next-Level Addons: Elevating Your Implementation

Take this technique further by combining it with complementary design token studios and animation tools:

Selected Text PASS 9.6:1

1. Text Selection Studio in Interaction Studio

Explore in Custom Selection Studio →

Experiment with brand selection palettes, text shadow extrusions, and verify WCAG contrast ratios across dark and light surfaces in real time.

Luminous Gradient Text

2. Selection Highlights in CSS Text Effects Studio

Explore in CSS Text Effects Studio →

Combine custom text selection highlights with glowing neon typography and metallic gradient text effects.

::selection #ffffff on #6366f1

3. Selection Paired with Color Contrast Checker

Explore in WCAG & APCA Color Contrast Checker →

Verify that your custom ::selection background-color and text color maintain at least 4.5:1 contrast.

Chaam
Chaam's Pro Tip

A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.

Why I Built the Custom Selection Studio

I got tired of guessing values and testing browser quirks in DevTools, so I built this tool to tweak and preview values in real time with 1-click code export.

Open in Custom Selection Studio ↗

Frequently Asked Questions

Practical answers to common implementation questions, browser quirks, and performance tips.

Which CSS properties are allowed inside the ::selection pseudo-element?

The only allowed properties inside ::selection are: color, background-color, text-decoration, text-shadow, and SVG fill and stroke.

Why does setting padding or border inside ::selection have no effect?

Allowing geometry properties like padding, margin, or font size inside text selection would force browser layout reflows every time a user drags their mouse across text, causing severe performance jank.

Why should I always specify color when setting ::selection background-color?

If you only declare background-color, the text keeps its original color. If that text color matches or clashes with your highlight color, selected text becomes completely unreadable.

Can I apply different selection colors to different parts of the same page?

Yes. ::selection can be scoped to any parent selector, such as .hero ::selection or aside ::selection.

Does ::selection work with vendor prefixes like -moz-selection?

Modern browsers have supported standard unprefixed ::selection for many years. Vendor prefixes like ::-moz-selection are no longer required.

← Back to all blog guides Launch Generator Studio →