CSS user-select: Select a Whole Code Block with One Click
Setting user-select: all on a code element selects the whole code block with a single click on the text. Conversely, any part with user-select: none is left out of the selection even when you drag over it. When you ask readers to copy command examples in a blog or technical documentation, this lets them select everything in one action, and it keeps prompt symbols like "$" and line numbers out of the copied text.
The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened the page with Playwright, sent mouse clicks and drags, read the selected text with getSelection().toString(), and read the copied text with navigator.clipboard.readText() after Ctrl+C. I did not test Firefox, Safari, or real smartphones (selection by long-press). The browser version support figures are from the compatibility data (@mdn/browser-compat-data 8.1.8).

Select the whole code with one click
Set all for user-select on the <code> element of the command example.
<pre><code class="copy-all">npm install example-package</code></pre>
.copy-all {
-webkit-user-select: all; /* Safari */
user-select: all;
}
In the test environment, a single click on the text of a <code> with no setting selected nothing. A double-click selected only the word example, and a triple-click selected the whole line.
With user-select: all on the <code>, a single click anywhere on the text selected all of npm install example-package. Pressing Ctrl+C after that copied the same string.
According to MDN, the contents of an element set to all are selected as a single unit. Even if you try to select only part of it, the whole element is selected. The test environment behaved the same way:
- Dragging across only the
installpart of the code selected the whole code block. - Dragging from near the end of a paragraph above the code to the middle of the code selected the code through to its end.
- Clicking a
<span>inside an element set toallselected the whole element that hadall, not just the<span>.
When the block was already selected, clicking it again cleared the selection. A third click selected the whole block again.
Also, in the <pre>, clicking the empty space on the right side, where there is no text, selected nothing. This is because user-select: all was set on the <code>, and the empty space belongs to the <pre>. A click only selects the element that has all.
Keep the prompt "$" and line numbers out of the copied text
The $ at the start of a command example and the line numbers in code are useful to see, but you don't want them in the copied text. Wrap these parts in a <span> and set user-select: none.
<pre><code><span class="prompt">$ </span>npm install example-package
<span class="prompt">$ </span>npm run build</code></pre>
<pre><code><span class="line-no">1</span>const a = 1;
<span class="line-no">2</span>console.log(a);</code></pre>
.prompt,
.line-no {
-webkit-user-select: none; /* Safari */
user-select: none;
}

In the test environment, I dragged from the beginning of npm on line 1 to the end of line 2 and copied with Ctrl+C. With user-select: none in place, the copied text was as follows, and the $ on line 2 was not included.
npm install example-package
npm run build
When I removed user-select: none and did the same operation, line 2 came out as $ npm run build. Without the setting, the line numbers were also copied joined together, as in 2console.log(a);. With the setting, only console.log(a); was copied. The $ and line numbers were also left out when I dragged from the paragraph above all the way to the bottom of the code, and when I selected the whole page with Ctrl+A.
One thing to watch out for is starting a drag from a part with user-select: none. In the test environment, when I started a drag on the $ at the start of a line or on a line number, nothing was selected. The CSS specification (CSS Basic User Interface Level 4) also states that selection cannot start inside an element with none. When selecting a command, start the drag at the first character of the command, to the right of the $ .
Stop text in button-like elements from being selected
Tabs and buttons built from <span> or <div> can end up with their text selected if you click them quickly several times. In the test environment, double-clicking a tab built from a <span>, the tab labeled "Usage" in the sample code, selected its text. Triple-clicking selected the whole row of tabs. When I set user-select: none on the tabs, neither action selected anything.
<div class="tabs no-select">
<span class="tab" role="tab" tabindex="0">概要</span>
<span class="tab" role="tab" tabindex="0">使い方</span>
</div>
.no-select .tab {
-webkit-user-select: none; /* Safari */
user-select: none;
}
On the other hand, for <button> elements, the Chromium in the test environment did not select the text on double-click or triple-click, whether or not user-select: none was set.
Make a selectable part inside a non-selectable area
When you set user-select: none on a parent element, its child elements also cannot be selected. To make only part of the area selectable, set user-select: text on that child.
<div class="panel">
<p>設定パネル(ここは選択できない)</p>
<p class="selectable">API_KEY_EXAMPLE_0000</p>
</div>
.panel {
user-select: none;
}
.panel .selectable {
user-select: text;
}
In the test environment, double-clicking the first <p> selected nothing, while double-clicking the <p> with .selectable selected API_KEY_EXAMPLE_0000. The user-select property is not inherited according to the specification, yet the children became unselectable anyway. This is because the initial value auto is determined by looking at the parent's value. According to MDN's page on user-select, auto behaves as none if the parent is none, as all if the parent is all, and as text otherwise. As a result, elements inside .panel can only be selected if they have text set explicitly.
One note: when I read the value with getComputedStyle() in the test environment's Chromium, the user-select of the first <p>, which had no setting, returned none rather than auto. The specification says the computed value should be the value as specified (here, auto). The same MDN page notes that Chromium and WebKit-based browsers implement this property as inherited. That is why getComputedStyle() in Chromium returns the none inherited from the parent.
Use -webkit- for Safari
According to the compatibility data, Chrome supports the unprefixed user-select from version 54, Edge from 79, and Firefox from 69. Safari only supports the unprefixed user-select in its Technology Preview, so the stable release requires -webkit-user-select. The iOS Safari also only supports -webkit-user-select. For this reason, the CSS in this article lists -webkit-user-select and user-select side by side.
By value, all is supported from version 16 in Safari and iOS Safari, and from version 53 in Chrome. none, text, and auto have been supported in all browsers for a long time.
The specification also defines a value called contain, but according to MDN, no browser supports it.
user-select: none is not a way to prevent people from copying text. The CSS specification allows browsers to provide selection in other ways, and users can disable it in their own style sheets. The specification also gives examples of usability problems: if none is applied to the whole page and only some parts are made selectable, users may be unable to clear the selection by clicking empty space. Apply none only to the parts that are likely to be selected by accident.





