CSS Reference ยท Scrolling, Interaction and UI
The user-select property
Reference for the CSS user-select property: stop text from being selected on buttons, select a whole block with one click, and know when not to use it.
The user-select property controls whether the user can select the text of an element by dragging, double-clicking or using the keyboard. It changes what can be highlighted, not what is in the page: the text is still in the DOM, still read by screen readers and still found by search.
Typical uses are making interface labels and icon buttons unselectable so a quick double-click does not paint them blue, and making a code snippet or token select in one click.
.toolbar button { user-select: none; } .token { user-select: all; }
Try it
Syntax
user-select: auto | none | text | all | contain
- Initial value:
auto - Applies to: all elements
- Inherited: no, but the behavior of
autodepends on the parent (see below) - Animatable: no
- Computed value: as specified, except that
autois resolved according to the element and its parent
Values
| Value | Description |
|---|---|
auto |
The browser decides. Normally this is the same as text, except for some UI elements and for children of an element whose value is none or all. |
none |
The text cannot be selected, and selections do not start inside the element. |
text |
The text can be selected. Use it to turn selection back on inside a none parent. |
all |
One click or tap selects the whole element, and a selection that touches the element includes all of it. Good for copy-ready values. |
contain |
A selection that starts inside the element stays inside it. Support is patchy; check a compatibility table before relying on it. |
Usage notes
- Use it on interface chrome, not on content. Buttons, tabs, drag handles and icon labels are good candidates. Articles, error messages and anything someone may want to quote or search for are not. Blocking selection is a common annoyance and it does not protect anything: the text can still be read from the source or the page.
- It does not stop copying. Users can still view the source, use developer tools or save the page. Never treat
noneas a security or copyright measure. - Form fields stay editable. Inputs and textareas keep working even if an ancestor has
user-select: none, because the browser treats editable fields specially. Browsers do differ in small details, so test the controls you care about. - Double-click on rapid UI. Buttons that are clicked in quick succession (counters, steppers) are the classic reason to add
user-select: none. - Prefixes. Safari has long needed the
-webkit-user-selectform, so many style sheets write both declarations, as in the example. Check a compatibility table for the version range you support. - Style the selection itself with
::selection. - Accessibility. Screen readers and keyboard users read text through the accessibility tree, which this property does not change. People who use selection to translate, look up or read aloud are affected though, so keep it off real content.
Browser support
Supported in every current browser; Safari needs the -webkit- prefix, and the contain value is not reliably supported.
Related
pointer-eventsโ controls pointer targetingcursorโ sets the mouse cursor::selectionโ styles selected textcaret-colorโ color of the text caret- Lesson: User Interface