brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ::selection in CSS changes the color of text people highlight

Css
~/tips/tip-css-selection-highlight-color

::selection {

Selected text gets your own colors instead of the system blue.

When someone drags over text to copy it, the browser paints it in the system highlight color. ::selection lets you pick a color that matches the site.

::selection {
  background-color: #ffe08a;
  color: #1f2328;
}
pre ::selection {
  background-color: #264f78;
  color: #ffffff;
}

The first rule applies to all text on the page. The second one gives code blocks their own darker highlight, because a yellow on a dark code theme is hard to read. Note the space in pre ::selection: that targets selected text inside the block.

Only a small set of properties works here: color, background-color, text-decoration and its parts, text-shadow and a few stroke properties. A background-image or padding is ignored.

I’d always pick a pair with enough contrast. People select text to read it closely or to copy it, and a pale highlight with pale text defeats the purpose.

Desktop browsers all support it. According to MDN, Safari on iPhone and iPad ignores it and keeps the system color, so do not rely on it to make anything readable there.

Further reading: MDN: ::selection

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link