Gotta Select'em All
CSS can control how text is selected, though it isn't widely known. The property user-select: none; keeps text from being selected. As a general rule that's questionable UX, but there are narrow cases where it helps — for instance, decorative period (.) characters could be excluded from selection.
The mirror case is user-select: all;, which forces an element's entire text to be selected. That's also somewhat iffy UX. Making someone select all of the text is fairly rare on the web, and actively blocking selection of part of it comes across as trying so hard to be helpful that it actually hurts.
For more detail, Dag Frode Solberg has a write-up at css.christmas/2019/5.
The author forked his demo to show a simple scenario where click-to-select might make some level of sense:
const cells = document.querySelectorAll("td");
function highlight(event) {
window.getSelection()
.selectAllChildren(
event.target
);
event.target.removeEventListener("click", highlight);
}
cells.forEach(cell => {
cell.addEventListener("click", highlight);
});
To implement click once to highlight all and then stop interfering, you can do that in JavaScript with a click handler that removes itself after the first click.

Sponsor CSS-Tricks!
We've got lots of ways to help you connect with the greatest front-end community in all the land.

CSS-Tricks Newsletter
Ads are self-hosted



