user-select

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

user-select CSS 属性控制用户是否可以选择文本。这不会对作为浏览器用户界面(其chrome)一部分加载的内容产生任何影响,但在文本框中除外。

试一试

user-select: none;
user-select: text;
user-select: all;
<section id="default-example">
  <p id="example-element">Try to select this text</p>
</section>
#example-element {
  font-size: 1.5rem;
}

语法

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

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

注意: user-select 不是一个继承属性,尽管初始值 auto 使它在大多数时候表现得像继承一样。基于 WebKit/Chromium 的浏览器确实将此属性实现为继承的,这违反了规范中描述的行为,并将带来一些问题。到目前为止,Chromium 选择修复了这些问题,以使最终行为符合规范。

none

元素及其子元素的文本不可选择。请注意,Selection 对象可以包含这些元素。

auto

auto 的实际值确定如下:

  • ::before::after 伪元素上,实际值为 none
  • 如果此元素的父元素的 user-select 实际值为 none,则实际值为 none
  • 否则,如果此元素的父元素的 user-select 实际值为 all,则实际值为 all
  • 否则,实际值为 text
文本

文本可以被用户选择。

all

元素的内容应原子化地选择:如果选择包含元素的一部分,则选择必须包含整个元素,包括其所有后代。如果在子元素中发生双击或上下文点击,则将选择具有此值的最高祖先。

注意: CSS 基本用户界面模块为 user-select 属性定义了 contain 值,以允许选择从元素内部开始并由该元素的边界包含,然而,这在任何浏览器中都不受支持。

正式定义

初始值auto
应用于所有元素
继承性
计算值同指定值
动画类型离散

正式语法

user-select = 
auto |
text |
none |
contain |
all

示例

HTML

html
<p>You should be able to select this text.</p>
<p class="unselectable">Hey, you can't select this text!</p>
<p class="all">Clicking once will select all of this text.</p>

CSS

css
.unselectable {
  -webkit-user-select: none; /* Safari */
  user-select: none;
}

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

结果

规范

规范
CSS Basic User Interface Module Level 4
# 内容选择

浏览器兼容性

另见