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: user-select: all in CSS selects a whole command with one click

Css
~/tips/tip-css-user-select-all-code

user-select: all;

One click on a code snippet selects all of it, ready to copy.

Selecting a long command by dragging always seems to miss the first or last character. With user-select: all a single click selects the whole element.

.copy-me {
  -webkit-user-select: all;
  user-select: all;
}

I’d put it on short things people copy: an install command, an API key placeholder, a hash. Click once and the full text is selected, so Ctrl+C or Cmd+C gets everything. MDN also notes that a double click inside the element keeps the selection on the whole element instead of a single word.

The same property can do the opposite. user-select: none keeps text like button labels or the line numbers in a code listing from being selected, so a copy of the code does not include them.

The prefixed line still matters. Safari supports the value only through -webkit-user-select in current stable versions; MDN lists the unprefixed property for a Safari beta only. So keep both lines.

Use all on small elements only. On a long paragraph it gets annoying fast, because people can no longer select a single sentence to quote.

Further reading: MDN: user-select

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