user-select

CSS 属性 user-select 控制用户能否选中文本。除了文本框内,它对被载入为 chrome 的内容没有影响。

/* Keyword values */
user-select: none;
user-select: auto;
user-select: text;
user-select: contain;
user-select: all;

/* Global values */
user-select: inherit;
user-select: initial;
user-select: unset;

/* Mozilla-specific values */
-moz-user-select: none;
-moz-user-select: text;
-moz-user-select: all;

/* WebKit-specific values */
-webkit-user-select: none;
-webkit-user-select: text;
-webkit-user-select: all; /* Doesn't work in Safari; use only
                             "none" or "text", or else it will
                             allow typing in the <html> container */

/* Microsoft-specific values */
-ms-user-select: none;
-ms-user-select: text;
-ms-user-select: element;
初始值auto
适用元素all elements
是否是继承属性
计算值as specified
Animation typediscrete

注意:user-select 不是继承属性,即使默认的属性值 auto 的表现基本上以继承为主,似乎是继承属性。甚至,WebKit/基于 Chromium 的浏览器在实现此属性时将其作为继承属性,但这和有关规范是相悖的,且会带来一些问题。目前,Chromium 暂时选择修复将其作为继承属性所带来的问题,使最终表现符合规范。

语法

none
元素及其子元素的文本不可选中。 请注意这个Selection 对象可以包含这些元素。 从Firefox 21开始, none 表现的像 -moz-none,因此可以使用 -moz-user-select: text 在子元素上重新启用选择。
auto

auto 的具体取值取决于一系列条件,具体如下:

  • ::before::after 伪元素上,采用的属性值是 none
  • 如果元素是可编辑元素,则采用的属性值是 contain
  • 否则,如果此元素的父元素的 user-select 采用的属性值为 all,则该元素采用的属性值也为 all
  • 否则,如果此元素的父元素的 user-select 采用的属性值为 none,则该元素采用的属性值也为 none
  • 否则,采用的属性值为 text
text
用户可以选择文本。
all
在一个HTML编辑器中,当双击子元素或者上下文时,那么包含该子元素的最顶层元素也会被选中。
contain
允许在元素内选择;但是,选区将被限制在该元素的边界之内。
element (IE 专有别名)
contain 相同,但仅在 Internet Explorer 中受支持。

形式化语法

auto | text | none | contain | all

示例

HTML

<p>你应该可以选中这段文本。</p>
<p class="unselectable">嘿嘿,你不能选中这段文本!</p>
<p class="all">点击一次就会选中这段文本。</p>

CSS

.unselectable {
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.all {
  -moz-user-select: all;
  -webkit-user-select: all;
  -ms-user-select: all;
  user-select: all;
}

Result

规范

规范 状态 备注
CSS Basic User Interface Module Level 4
user-select
Working Draft 初始值。也定义了 -webkit-user-select 作为一个不推荐使用的别名 user-select.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
user-selectChrome Full support 54
Full support 54
Full support 1
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Edge Full support 12
Prefixed
Full support 12
Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Full support 12
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Firefox Full support 69
Full support 69
Full support 49
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Full support 1
Prefixed
Prefixed Implemented with the vendor prefix: -moz-
Full support 44
Prefixed Disabled
Prefixed Implemented with the vendor prefix: -webkit-
Disabled From version 44: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE Full support 10
Prefixed
Full support 10
Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera Full support 15
Prefixed
Full support 15
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Safari Full support 3
Prefixed
Full support 3
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
No support 2 — 3
Prefixed
Prefixed Implemented with the vendor prefix: -khtml-
WebView Android Full support 54
Full support 54
Full support ≤37
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Chrome Android Full support 54
Full support 54
Full support 18
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Firefox Android Full support 49
Prefixed
Full support 49
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Full support 4
Prefixed
Prefixed Implemented with the vendor prefix: -moz-
Full support 44
Prefixed Disabled
Prefixed Implemented with the vendor prefix: -webkit-
Disabled From version 44: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 14
Prefixed
Full support 14
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Safari iOS Full support 3
Prefixed
Full support 3
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Samsung Internet Android Full support 6.0
Full support 6.0
Full support 1.0
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
allChrome Full support 53Edge Full support 79Firefox Full support 1IE No support NoOpera Full support 40Safari No support NoWebView Android Full support 53Chrome Android Full support 53Firefox Android Full support 4Opera Android Full support 41Safari iOS No support NoSamsung Internet Android Full support 6.0
autoChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 10Opera Full support 15Safari Full support 2WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 14Safari iOS Full support 3Samsung Internet Android Full support 1.0
containChrome No support NoEdge No support 12 — 79
Alternate Name
No support 12 — 79
Alternate Name
Alternate Name Uses the non-standard name: element
Firefox No support NoIE Full support 10
Alternate Name
Full support 10
Alternate Name
Alternate Name Uses the non-standard name: element
Opera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
noneChrome Full support 1Edge Full support 12Firefox Full support 21
Full support 21
No support 1 — 65
Prefixed
Prefixed Implemented with the vendor prefix: -moz-
IE Full support 10Opera Full support 15Safari Full support 2WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 21
Full support 21
No support 4 — 65
Prefixed
Prefixed Implemented with the vendor prefix: -moz-
Opera Android Full support 14Safari iOS Full support 3Samsung Internet Android Full support 1.0
textChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 10Opera Full support 15Safari Partial support 2
Notes
Partial support 2
Notes
Notes Allows typing in the <html> container.
WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 14Safari iOS Partial support 3
Notes
Partial support 3
Notes
Notes Allows typing in the <html> container.
Samsung Internet Android Full support 1.0

Legend

Full support  
Full support
Partial support  
Partial support
No support  
No support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.
Uses a non-standard name.
Uses a non-standard name.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

参见