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
# 內容選擇

瀏覽器相容性

另見