CSS propertyInteraction and more
user-select
Lets you block or ease text selection by the user.
- Initial value
auto- Inherited
- No
- Animatable
- no
- Applies to
- all elements
- JavaScript
element.style.userSelect = "none"
Example
#CSS
.install-command {
user-select: all;
-webkit-user-select: all;
}Example HTML
<p style="margin: 0 0 6px; color: #a3a3a3;">Click once to select the whole command:</p>
<code style="display: block; padding: 8px 12px; border-radius: 8px; background: #111; user-select: all; -webkit-user-select: all;">npm create vite@latest my-project</code>Preview
Definition and usage
#The user-select property controls whether the user can select an element’s text. none blocks selection, all makes a single click select the whole content, and text forces selection to be possible.
user-select: none is useful in interface elements where accidental selection gets in the way, for example buttons clicked many times or draggable cards. The all value works great for fields with code to copy.
Safari still needs the prefixed -webkit-user-select version, so it is worth writing both lines.
Syntax
#Syntax
user-select: auto | text | none | all;Values
#Values in practice
#auto
Normal behaviour: the text can be selected.
.demo { user-select: auto; -webkit-user-select: auto; }none
The text cannot be selected.
.demo { user-select: none; -webkit-user-select: none; }all
A single click selects the whole paragraph.
.demo { user-select: all; -webkit-user-select: all; }Browser support
#Limited support
Not every browser supports it. Check support before you use it in production.
Good practices
#- Don’t block selection in content people may want to copy, such as articles and code.
- Add
-webkit-user-selectnext touser-selectso it works in Safari too.
Related entries
#Practise it in lessons
#Spotted a mistake or missing an example? Write to us.